Skip to content

Commit

Permalink
feat: hook up disable proxy feature flag (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Jan 12, 2025
1 parent 9bc5425 commit 4ca0df0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 51 deletions.
10 changes: 5 additions & 5 deletions packages/fern-docs/edge-config/src/getEdgeFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const EDGE_FLAGS = [
"http-snippets-enabled" as const,
"inline-feedback-enabled" as const,
"dark-code-enabled" as const,
"proxy-uses-app-buildwithfern" as const,
"disable-proxy" as const,
"image-zoom-disabled" as const,
"use-javascript-as-typescript" as const,
"always-enable-javascript-fetch" as const,
Expand Down Expand Up @@ -87,9 +87,9 @@ export async function getEdgeFlags(domain: string): Promise<EdgeFlags> {
domain,
config["dark-code-enabled"]
);
const proxyShouldUseAppBuildwithfernCom = checkDomainMatchesCustomers(
const isProxyDisabled = checkDomainMatchesCustomers(
domain,
config["proxy-uses-app-buildwithfern"]
config["disable-proxy"]
);
const isImageZoomDisabled = checkDomainMatchesCustomers(
domain,
Expand Down Expand Up @@ -184,7 +184,7 @@ export async function getEdgeFlags(domain: string): Promise<EdgeFlags> {
isHttpSnippetsEnabled,
isInlineFeedbackEnabled,
isDarkCodeEnabled,
proxyShouldUseAppBuildwithfernCom,
isProxyDisabled,
isImageZoomDisabled,
useJavaScriptAsTypeScript,
alwaysEnableJavaScriptFetch,
Expand Down Expand Up @@ -219,7 +219,7 @@ export async function getEdgeFlags(domain: string): Promise<EdgeFlags> {
isHttpSnippetsEnabled: false,
isInlineFeedbackEnabled: isFern(domain),
isDarkCodeEnabled: false,
proxyShouldUseAppBuildwithfernCom: false,
isProxyDisabled: false,
isImageZoomDisabled: false,
useJavaScriptAsTypeScript: false,
alwaysEnableJavaScriptFetch: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export const PlaygroundEndpoint = ({
);
});

const { usesApplicationJsonInFormDataValue } = useEdgeFlags();
const { usesApplicationJsonInFormDataValue, isProxyDisabled } =
useEdgeFlags();
const [response, setResponse] =
useState<Loadable<PlaygroundResponse>>(notStartedLoading());

Expand Down Expand Up @@ -130,7 +131,7 @@ export const PlaygroundEndpoint = ({
),
};
if (endpoint.responses?.[0]?.body.type === "stream") {
const [res, stream] = await executeProxyStream(req);
const [res, stream] = await executeProxyStream(req, isProxyDisabled);

const time = Date.now();
const reader = stream.getReader();
Expand All @@ -156,7 +157,7 @@ export const PlaygroundEndpoint = ({
);
}
} else {
const res = await executeProxyRest(req);
const res = await executeProxyRest(req, isProxyDisabled);
setResponse(loaded(res));
if (res.type !== "stream") {
track("api_playground_request_received", {
Expand Down Expand Up @@ -191,6 +192,7 @@ export const PlaygroundEndpoint = ({
baseUrl,
setOAuthValue,
usesApplicationJsonInFormDataValue,
isProxyDisabled,
]);

const settings = usePlaygroundSettings();
Expand Down
27 changes: 0 additions & 27 deletions packages/fern-docs/ui/src/playground/fetch-utils/executeGrpc.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { toBodyInit } from "./requestToBodyInit";
const PROXY_URL = "https://proxy.ferndocs.com/";

export async function executeProxyRest(
req: ProxyRequest
req: ProxyRequest,
disableProxy: boolean = false
): Promise<PlaygroundResponse> {
const requestHeaders = new Headers(req.headers);
requestHeaders.set(
Expand All @@ -19,12 +20,15 @@ export async function executeProxyRest(
requestHeaders.delete("Content-Type");
}

const res = await fetch(urljoin(PROXY_URL, req.url), {
method: req.method,
headers: requestHeaders,
body: await toBodyInit(req.body),
mode: "cors",
});
const res = await fetch(
disableProxy ? req.url : urljoin(PROXY_URL, req.url),
{
method: req.method,
headers: requestHeaders,
body: await toBodyInit(req.body),
mode: "cors",
}
);

const responseHeadersList = (
res.headers.get("X-Fern-Proxy-Response-Headers") ?? ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { toBodyInit } from "./requestToBodyInit";
const PROXY_URL = "https://proxy.ferndocs.com/";

export async function executeProxyStream(
req: ProxyRequest
req: ProxyRequest,
disableProxy: boolean = false
): Promise<[Response, ReadableStream<Uint8Array>]> {
const requestHeaders = new Headers(req.headers);
requestHeaders.set(
Expand All @@ -18,12 +19,15 @@ export async function executeProxyStream(
requestHeaders.delete("Content-Type");
}

const response = await fetch(urljoin(PROXY_URL, req.url), {
method: req.method,
headers: requestHeaders,
body: await toBodyInit(req.body),
mode: "cors",
});
const response = await fetch(
disableProxy ? req.url : urljoin(PROXY_URL, req.url),
{
method: req.method,
headers: requestHeaders,
body: await toBodyInit(req.body),
mode: "cors",
}
);

if (response.body == null) {
throw new Error("Response body is null");
Expand Down
4 changes: 2 additions & 2 deletions packages/fern-docs/utils/src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface EdgeFlags {
isHttpSnippetsEnabled: boolean;
isInlineFeedbackEnabled: boolean;
isDarkCodeEnabled: boolean;
proxyShouldUseAppBuildwithfernCom: boolean;
isProxyDisabled: boolean;
isImageZoomDisabled: boolean;
useJavaScriptAsTypeScript: boolean;
alwaysEnableJavaScriptFetch: boolean;
Expand Down Expand Up @@ -43,7 +43,7 @@ export const DEFAULT_EDGE_FLAGS: EdgeFlags = {
isHttpSnippetsEnabled: false,
isInlineFeedbackEnabled: false,
isDarkCodeEnabled: false,
proxyShouldUseAppBuildwithfernCom: false,
isProxyDisabled: false,
isImageZoomDisabled: false,
useJavaScriptAsTypeScript: false,
alwaysEnableJavaScriptFetch: false,
Expand Down

0 comments on commit 4ca0df0

Please sign in to comment.