From f96f212153b55015619d0d481afb29bfc3eaf0d6 Mon Sep 17 00:00:00 2001 From: Sergio Moreno Date: Fri, 16 Apr 2021 20:22:08 +0200 Subject: [PATCH 1/2] fix: patch requests are case-sensitive --- src/index.js | 14 +++++++------- test/index.test.js | 26 ++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index c266c73..acd9067 100644 --- a/src/index.js +++ b/src/index.js @@ -76,25 +76,25 @@ export default (function create(/** @type {Options} */ defaults) { redaxios.request = redaxios; /** @public @type {BodylessMethod} */ - redaxios.get = (url, config) => redaxios(url, config, 'get'); + redaxios.get = (url, config) => redaxios(url, config, 'GET'); /** @public @type {BodylessMethod} */ - redaxios.delete = (url, config) => redaxios(url, config, 'delete'); + redaxios.delete = (url, config) => redaxios(url, config, 'DELETE'); /** @public @type {BodylessMethod} */ - redaxios.head = (url, config) => redaxios(url, config, 'head'); + redaxios.head = (url, config) => redaxios(url, config, 'HEAD'); /** @public @type {BodylessMethod} */ - redaxios.options = (url, config) => redaxios(url, config, 'options'); + redaxios.options = (url, config) => redaxios(url, config, 'OPTIONS'); /** @public @type {BodyMethod} */ - redaxios.post = (url, data, config) => redaxios(url, config, 'post', data); + redaxios.post = (url, data, config) => redaxios(url, config, 'POST', data); /** @public @type {BodyMethod} */ - redaxios.put = (url, data, config) => redaxios(url, config, 'put', data); + redaxios.put = (url, data, config) => redaxios(url, config, 'POST', data); /** @public @type {BodyMethod} */ - redaxios.patch = (url, data, config) => redaxios(url, config, 'patch', data); + redaxios.patch = (url, data, config) => redaxios(url, config, 'PATCH', data); /** @public */ redaxios.all = Promise.all.bind(Promise); diff --git a/test/index.test.js b/test/index.test.js index cab5374..c2160fe 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -87,7 +87,7 @@ describe('redaxios', () => { expect(window.fetch).toHaveBeenCalledWith( 'http://foo/bar', jasmine.objectContaining({ - method: 'get', + method: 'GET', headers: {}, body: undefined }) @@ -112,7 +112,7 @@ describe('redaxios', () => { expect(window.fetch).toHaveBeenCalledWith( '/foo/bar', jasmine.objectContaining({ - method: 'get', + method: 'GET', headers: {}, body: undefined }) @@ -203,7 +203,25 @@ describe('redaxios', () => { expect(fetchMock).toHaveBeenCalledWith( '/foo', jasmine.objectContaining({ - method: 'post', + method: 'POST', + headers: { + 'content-type': 'application/json' + }, + body: '{"hello":"world"}' + }) + ); + expect(res.status).toEqual(200); + expect(res.data).toEqual('yep'); + }); + + it('should issue PATCH requests (with JSON body)', async () => { + const res = await axios.patch('/foo', { + hello: 'world' + }); + expect(fetchMock).toHaveBeenCalledWith( + '/foo', + jasmine.objectContaining({ + method: 'PATCH', headers: { 'content-type': 'application/json' }, @@ -235,7 +253,7 @@ describe('redaxios', () => { expect(fetchMock).toHaveBeenCalledWith( '/foo', jasmine.objectContaining({ - method: 'post', + method: 'POST', headers: { 'content-type': 'multipart/form-data' }, From c66487d54d6e1e8a58c1d462f3b835f5e1667ccd Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 29 Apr 2022 11:42:36 -0400 Subject: [PATCH 2/2] fall back to get --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 5de6fe7..a2c2efb 100644 --- a/src/index.js +++ b/src/index.js @@ -196,7 +196,7 @@ export default (function create(/** @type {Options} */ defaults) { const fetchFunc = options.fetch || fetch; return fetchFunc(url, { - method: (_method || options.method).toUpperCase(), + method: (_method || options.method || 'get').toUpperCase(), body: data, headers: deepMerge(options.headers, customHeaders, true), credentials: options.withCredentials ? 'include' : 'same-origin'