Skip to content

Commit 28aa074

Browse files
authored
Add custom fetch support as fetch config option (#23)
1 parent 982fd7f commit 28aa074

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
"eslint": "^6.8.0",
3131
"eslint-config-developit": "^1.1.1",
3232
"file-loader": "^5.0.2",
33+
"isomorphic-fetch": "^2.2.1",
3334
"jest": "^24.9.0",
3435
"karmatic": "^1.4.0",
3536
"microbundle": "^0.11.0",
3637
"sinon": "^8.0.4",
3738
"webpack": "^4.41.5"
3839
}
39-
}
40+
}

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ export default (function create(defaults) {
142142
}
143143
}
144144
}
145-
145+
146+
const fetchFunc = options.fetch || fetch;
146147
const customHeaders = {};
147148

148149
if (data && typeof data === 'object') {
@@ -168,7 +169,7 @@ export default (function create(defaults) {
168169
const response = {};
169170
response.config = config;
170171

171-
return fetch(url, {
172+
return fetchFunc(url, {
172173
method: options.method,
173174
body: data,
174175
headers: deepMerge(options.headers, customHeaders, true)

test/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import axios from '../src/index.js';
1616
import textExample from 'file-loader!./fixtures/example.txt';
1717
import jsonExample from 'file-loader!./fixtures/example.json.txt';
18+
import fetch from 'isomorphic-fetch';
1819

1920
describe('redaxios', () => {
2021
it('smoke test', () => {
@@ -73,4 +74,13 @@ describe('redaxios', () => {
7374
window.fetch = oldFetch;
7475
}
7576
});
77+
78+
it('should accept a custom fetch implementation', async () => {
79+
const req = axios.get(jsonExample, { fetch });
80+
expect(req).toBeInstanceOf(Promise);
81+
const res = await req;
82+
expect(res).toBeInstanceOf(Object);
83+
expect(res.status).toEqual(200);
84+
expect(JSON.parse(res.data)).toEqual({ hello: 'world' });
85+
});
7686
});

0 commit comments

Comments
 (0)