Skip to content

Commit dc24154

Browse files
committed
refactor: return detailed response as second callback argument (#34)
BREAKING CHANGE: The response body is no longer the 2nd callback argument, the detailed response is. The body is located under the `result` property. The `data` property is removed.
1 parent d47c737 commit dc24154

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

lib/requestwrapper.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,16 @@ export class RequestWrapper {
205205

206206
this.axiosInstance(requestParams)
207207
.then(res => {
208+
// these objects contain circular json structures and are not always relevant to the user
209+
// if the user wants them, they can be accessed through the debug properties
208210
delete res.config;
209211
delete res.request;
212+
210213
// the other sdks use the interface `result` for the body
211214
res.result = res.data;
212-
_callback(null, res.data, res);
215+
delete res.data;
216+
217+
_callback(null, res);
213218
})
214219
.catch(error => {
215220
_callback(this.formatError(error));

migration-guide.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Migration Guide for v1
22

33
## Breaking Changes
4+
5+
### Node Versions
46
Node versions 6 and 8 are no longer supported.
7+
8+
### Callback arguments
9+
The old callback argument structure of `(error, body, response)` has been changed to `(error, response)`. The body is available under the `result` property of the response. The `data` property has been removed in favor of `result`.

test/unit/requestWrapper.test.js

+30-26
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,43 @@ mockAxiosInstance.interceptors = {
1515
axios.default.create.mockReturnValue(mockAxiosInstance);
1616

1717
const { RequestWrapper } = require('../../lib/requestwrapper');
18-
let requestWrapperInstance;
18+
const requestWrapperInstance = new RequestWrapper();
1919

2020
describe('axios', () => {
2121
it('should enable debug', () => {
22-
requestWrapperInstance = new RequestWrapper();
22+
// these should have been called when requestWrapperInstance was instantiated
2323
expect(mockAxiosInstance.interceptors.request.use).toHaveBeenCalledTimes(1);
2424
expect(mockAxiosInstance.interceptors.response.use).toHaveBeenCalledTimes(1);
2525
});
2626
});
2727

2828
describe('sendRequest', () => {
29+
let axiosResolveValue;
30+
let expectedResult;
31+
32+
beforeEach(() => {
33+
// these objects get messed with, so reset them before each test
34+
axiosResolveValue = {
35+
data: 'test',
36+
config: 'test',
37+
request: 'test',
38+
statusText: 'test',
39+
status: 200,
40+
headers: 'test',
41+
};
42+
43+
expectedResult = {
44+
result: 'test',
45+
statusText: 'test',
46+
status: 200,
47+
headers: 'test',
48+
};
49+
});
50+
2951
afterEach(() => {
3052
mockAxiosInstance.mockReset();
3153
});
3254

33-
const axiosResolveValue = {
34-
data: 'test',
35-
config: 'test',
36-
request: 'test',
37-
statusText: 'test',
38-
status: 200,
39-
headers: 'test',
40-
};
41-
42-
const expectedResult = {
43-
data: 'test',
44-
result: 'test',
45-
statusText: 'test',
46-
status: 200,
47-
headers: 'test',
48-
};
49-
5055
it('should send a request with default parameters', done => {
5156
const parameters = {
5257
defaultOptions: {
@@ -65,7 +70,7 @@ describe('sendRequest', () => {
6570

6671
mockAxiosInstance.mockResolvedValue(axiosResolveValue);
6772

68-
requestWrapperInstance.sendRequest(parameters, (err, body, res) => {
73+
requestWrapperInstance.sendRequest(parameters, (err, res) => {
6974
// assert results
7075
expect(mockAxiosInstance.mock.calls[0][0].data).toEqual('post=body');
7176
expect(mockAxiosInstance.mock.calls[0][0].url).toEqual(
@@ -103,10 +108,9 @@ describe('sendRequest', () => {
103108

104109
mockAxiosInstance.mockRejectedValue('error');
105110

106-
requestWrapperInstance.sendRequest(parameters, (err, body, res) => {
111+
requestWrapperInstance.sendRequest(parameters, (err, res) => {
107112
// assert results
108113
expect(err).toEqual(expect.anything());
109-
expect(body).toBeUndefined();
110114
expect(res).toBeUndefined();
111115
done();
112116
});
@@ -150,7 +154,7 @@ describe('sendRequest', () => {
150154
return Promise.resolve(axiosResolveValue);
151155
});
152156

153-
requestWrapperInstance.sendRequest(parameters, (err, body, res) => {
157+
requestWrapperInstance.sendRequest(parameters, (err, res) => {
154158
// assert results
155159
expect(serializedParams).toBe('version=2018-10-15&array_style=a%2Cb');
156160
expect(mockAxiosInstance.mock.calls[0][0].url).toEqual(
@@ -220,7 +224,7 @@ describe('sendRequest', () => {
220224
return Promise.resolve(axiosResolveValue);
221225
});
222226

223-
requestWrapperInstance.sendRequest(parameters, (err, body, res) => {
227+
requestWrapperInstance.sendRequest(parameters, (err, res) => {
224228
// assert results
225229
expect(mockAxiosInstance.mock.calls[0][0].url).toEqual(
226230
'https://example.ibm.com/v1/environments/environment-id/configurations/configuration-id'
@@ -301,7 +305,7 @@ describe('sendRequest', () => {
301305
return Promise.resolve(axiosResolveValue);
302306
});
303307

304-
requestWrapperInstance.sendRequest(parameters, (err, body, res) => {
308+
requestWrapperInstance.sendRequest(parameters, (err, res) => {
305309
// assert results
306310
expect(mockAxiosInstance.mock.calls[0][0].data).toEqual('a=a&b=b');
307311
expect(mockAxiosInstance.mock.calls[0][0].url).toEqual(
@@ -346,7 +350,7 @@ describe('sendRequest', () => {
346350
//
347351
// mockAxiosInstance.mockResolvedValue('res');
348352
//
349-
// requestWrapperInstance.sendRequest(parameters, (err, body, res) => {
353+
// requestWrapperInstance.sendRequest(parameters, (err, res) => {
350354
// // assert results
351355
// expect(mockAxiosInstance.mock.calls[0][0].otherParam).toEqual(500);
352356
// expect(res).toEqual(expectedResult);

0 commit comments

Comments
 (0)