Skip to content

Commit b8bb89d

Browse files
fern-supporteyw520dsinghvi
authored
fix: fallback to string when response is not JSONable. (#2097)
Co-authored-by: eden <[email protected]> Co-authored-by: Deep Singhvi <[email protected]>
1 parent b6d4891 commit b8bb89d

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

packages/fern-docs/ui/src/playground/endpoint/PlaygroundEndpoint.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ export const PlaygroundEndpoint = ({
144144
break;
145145
}
146146
result += decoder.decode(value);
147-
console.log(result);
148147
setResponse(
149148
loaded({
150149
type: "stream",

packages/fern-docs/ui/src/playground/fetch-utils/executeProxyRest.ts

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,62 @@ export async function executeProxyRest(
4848
res.headers.get("Content-Type")?.toLowerCase()?.includes("application/json")
4949
) {
5050
const startTime = Date.now();
51-
const json = await res.json();
52-
const endTime = Date.now();
51+
try {
52+
const text = await res.text();
53+
const endTime = Date.now();
5354

54-
const fallbackTime =
55-
Number(res.headers.get("X-Fern-Proxy-Origin-Latency") ?? 0) +
56-
endTime -
57-
startTime;
55+
const fallbackTime =
56+
Number(res.headers.get("X-Fern-Proxy-Origin-Latency") ?? 0) +
57+
endTime -
58+
startTime;
5859

59-
return {
60-
type: "json",
61-
response: {
62-
headers: responseHeaders,
63-
ok: res.ok,
64-
redirected: res.redirected,
65-
status: res.status,
66-
statusText: res.statusText,
67-
type: res.type,
68-
url: res.url,
69-
body: json,
70-
},
71-
contentType: res.headers.get("Content-Type") ?? "application/json",
72-
time: Number(
73-
res.headers.get("X-Fern-Proxy-Response-Time") ?? fallbackTime
74-
),
75-
size:
76-
res.headers.get("Content-Length") ??
77-
String(new TextEncoder().encode(JSON.stringify(json)).length),
78-
};
60+
try {
61+
const json = JSON.parse(text);
62+
return {
63+
type: "json",
64+
response: {
65+
headers: responseHeaders,
66+
ok: res.ok,
67+
redirected: res.redirected,
68+
status: res.status,
69+
statusText: res.statusText,
70+
type: res.type,
71+
url: res.url,
72+
body: json,
73+
},
74+
contentType: res.headers.get("Content-Type") ?? "application/json",
75+
time: Number(
76+
res.headers.get("X-Fern-Proxy-Response-Time") ?? fallbackTime
77+
),
78+
size:
79+
res.headers.get("Content-Length") ??
80+
String(new TextEncoder().encode(text).length),
81+
};
82+
} catch {
83+
return {
84+
type: "string",
85+
response: {
86+
headers: responseHeaders,
87+
ok: res.ok,
88+
redirected: res.redirected,
89+
status: res.status,
90+
statusText: res.statusText,
91+
type: res.type,
92+
url: res.url,
93+
body: text,
94+
},
95+
contentType: res.headers.get("Content-Type") ?? "text/plain",
96+
time: Number(
97+
res.headers.get("X-Fern-Proxy-Response-Time") ?? fallbackTime
98+
),
99+
size:
100+
res.headers.get("Content-Length") ??
101+
String(new TextEncoder().encode(text).length),
102+
};
103+
}
104+
} catch () {
105+
throw new Error("Failed to read response body");
106+
}
79107
}
80108

81109
const startTime = Date.now();

packages/fern-docs/ui/src/playground/types/playgroundResponse.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ export declare namespace PlaygroundResponse {
1010
time: number;
1111
}
1212

13+
export interface String {
14+
type: "string";
15+
response: ProxyResponse.SerializableBody;
16+
contentType: string;
17+
time: number;
18+
size: string | null;
19+
}
20+
1321
export interface Json {
1422
type: "json";
1523
response: ProxyResponse.SerializableBody;
@@ -30,4 +38,5 @@ export declare namespace PlaygroundResponse {
3038
export type PlaygroundResponse =
3139
| PlaygroundResponse.Stream
3240
| PlaygroundResponse.Json
41+
| PlaygroundResponse.String
3342
| PlaygroundResponse.File;

packages/fern-docs/ui/src/playground/utils/oauth.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ export const oAuthClientCredentialReferencedEndpointLoginFlow = async ({
8888
setDisplayFailedLogin && setDisplayFailedLogin(true);
8989
}
9090
},
91+
string: () => {
92+
setDisplayFailedLogin && setDisplayFailedLogin(true);
93+
},
9194
file: () => {
9295
setDisplayFailedLogin && setDisplayFailedLogin(true);
9396
},

0 commit comments

Comments
 (0)