Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/14-204-content-type-error #15

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion __tests__/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,48 @@ describe('apiMiddleware', () => {
expect(response).toEqual(expectedResponse);
});

it('Should dispatch action success when API returns 204', async () => {
const get = () => {};

const apiCallFunction = jest.fn().mockResolvedValue({
headers: {
get,
status: 204,
},
});

const action = {
types: {
request: 'REQUEST',
success: 'SUCCESS',
failure: 'FAILURE',
},
apiCallFunction,
};

const response = await apiMiddleware({ dispatch, getState })(next)(action);

expect(next).not.toBeCalled();
expect(getState).toBeCalled();

const expectedResponse = {
headers: {
get,
status: 204,
},
};
expect(dispatch).toBeCalledWith({
extraData: {},
type: 'REQUEST',
});
expect(dispatch).toBeCalledWith({
extraData: {},
response: expectedResponse,
type: 'SUCCESS',
});
expect(response).toEqual(expectedResponse);
});

it('Should dispatch action failure when has some error on request - no json', async () => {
function get() {
return 'application/x-www-form-urlencoded';
Expand Down Expand Up @@ -237,7 +279,7 @@ describe('apiMiddleware', () => {
}
});

it('Should pass action forwarn if no types are defined', async () => {
it('Should pass action forward if no types are defined', async () => {
const action = {
type: 'REQUEST',
};
Expand Down
8 changes: 6 additions & 2 deletions dist/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ function apiMiddleware(_ref) {
return new Promise(function (resolve, reject) {
apiCallFunction(dispatch).then(function (response) {
// if it's a json response, we unpack and parse it
if (response.headers && typeof response.headers.get === 'function' && response.headers.get('content-type').startsWith('application/json')) {
var contentType = response.headers && typeof response.headers.get === 'function' && response.headers.get('content-type');

if (contentType && contentType.startsWith('application/json')) {
response.json().then(function (data) {
response.data = data; // from backend response

Expand All @@ -83,7 +85,9 @@ function apiMiddleware(_ref) {
}
}).catch(function (error) {
// if it's a json response, we unpack and parse it
if (error.headers && typeof error.headers.get === 'function' && error.headers.get('content-type').startsWith('application/json')) {
var contentType = error.headers && typeof error.headers.get === 'function' && error.headers.get('content-type');

if (contentType && contentType.startsWith('application/json')) {
error.json().then(function (data) {
error.data = data; // form backend error

Expand Down
14 changes: 8 additions & 6 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ export default function apiMiddleware({ dispatch, getState }) {
apiCallFunction(dispatch)
.then(response => {
// if it's a json response, we unpack and parse it
if (
const contentType =
response.headers &&
typeof response.headers.get === 'function' &&
response.headers.get('content-type').startsWith('application/json')
) {
response.headers.get('content-type');

if (contentType && contentType.startsWith('application/json')) {
response.json().then(data => {
response.data = data; // from backend response
dispatch({ extraData, response, type: types.success });
Expand All @@ -64,11 +65,12 @@ export default function apiMiddleware({ dispatch, getState }) {
})
.catch(error => {
// if it's a json response, we unpack and parse it
if (
const contentType =
error.headers &&
typeof error.headers.get === 'function' &&
error.headers.get('content-type').startsWith('application/json')
) {
error.headers.get('content-type');

if (contentType && contentType.startsWith('application/json')) {
error.json().then(data => {
error.data = data; // form backend error
dispatch({ extraData, error, response: error, type: types.failure });
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-redux-api-tools",
"version": "2.1.2",
"version": "2.1.3",
"description": "Middleware and helpers to improve the React-Redux flow when communicating with APIs.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -62,7 +62,7 @@
},
"husky": {
"hooks": {
"pre-commit": "npm run lint && npm run test"
"pre-commit": "npm run lint && npm run dist && npm run test"
}
}
}