Skip to content

Commit

Permalink
fix: fallback to string when response is not JSONable. (#2097)
Browse files Browse the repository at this point in the history
Co-authored-by: eden <[email protected]>
Co-authored-by: Deep Singhvi <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2025
1 parent b6d4891 commit b8bb89d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ export const PlaygroundEndpoint = ({
break;
}
result += decoder.decode(value);
console.log(result);
setResponse(
loaded({
type: "stream",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,62 @@ export async function executeProxyRest(
res.headers.get("Content-Type")?.toLowerCase()?.includes("application/json")
) {
const startTime = Date.now();
const json = await res.json();
const endTime = Date.now();
try {
const text = await res.text();
const endTime = Date.now();

const fallbackTime =
Number(res.headers.get("X-Fern-Proxy-Origin-Latency") ?? 0) +
endTime -
startTime;
const fallbackTime =
Number(res.headers.get("X-Fern-Proxy-Origin-Latency") ?? 0) +
endTime -
startTime;

return {
type: "json",
response: {
headers: responseHeaders,
ok: res.ok,
redirected: res.redirected,
status: res.status,
statusText: res.statusText,
type: res.type,
url: res.url,
body: json,
},
contentType: res.headers.get("Content-Type") ?? "application/json",
time: Number(
res.headers.get("X-Fern-Proxy-Response-Time") ?? fallbackTime
),
size:
res.headers.get("Content-Length") ??
String(new TextEncoder().encode(JSON.stringify(json)).length),
};
try {
const json = JSON.parse(text);
return {
type: "json",
response: {
headers: responseHeaders,
ok: res.ok,
redirected: res.redirected,
status: res.status,
statusText: res.statusText,
type: res.type,
url: res.url,
body: json,
},
contentType: res.headers.get("Content-Type") ?? "application/json",
time: Number(
res.headers.get("X-Fern-Proxy-Response-Time") ?? fallbackTime
),
size:
res.headers.get("Content-Length") ??
String(new TextEncoder().encode(text).length),
};
} catch {
return {
type: "string",
response: {
headers: responseHeaders,
ok: res.ok,
redirected: res.redirected,
status: res.status,
statusText: res.statusText,
type: res.type,
url: res.url,
body: text,
},
contentType: res.headers.get("Content-Type") ?? "text/plain",
time: Number(
res.headers.get("X-Fern-Proxy-Response-Time") ?? fallbackTime
),
size:
res.headers.get("Content-Length") ??
String(new TextEncoder().encode(text).length),
};
}
} catch () {
throw new Error("Failed to read response body");
}
}

const startTime = Date.now();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export declare namespace PlaygroundResponse {
time: number;
}

export interface String {
type: "string";
response: ProxyResponse.SerializableBody;
contentType: string;
time: number;
size: string | null;
}

export interface Json {
type: "json";
response: ProxyResponse.SerializableBody;
Expand All @@ -30,4 +38,5 @@ export declare namespace PlaygroundResponse {
export type PlaygroundResponse =
| PlaygroundResponse.Stream
| PlaygroundResponse.Json
| PlaygroundResponse.String
| PlaygroundResponse.File;
3 changes: 3 additions & 0 deletions packages/fern-docs/ui/src/playground/utils/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export const oAuthClientCredentialReferencedEndpointLoginFlow = async ({
setDisplayFailedLogin && setDisplayFailedLogin(true);
}
},
string: () => {
setDisplayFailedLogin && setDisplayFailedLogin(true);
},
file: () => {
setDisplayFailedLogin && setDisplayFailedLogin(true);
},
Expand Down

0 comments on commit b8bb89d

Please sign in to comment.