Skip to content

Commit

Permalink
hack: fileforge uses json blob (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored May 3, 2024
1 parent 836e23a commit 3bcc15c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
12 changes: 8 additions & 4 deletions packages/ui/app/src/api-playground/PlaygroundEndpoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const PlaygroundEndpoint: FC<PlaygroundEndpointProps> = ({
resetWithoutExample,
types,
}): ReactElement => {
const { basePath } = useDocsContext();
const { basePath, domain } = useDocsContext();
const [response, setResponse] = useState<Loadable<PlaygroundResponse>>(notStartedLoading());
// const [, startTransition] = useTransition();

Expand All @@ -187,7 +187,7 @@ export const PlaygroundEndpoint: FC<PlaygroundEndpointProps> = ({
url: buildEndpointUrl(endpoint, formState),
method: endpoint.method,
headers: buildUnredactedHeaders(endpoint, formState),
body: await serializeFormStateBody(formState.body, basePath),
body: await serializeFormStateBody(formState.body, basePath, domain),
};
if (endpoint.responseBody?.shape.type === "stream") {
const [res, stream] = await executeProxyStream(req, basePath);
Expand Down Expand Up @@ -245,7 +245,7 @@ export const PlaygroundEndpoint: FC<PlaygroundEndpointProps> = ({
},
});
}
}, [basePath, endpoint, formState]);
}, [basePath, domain, endpoint, formState]);

return (
<FernTooltipProvider>
Expand Down Expand Up @@ -283,6 +283,7 @@ export const PlaygroundEndpoint: FC<PlaygroundEndpointProps> = ({
async function serializeFormStateBody(
body: PlaygroundFormStateBody | undefined,
basePath: string | undefined,
domain: string,
): Promise<ProxyRequest.SerializableBody | undefined> {
if (body == null) {
return undefined;
Expand Down Expand Up @@ -316,7 +317,10 @@ async function serializeFormStateBody(
assertNever(value);
}
}
return { type: "form-data", value: formDataValue };
// this is a hack to allow the API Playground to send JSON blobs in form data
// revert this once we have a better solution
const isJsonBlob = domain.includes("fileforge");
return { type: "form-data", value: formDataValue, isJsonBlob };
}
case "octet-stream":
return { type: "octet-stream", value: await serializeFile(body.value, basePath) };
Expand Down
1 change: 1 addition & 0 deletions packages/ui/app/src/api-playground/types/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export declare namespace ProxyRequest {
export interface SerializableFormData {
type: "form-data";
value: Record<string, SerializableFormDataEntryValue>;
isJsonBlob?: boolean; // this is a hack to allow the API Playground to send JSON blobs in form data
}

export interface SerializableOctetStream {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ export async function buildRequestBody(body: ProxyRequest.SerializableBody | und
);
return [key, files] as const;
}
case "json":
case "json": {
if (body.isJsonBlob) {
return [
key,
new Blob([JSON.stringify(value.value)], { type: "application/json" }),
] as const;
}
return [key, JSON.stringify(value.value)] as const;
}
default:
assertNever(value);
}
Expand Down

0 comments on commit 3bcc15c

Please sign in to comment.