Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noe/add meta tag max size #23

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/smart-lobsters-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@open-frames/proxy-client": patch
"@open-frames/proxy": patch
---

Adding the max meta tag size parameter
28 changes: 19 additions & 9 deletions packages/client/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import type { GetMetadataResponse, PostRedirectResponse } from '@open-frames/pro
import { ApiError } from './errors.js';
import { JSONSerializable } from './types.js';

export async function readMetadata(url: string, proxyUrl: string): Promise<GetMetadataResponse> {
const response = await fetch(`${proxyUrl}?url=${encodeURIComponent(url)}`);
export async function readMetadata(url: string, proxyUrl: string, maxMetaTagSize?: number | undefined): Promise<GetMetadataResponse> {
const response = await fetch(
`${proxyUrl}?url=${encodeURIComponent(url)}${maxMetaTagSize ? `&max-meta-tag-bytes=${maxMetaTagSize}` : ''}`,
);

if (!response.ok) {
throw new ApiError(`Failed to read metadata for ${url}`, response.status);
Expand All @@ -13,14 +15,22 @@ export async function readMetadata(url: string, proxyUrl: string): Promise<GetMe
return (await response.json()) as GetMetadataResponse;
}

export async function post(url: string, payload: JSONSerializable, proxyUrl: string): Promise<GetMetadataResponse> {
const response = await fetch(`${proxyUrl}?url=${encodeURIComponent(url)}`, {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
export async function post(
url: string,
payload: JSONSerializable,
proxyUrl: string,
maxMetaTagSize?: number | undefined,
): Promise<GetMetadataResponse> {
const response = await fetch(
`${proxyUrl}?url=${encodeURIComponent(url)}${maxMetaTagSize ? `&max-meta-tag-bytes=${maxMetaTagSize}` : ''}`,
{
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
},
},
});
);

if (!response.ok) {
throw new Error(`Failed to post to frame: ${response.status} ${response.statusText}`);
Expand Down
8 changes: 5 additions & 3 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { JSONSerializable } from './types.js';

export class OpenFramesProxy {
baseUrl: string;
maxMetaTagSize: number | undefined;

constructor(baseUrl: string) {
constructor(baseUrl: string, maxMetaTagSize?: number | undefined) {
this.baseUrl = baseUrl;
this.maxMetaTagSize = maxMetaTagSize;
}

async readMetadata(url: string): Promise<GetMetadataResponse> {
return readMetadata(url, this.baseUrl);
return readMetadata(url, this.baseUrl, this.maxMetaTagSize);
}

async post(url: string, payload: JSONSerializable): Promise<GetMetadataResponse> {
return post(url, payload, this.baseUrl);
return post(url, payload, this.baseUrl, this.maxMetaTagSize);
}

async postRedirect(url: string, payload: JSONSerializable): Promise<PostRedirectResponse> {
Expand Down
Loading