Skip to content

Commit

Permalink
Add the maxMetaTagSize parameter to the proxy client
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalzieu committed Mar 28, 2024
1 parent b6a5a36 commit de5e422
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
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

0 comments on commit de5e422

Please sign in to comment.