-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
176 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/ui/docs-bundle/src/app/api/fern-docs/proxy/rest/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { buildRequestBody } from "@/server/buildRequestBody"; | ||
import { withProxyCors } from "@/server/withProxyCors"; | ||
import { getDocsDomainEdge } from "@/server/xfernhost/edge"; | ||
import { ProxyRequestSchema } from "@fern-ui/ui"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
/** | ||
* Note: edge functions must return a response within 25 seconds. | ||
*/ | ||
|
||
export const runtime = "edge"; | ||
|
||
export async function OPTIONS(req: NextRequest): Promise<NextResponse> { | ||
const headers = new Headers(withProxyCors(getDocsDomainEdge(req))); | ||
|
||
return new NextResponse(null, { status: 204, headers }); | ||
} | ||
|
||
export async function POST(req: NextRequest): Promise<NextResponse> { | ||
const headers = new Headers(withProxyCors(getDocsDomainEdge(req))); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("Starting proxy request to", req.url); | ||
|
||
try { | ||
const body = await req.json(); | ||
const proxyRequest = ProxyRequestSchema.parse(body); | ||
const [mime, requestBody] = await buildRequestBody(proxyRequest.body); | ||
const proxyHeaders = new Headers(proxyRequest.headers); | ||
|
||
// omit content-type for multipart/form-data so that fetch can set it automatically with the boundary | ||
const contentType = proxyHeaders.get("Content-Type"); | ||
if (contentType != null && contentType.toLowerCase().includes("multipart/form-data")) { | ||
proxyHeaders.delete("Content-Type"); | ||
} else if (mime != null) { | ||
proxyHeaders.set("Content-Type", mime); | ||
} | ||
|
||
const startTime = Date.now(); | ||
|
||
const response = await fetch(proxyRequest.url, { | ||
method: proxyRequest.method, | ||
headers: proxyHeaders, | ||
body: requestBody, | ||
}); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("Proxy request to", req.url, "completed with status", response.status); | ||
|
||
let responseBody = await response.text(); | ||
const endTime = Date.now(); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("Proxy request to", req.url, "received response body after", endTime - startTime, "milliseconds"); | ||
|
||
try { | ||
responseBody = JSON.parse(responseBody); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.log("Failed to parse response body as JSON, but will return it as text."); | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
} | ||
|
||
response.headers.forEach((value, name) => { | ||
headers.set(name, value); | ||
}); | ||
|
||
return new NextResponse( | ||
JSON.stringify({ | ||
response: { | ||
headers: Object.fromEntries(response.headers.entries()), | ||
ok: response.ok, | ||
redirected: response.redirected, | ||
status: response.status, | ||
statusText: response.statusText, | ||
type: response.type, | ||
url: response.url, | ||
body: responseBody, | ||
}, | ||
time: endTime - startTime, | ||
size: response.headers.get("Content-Length"), | ||
}), | ||
{ | ||
status: 200, | ||
headers, | ||
}, | ||
); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(err); | ||
return new NextResponse(null, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 0 additions & 119 deletions
119
packages/ui/docs-bundle/src/pages/api/fern-docs/proxy/grpc.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.