Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
feat(core): allow responseType to be defined by the responseNormalizer
Browse files Browse the repository at this point in the history
feat(json-api): add json as responseType for the responseNormalizer
  • Loading branch information
Bart van der Hoek committed Sep 17, 2019
1 parent d35f8ec commit 6e72ab5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
35 changes: 35 additions & 0 deletions packages/core/__tests__/Connector/HttpConnector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,39 @@ describe('The axios instance configuration', () => {
test('that the axios instance contains the globally set configuration', () => {
expect(connector.axios.defaults.baseURL).toEqual('/test');
});

test('that the axios instance response type configuration defaults to json response type', () => {
expect(connector.axios.defaults.responseType).toEqual('json');
});
});

describe('The axios instance response type by responseNormalizer', () => {
const urlSerializer = jest.fn();
const paramsSerializer = jest.fn();
const requestSerializer = jest.fn();
const responseNormalizer = jest.fn();

responseNormalizer.responseType = 'xml';

mockAxios.defaults = {
baseURL: '/test',
paramsSerializer: null,
transformResponse: [],
transformRequest: [],
};
mockAxios.create = jest.fn(() => cloneDeep(mockAxios));

const connector = HttpConnector.create(
mockAxios,
{
urlSerializer,
paramsSerializer,
requestSerializer,
responseNormalizer,
},
);

test('that the axios instance has the xml response type', () => {
expect(connector.axios.defaults.responseType).toEqual('xml');
});
});
2 changes: 1 addition & 1 deletion packages/core/src/Connector/HttpConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function HttpConnector(
) {
const axiosInstance = axios.create ? axios.create() : axios;

axiosInstance.defaults.responseType = 'json';
axiosInstance.defaults.responseType = responseNormalizer.responseType || 'json';
axiosInstance.defaults.paramsSerializer = paramsSerializer;

return {
Expand Down
6 changes: 5 additions & 1 deletion packages/json-api/src/Response/responseNormalizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import normalizePaging from './Resource/normalizePaging';
*
* @returns {{data: Resource[]|Resource, paging: {count: number, pages: number}}|{data: Resource}}
*/
export default function responseNormalizer(response) {
function responseNormalizer(response) {
if (response.errors) {
return response;
}
Expand Down Expand Up @@ -65,3 +65,7 @@ export default function responseNormalizer(response) {
paging: normalizePaging(response),
};
}

responseNormalizer.responseType = 'json';

export default responseNormalizer;

0 comments on commit 6e72ab5

Please sign in to comment.