Skip to content

Commit

Permalink
merge from main
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Dec 19, 2024
2 parents cec5db2 + 4f8db4d commit 73ba050
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const AudioExampleInternal: FC<AudioExample.Props> = ({ ...props }) => {
return (
<TitledExample {...props}>
<FernAudioPlayer
src="https://eleven-public-cdn.elevenlabs.io/audio-native/d65e433fd8a560cbba1b7dd26809dd48ea2b408c5b6c0a3e42e5b83c43957f5b/ISOcFnHEWohrLHrKmdyq.mp3"
src="https://files.buildwithfern.com/elevenlabs-apiref.mp3"
title={"Audio by ElevenLabs"}
className="p-4"
/>
Expand Down
2 changes: 0 additions & 2 deletions packages/ui/docs-bundle/.depcheckrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
"tailwindcss",
"jsonpath",
"cssnano",
"node-fetch",
"@types/node-fetch",
"sharp",
"esbuild",
"glslify-import",
Expand Down
4 changes: 0 additions & 4 deletions packages/ui/docs-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@fern-api/fdr-sdk": "workspace:*",
"@fern-api/ui-core-utils": "workspace:*",
"@fern-fern/fern-docs-sdk": "0.0.5",
"@fern-fern/proxy-sdk": "0.0.26",
"@fern-ui/components": "workspace:*",
"@fern-ui/fdr-utils": "workspace:*",
"@fern-ui/fern-docs-auth": "workspace:*",
Expand All @@ -55,12 +54,10 @@
"es-toolkit": "^1.27.0",
"esbuild": "0.20.2",
"feed": "^4.2.2",
"form-data": "4.0.0",
"iron-session": "^8.0.3",
"jose": "^5.2.3",
"jsonpath": "^1.1.1",
"next": "^14",
"node-fetch": "2.7.0",
"postcss-import": "^16.0.1",
"posthog-node": "^4.2.1",
"qs": "6.12.0",
Expand All @@ -78,7 +75,6 @@
"@tailwindcss/forms": "^0.5.7",
"@tailwindcss/typography": "^0.5.10",
"@types/node": "^18.7.18",
"@types/node-fetch": "2.6.9",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.4.16",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import { buildRequestBody } from "@/server/buildRequestBody";
import { withProxyCors } from "@/server/withProxyCors";
import { getDocsDomainEdge } from "@/server/xfernhost/edge";
import { ProxyRequestSchema } from "@fern-ui/ui";
import { buildRequestBody } from "./rest";
import { NextRequest, NextResponse } from "next/server";

/**
* Note: edge functions must return a response within 25 seconds.
*/

export const runtime = "edge";
export const dynamic = "force-dynamic";

export default async function handler(req: Request): Promise<Response> {
if (req.method !== "POST" && req.method !== "OPTIONS") {
return new Response(null, { status: 405 });
}

const origin = req.headers.get("origin");
if (origin == null) {
return new Response(null, { status: 400 });
}

const headers = new Headers({
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "Content-Type",
});
export async function OPTIONS(req: NextRequest): Promise<NextResponse> {
const headers = new Headers(withProxyCors(getDocsDomainEdge(req)));
return new NextResponse(null, { status: 204, headers });
}

if (req.method === "OPTIONS") {
return new Response(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);
Expand Down Expand Up @@ -56,13 +46,13 @@ export default async function handler(req: Request): Promise<Response> {
headers.set(name, value);
});

return new Response(response.body, {
return new NextResponse(response.body, {
status: response.status,
headers,
});
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
return new Response(null, { status: 500 });
return new NextResponse(null, { status: 500 });
}
}
94 changes: 94 additions & 0 deletions packages/ui/docs-bundle/src/app/api/fern-docs/proxy/rest/route.ts
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 });
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { withProxyCors } from "@/server/withProxyCors";
import { getDocsDomainEdge } from "@/server/xfernhost/edge";
import { ProxyRequestSchema } from "@fern-ui/ui";
import { NextRequest, NextResponse } from "next/server";

Expand All @@ -6,27 +8,15 @@ import { NextRequest, NextResponse } from "next/server";
*/

export const runtime = "edge";
export const dynamic = "force-dynamic";

export default async function POST(req: NextRequest): Promise<NextResponse<null | Uint8Array>> {
if (req.method !== "POST" && req.method !== "OPTIONS") {
return new NextResponse(null, { status: 405 });
}

const origin = req.headers.get("Origin");
if (origin == null) {
return new NextResponse(null, { status: 400 });
}
export async function OPTIONS(req: NextRequest): Promise<NextResponse> {
const headers = new Headers(withProxyCors(getDocsDomainEdge(req)));
return new NextResponse(null, { status: 204, headers });
}

const corsHeaders = {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "Content-Type",
};
export async function POST(req: NextRequest): Promise<NextResponse<null | Uint8Array>> {
const corsHeaders = new Headers(withProxyCors(getDocsDomainEdge(req)));

if (req.method === "OPTIONS") {
return new NextResponse(null, { status: 204, headers: corsHeaders });
}
try {
const proxyRequest = ProxyRequestSchema.parse(await req.json());
const startTime = Date.now();
Expand Down
119 changes: 0 additions & 119 deletions packages/ui/docs-bundle/src/pages/api/fern-docs/proxy/grpc.ts

This file was deleted.

Loading

0 comments on commit 73ba050

Please sign in to comment.