Skip to content

Commit a1e0735

Browse files
committed
refactor to remove intermediary function and shave some bytes, add more tests
1 parent 21cca9e commit a1e0735

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/index.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @property {'get'|'post'|'put'|'patch'|'delete'|'options'|'head'|'GET'|'POST'|'PUT'|'PATCH'|'DELETE'|'OPTIONS'|'HEAD'} [method="get"] HTTP method, case-insensitive
1919
* @property {Headers} [headers] Request headers
2020
* @property {FormData|string|object} [body] a body, optionally encoded, to send
21-
* @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream'} [responseType="text"] An encoding to use for the response
21+
* @property {'text'|'json'|'stream'|'blob'|'arrayBuffer'|'formData'|'stream'} [responseType="json"] An encoding to use for the response
2222
* @property {Record<string,any>|URLSearchParams} [params] querystring parameters
2323
* @property {(params: Options['params']) => string} [paramsSerializer] custom function to stringify querystring parameters
2424
* @property {boolean} [withCredentials] Send the request with credentials like cookies
@@ -135,19 +135,6 @@ export default (function create(/** @type {Options} */ defaults) {
135135
return out;
136136
}
137137

138-
/**
139-
* @private
140-
*/
141-
function transformResponse(data) {
142-
if (typeof data === 'string') {
143-
try {
144-
data = JSON.parse(data);
145-
}
146-
catch (e) {}
147-
}
148-
return data;
149-
}
150-
151138
/**
152139
* Issues a request.
153140
* @public
@@ -221,10 +208,15 @@ export default (function create(/** @type {Options} */ defaults) {
221208
response.data = res.body;
222209
return response;
223210
}
224-
return res[options.responseType || 'text']().then((data) => {
225-
response.data = transformResponse(data);
226-
return response;
227-
});
211+
212+
return res[options.responseType || 'text']()
213+
.then((data) => {
214+
response.data = data;
215+
// its okay if this fails: response.data will be the unparsed value:
216+
response.data = JSON.parse(data);
217+
})
218+
.catch(Object)
219+
.then(() => response);
228220
});
229221
}
230222

@@ -234,6 +226,12 @@ export default (function create(/** @type {Options} */ defaults) {
234226
*/
235227
redaxios.CancelToken = /** @type {any} */ (typeof AbortController == 'function' ? AbortController : Object);
236228

229+
/**
230+
* @public
231+
* @type {Options}
232+
*/
233+
redaxios.defaults = defaults;
234+
237235
/**
238236
* @public
239237
*/

test/index.test.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,33 @@ describe('redaxios', () => {
4040
expect(res.data).toEqual('some example content');
4141
});
4242

43-
it('should request JSON', async () => {
44-
const req = axios.get(jsonExample, {
45-
responseType: 'json'
43+
describe('responseType', () => {
44+
it('should default to JSON', async () => {
45+
const res = await axios.get(jsonExample);
46+
expect(res.data).toEqual({ hello: 'world' });
47+
});
48+
49+
it('should force JSON for responseType:json', async () => {
50+
const res = await axios.get(jsonExample, {
51+
responseType: 'json'
52+
});
53+
expect(res.data).toEqual({ hello: 'world' });
54+
});
55+
56+
it('should fall back to null for failed JSON parse', async () => {
57+
const res = await axios.get(textExample, {
58+
responseType: 'json'
59+
});
60+
expect(res.data).toEqual(undefined);
61+
});
62+
63+
it('should still parse JSON when responseType:text', async () => {
64+
// this is just how axios works
65+
const res = await axios.get(jsonExample, {
66+
responseType: 'text'
67+
});
68+
expect(res.data).toEqual({ hello: 'world' });
4669
});
47-
expect(req).toBeInstanceOf(Promise);
48-
const res = await req;
49-
expect(res).toBeInstanceOf(Object);
50-
expect(res.status).toEqual(200);
51-
expect(res.data).toEqual({ hello: 'world' });
5270
});
5371

5472
it('response should be parsed json', async () => {
@@ -227,7 +245,7 @@ describe('redaxios', () => {
227245
const res = await req;
228246
expect(res).toBeInstanceOf(Object);
229247
expect(res.status).toEqual(200);
230-
expect(JSON.parse(res.data)).toEqual({ hello: 'world' });
248+
expect(res.data).toEqual({ hello: 'world' });
231249
});
232250

233251
it('should support params and paramsSerializer options', async () => {

0 commit comments

Comments
 (0)