Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
fix: should handle empty content (HTTP 204) (#23)
Browse files Browse the repository at this point in the history
* fix: should handle empty content (HTTP 204)

* feat: re-implement by checking Content-Type

* update test
  • Loading branch information
KotoriK committed Apr 30, 2023
1 parent f878cd3 commit 4ce3828
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,22 @@ describe('delete()', () => {
params: { path: { post_id: '123' } },
});

// assert correct URL was called
expect(fetchMocker.mock.calls[0][0]).toBe('/post/123');
// assert correct data was returned
expect(data).toEqual({});

// assert error is empty
expect(error).toBe(undefined);
});

it('returns empty object on Content-Length: 0', async () => {
const client = createClient<paths>();
fetchMocker.mockResponseOnce(() => ({ headers: { 'Content-Length': 0 }, status: 200, body: '' }));
const { data, error, response } = await client.del('/post/{post_id}', {
params: { path: { post_id: '123' } },
});

// assert correct data was returned
expect(data).toEqual({});
expect(response.status).toBe(204);

// assert error is empty
expect(error).toBe(undefined);
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default function createClient<T>(options?: ClientOptions) {
});

async function coreFetch<U extends keyof T, M extends keyof T[U]>(url: U, fetchOptions: FetchOptions<T[U][M]>): Promise<FetchResponse<T[U][M]>> {
let { headers, body, params = {}, querySerializer = (q: any) => new URLSearchParams(q).toString(), ...init } = fetchOptions || {};
let { headers, body: requestBody, params = {}, querySerializer = (q: any) => new URLSearchParams(q).toString(), ...init } = fetchOptions || {};

// URL
let finalURL = `${options?.baseUrl ?? ''}${url as string}`;
Expand All @@ -123,19 +123,23 @@ export default function createClient<T>(options?: ClientOptions) {
...options,
...init,
headers: baseHeaders,
body: typeof body === 'string' ? body : JSON.stringify(body),
body: typeof requestBody === 'string' ? requestBody : JSON.stringify(requestBody),
});
const respHeaders = res.headers;
const response: TruncatedResponse = {
bodyUsed: res.bodyUsed,
headers: res.headers,
headers: respHeaders,
ok: res.ok,
redirected: res.redirected,
status: res.status,
statusText: res.statusText,
type: res.type,
url: res.url,
};
return res.ok ? { data: res.status === 204 ? {} : await res.json(), response } : { error: await res.json(), response };

const contentLength = res.headers.get('Content-Length');
const bodyParsed = res.status !== 204 && contentLength !== '0' ? await res.json() : {};
return res.ok ? { data: bodyParsed, response } : { error: bodyParsed, response };
}

return {
Expand Down

0 comments on commit 4ce3828

Please sign in to comment.