Skip to content
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
23 changes: 11 additions & 12 deletions src/callAPIMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ export default function callAPI(
if (body) accumulatedFetchOptions.body = body;

return fetch(url, accumulatedFetchOptions)
.then( response => {
const responseBodyPromise = response.body ? response[method]() : Promise.resolve(response.body);

if (response.ok) {
return responseBodyPromise;
}

return responseBodyPromise
.then( responseBody => {
throw new APIError(response.status, response.statusText, responseBody);
});
})
.then( response => response[method]()
.catch( () => {
if (!response.ok) throw new APIError(response.status, response.statusText);

return response.body || null;
})
.then( result => {
if (!response.ok) throw new APIError(response.status, response.statusText, result);

return result;
}))
.catch( error => error);
}
53 changes: 44 additions & 9 deletions test/callAPIMethod.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ import APIError from '../src/error';

const matcher = '*';

const ResponseGood = new Response(
const Response200 = new Response(
JSON.stringify({ sample: 'data'}),
{
status: 200
}
);

const ResponseBad = new Response(
const Response201 = new Response(
null,
{
status: 201
}
);

const Response400 = new Response(
JSON.stringify({ sample: 'not found'}),
{
status: 400
}
);

const Response500 = new Response(
null,
{
status: 500
Expand All @@ -36,16 +50,37 @@ test.serial('default params', async t => {
});

test.serial('200 reponse', async t => {
fetchMock.get(matcher, ResponseGood);
fetchMock.get(matcher, Response200);
const result = await callAPIMethod();

t.deepEqual(result, { sample: 'data' }, 'correct response body');

fetchMock.restore();
});

test.serial('201 reponse', async t => {
fetchMock.get(matcher, Response201);
const result = await callAPIMethod();

t.is(result, null, 'incorrect response body');

fetchMock.restore();
});

test.serial('400 reponse', async t => {
fetchMock.get(matcher, Response400);

const result = await callAPIMethod();

t.true(result instanceof Error, 'instance of Error');
t.true(result instanceof APIError, 'instance of APIError');
t.deepEqual(result.body, { sample: 'not found'}, 'correct error body');

fetchMock.restore();
});

test.serial('500 reponse', async t => {
fetchMock.get(matcher, ResponseBad);
fetchMock.get(matcher, Response500);

const result = await callAPIMethod();

Expand All @@ -56,7 +91,7 @@ test.serial('500 reponse', async t => {
});

test.serial('fetch options', async t => {
fetchMock.post(matcher, ResponseGood);
fetchMock.post(matcher, Response200);

const APINamespace = 'rest-api';
const namespace = 'user';
Expand All @@ -70,7 +105,7 @@ test.serial('fetch options', async t => {
},
method: 'text'
};
const spyResponseGood = sinon.spy(ResponseGood, 'text');
const spyResponse200 = sinon.spy(Response200, 'text');

fetchMock.post(matcher, {});

Expand All @@ -83,7 +118,7 @@ test.serial('fetch options', async t => {
method: 'POST',
mode: 'cors'
}, 'correct options');
t.true(spyResponseGood.calledOnce);
t.true(spyResponse200.calledOnce);

const newMethodOptions = {
...methodOptions,
Expand All @@ -101,12 +136,12 @@ test.serial('fetch options', async t => {
body: newMethodOptions.body
}, 'correct options');

spyResponseGood.restore();
spyResponse200.restore();
fetchMock.restore();
});

test.serial('unsupported Response method', async t => {
fetchMock.get(matcher, ResponseGood);
fetchMock.get(matcher, Response200);

const APINamespace = 'rest-api';
const namespace = 'user';
Expand Down