Skip to content

Commit

Permalink
OpenAPI: fix encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Dec 18, 2024
1 parent 92ef326 commit dada668
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions apps/docs/app/docs/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function PreviewRenderer({ preview }: { preview: string }): ReactNode {
return null;
}

export const dynamicParams = false;
export const revalidate = false;

export default async function Page(props: {
params: Promise<{ slug: string[] }>;
}): Promise<ReactElement> {
Expand Down
37 changes: 37 additions & 0 deletions apps/docs/content/docs/ui/(integrations)/openapi/proxy.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: Creating Proxy
description: Avoid CORS problem
---

## Introduction

A proxy server is useful for executing HTTP (`fetch`) requests, as it doesn't have CORS constraints like on the browser.
We can use it for executing HTTP requests on the OpenAPI playground, when the target API endpoints do not have CORS configured correctly.

<Callout type="warn" title="Warning">
Do not use this on unreliable sites and API endpoints, the proxy server will
forward all received headers & body, including HTTP-only `Cookies` and
`Authorization` header.
</Callout>

### Setup

Create a route handler for proxy server.

```ts title="/api/proxy/route.ts"
import { openapi } from '@/lib/source';

export const { GET, HEAD, PUT, POST, PATCH, DELETE } = openapi.createProxy();
```

> Follow the [Getting Started](/docs/ui/openapi) guide if `openapi` server is not yet configured.
And enable the proxy from `createOpenAPI`.

```ts title="lib/source.ts"
import { createOpenAPI } from 'fumadocs-openapi/server';

export const openapi = createOpenAPI({
proxyUrl: '/api/proxy',
});
```
13 changes: 8 additions & 5 deletions packages/openapi/src/server/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export function createProxy(allowedUrls?: string[]): Proxy {
});
}

const clonedReq = new Request(url, req);
const clonedReq = new Request(url, {
...req,
cache: 'no-cache',
mode: 'cors',
});
clonedReq.headers.forEach((_value, originalKey) => {
const key = originalKey.toLowerCase();
const notAllowed = key === 'origin';
Expand All @@ -40,9 +44,7 @@ export function createProxy(allowedUrls?: string[]): Proxy {
}
});

const res = await fetch(clonedReq, {
cache: 'no-cache',
}).catch((e) => new Error(e.toString()));
const res = await fetch(clonedReq).catch((e) => new Error(e.toString()));
if (res instanceof Error) {
return Response.json(`Failed to proxy request: ${res.message}`, {
status: 400,
Expand All @@ -52,7 +54,8 @@ export function createProxy(allowedUrls?: string[]): Proxy {
const headers = new Headers(res.headers);
headers.forEach((_value, originalKey) => {
const key = originalKey.toLowerCase();
const notAllowed = key.startsWith('access-control-');
const notAllowed =
key.startsWith('access-control-') || key === 'content-encoding';

if (notAllowed) {
headers.delete(originalKey);
Expand Down
1 change: 1 addition & 0 deletions packages/openapi/src/ui/playground/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function createBrowserFetcher(

return fetch(input.url, {
method: input.method,
cache: 'no-cache',
headers,
body: bodySchema
? createBodyFromValue(
Expand Down

0 comments on commit dada668

Please sign in to comment.