From 07c5156d91f6133bd8d767a608a6e7620cd65708 Mon Sep 17 00:00:00 2001 From: Kyle Welch Date: Wed, 26 Dec 2018 22:48:50 -0600 Subject: [PATCH] fix(request): set GET as default request method Defaults request method to "GET" to avoid setting the "Content-Type" to json for requests where method is omitted. PR: #50 Resolves: #49 --- src/__tests__/request.spec.ts | 13 +++++++++++++ src/request.ts | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/__tests__/request.spec.ts b/src/__tests__/request.spec.ts index 4ddfca8..a078f22 100644 --- a/src/__tests__/request.spec.ts +++ b/src/__tests__/request.spec.ts @@ -58,6 +58,19 @@ describe('request', () => { ); }); + it('calls should not send "application/json", if method is not passed', async () => { + await request(TEST_URL); + + expect(getMockFetch()).toHaveBeenCalledTimes(1); + expect(getMockFetch()).toHaveBeenCalledWith( + TEST_URL, + expect.objectContaining({ + credentials: 'same-origin', + headers: {}, + }) + ); + }); + it('calls fetch and respects overrides in options', async () => { await request(TEST_URL, {credentials: 'omit'}); diff --git a/src/request.ts b/src/request.ts index 4141a4b..4b908be 100644 --- a/src/request.ts +++ b/src/request.ts @@ -37,7 +37,7 @@ const _tryParseJSON = (res: Response): Promise => { */ export const _fetchJSON = ( url: string, - {headers, method, mode, ...options}: RequestInit = {} + {headers = {}, method = 'GET', mode, ...options}: RequestInit = {} ): Promise<{}> => { let fetchHeaders = headers as HeadersInit;