Skip to content

Commit a3f3bdd

Browse files
Fix no response API calls (#969)
## Description Fixes #968 This PR fixes cases where our APIs do not return a response. ## Documentation Does this require changes to the WorkOS Docs? E.g. the [API Reference](https://workos.com/docs/reference) or code snippets need updates. ``` [ ] Yes ``` If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.
1 parent a1d9ff0 commit a3f3bdd

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/common/utils/fetch-client.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ export class FetchClient {
88
options: { params?: Record<string, any>; headers?: HeadersInit },
99
) {
1010
const resourceURL = this.getResourceURL(path, options.params);
11-
const response = await this.fetch(resourceURL, {
11+
return await this.fetch(resourceURL, {
1212
headers: options.headers,
1313
});
14-
return { data: await response.json() };
1514
}
1615

1716
async post<Entity = any>(
@@ -20,12 +19,11 @@ export class FetchClient {
2019
options: { params?: Record<string, any>; headers?: HeadersInit },
2120
) {
2221
const resourceURL = this.getResourceURL(path, options.params);
23-
const response = await this.fetch(resourceURL, {
22+
return await this.fetch(resourceURL, {
2423
method: 'POST',
2524
headers: { ...getContentTypeHeader(entity), ...options.headers },
2625
body: getBody(entity),
2726
});
28-
return { data: await response.json() };
2927
}
3028

3129
async put<Entity = any>(
@@ -34,20 +32,19 @@ export class FetchClient {
3432
options: { params?: Record<string, any>; headers?: HeadersInit },
3533
) {
3634
const resourceURL = this.getResourceURL(path, options.params);
37-
const response = await this.fetch(resourceURL, {
35+
return await this.fetch(resourceURL, {
3836
method: 'PUT',
3937
headers: { ...getContentTypeHeader(entity), ...options.headers },
4038
body: getBody(entity),
4139
});
42-
return { data: await response.json() };
4340
}
4441

4542
async delete(
4643
path: string,
4744
options: { params?: Record<string, any>; headers?: HeadersInit },
4845
) {
4946
const resourceURL = this.getResourceURL(path, options.params);
50-
await this.fetch(resourceURL, {
47+
return await this.fetch(resourceURL, {
5148
method: 'DELETE',
5249
headers: options.headers,
5350
});
@@ -85,7 +82,14 @@ export class FetchClient {
8582
});
8683
}
8784

88-
return response;
85+
const contentType = response.headers.get('content-type');
86+
const isJsonResponse = contentType?.includes('application/json');
87+
88+
if (isJsonResponse) {
89+
return { data: await response.json() };
90+
}
91+
92+
return { data: null };
8993
}
9094
}
9195

src/common/utils/test-utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import fetch, { MockParams } from 'jest-fetch-mock';
22

33
export function fetchOnce(
44
response: any = {},
5-
{ status = 200, ...rest }: MockParams = {},
5+
{ status = 200, headers, ...rest }: MockParams = {},
66
) {
7-
return fetch.once(JSON.stringify(response), { status, ...rest });
7+
return fetch.once(JSON.stringify(response), {
8+
status,
9+
headers: { 'content-type': 'application/json;charset=UTF-8', ...headers },
10+
...rest,
11+
});
812
}
913

1014
export function fetchURL() {

0 commit comments

Comments
 (0)