diff --git a/package.json b/package.json index 5c43988..1d2885f 100644 --- a/package.json +++ b/package.json @@ -30,10 +30,11 @@ "eslint": "^6.8.0", "eslint-config-developit": "^1.1.1", "file-loader": "^5.0.2", + "isomorphic-fetch": "^2.2.1", "jest": "^24.9.0", "karmatic": "^1.4.0", "microbundle": "^0.11.0", "sinon": "^8.0.4", "webpack": "^4.41.5" } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index c35b632..f4c86f2 100644 --- a/src/index.js +++ b/src/index.js @@ -142,7 +142,8 @@ export default (function create(defaults) { } } } - + + const fetchFunc = options.fetch || fetch; const customHeaders = {}; if (data && typeof data === 'object') { @@ -168,7 +169,7 @@ export default (function create(defaults) { const response = {}; response.config = config; - return fetch(url, { + return fetchFunc(url, { method: options.method, body: data, headers: deepMerge(options.headers, customHeaders, true) diff --git a/test/index.test.js b/test/index.test.js index 7173c93..8327ba5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -15,6 +15,7 @@ import axios from '../src/index.js'; import textExample from 'file-loader!./fixtures/example.txt'; import jsonExample from 'file-loader!./fixtures/example.json.txt'; +import fetch from 'isomorphic-fetch'; describe('redaxios', () => { it('smoke test', () => { @@ -73,4 +74,13 @@ describe('redaxios', () => { window.fetch = oldFetch; } }); + + it('should accept a custom fetch implementation', async () => { + const req = axios.get(jsonExample, { fetch }); + expect(req).toBeInstanceOf(Promise); + const res = await req; + expect(res).toBeInstanceOf(Object); + expect(res.status).toEqual(200); + expect(JSON.parse(res.data)).toEqual({ hello: 'world' }); + }); });