Skip to content

Commit 15c3065

Browse files
ya-woodcutterTimeRaider
authored andcommitted
fix(callAPIMethod): bug ff & safari (#26)
1 parent 0d946d1 commit 15c3065

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

src/callAPIMethod.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@ export default function callAPI(
3333
if (body) accumulatedFetchOptions.body = body;
3434

3535
return fetch(url, accumulatedFetchOptions)
36-
.then( response => {
37-
const responseBodyPromise = response.body ? response[method]() : Promise.resolve(response.body);
38-
39-
if (response.ok) {
40-
return responseBodyPromise;
41-
}
42-
43-
return responseBodyPromise
44-
.then( responseBody => {
45-
throw new APIError(response.status, response.statusText, responseBody);
46-
});
47-
})
36+
.then( response => response[method]()
37+
.catch( () => {
38+
if (!response.ok) throw new APIError(response.status, response.statusText);
39+
40+
return response.body || null;
41+
})
42+
.then( result => {
43+
if (!response.ok) throw new APIError(response.status, response.statusText, result);
44+
45+
return result;
46+
}))
4847
.catch( error => error);
4948
}

test/callAPIMethod.spec.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@ import APIError from '../src/error';
66

77
const matcher = '*';
88

9-
const ResponseGood = new Response(
9+
const Response200 = new Response(
1010
JSON.stringify({ sample: 'data'}),
1111
{
1212
status: 200
1313
}
1414
);
1515

16-
const ResponseBad = new Response(
16+
const Response201 = new Response(
17+
null,
18+
{
19+
status: 201
20+
}
21+
);
22+
23+
const Response400 = new Response(
24+
JSON.stringify({ sample: 'not found'}),
25+
{
26+
status: 400
27+
}
28+
);
29+
30+
const Response500 = new Response(
1731
null,
1832
{
1933
status: 500
@@ -36,16 +50,37 @@ test.serial('default params', async t => {
3650
});
3751

3852
test.serial('200 reponse', async t => {
39-
fetchMock.get(matcher, ResponseGood);
53+
fetchMock.get(matcher, Response200);
4054
const result = await callAPIMethod();
4155

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

4458
fetchMock.restore();
4559
});
4660

61+
test.serial('201 reponse', async t => {
62+
fetchMock.get(matcher, Response201);
63+
const result = await callAPIMethod();
64+
65+
t.is(result, null, 'incorrect response body');
66+
67+
fetchMock.restore();
68+
});
69+
70+
test.serial('400 reponse', async t => {
71+
fetchMock.get(matcher, Response400);
72+
73+
const result = await callAPIMethod();
74+
75+
t.true(result instanceof Error, 'instance of Error');
76+
t.true(result instanceof APIError, 'instance of APIError');
77+
t.deepEqual(result.body, { sample: 'not found'}, 'correct error body');
78+
79+
fetchMock.restore();
80+
});
81+
4782
test.serial('500 reponse', async t => {
48-
fetchMock.get(matcher, ResponseBad);
83+
fetchMock.get(matcher, Response500);
4984

5085
const result = await callAPIMethod();
5186

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

5893
test.serial('fetch options', async t => {
59-
fetchMock.post(matcher, ResponseGood);
94+
fetchMock.post(matcher, Response200);
6095

6196
const APINamespace = 'rest-api';
6297
const namespace = 'user';
@@ -70,7 +105,7 @@ test.serial('fetch options', async t => {
70105
},
71106
method: 'text'
72107
};
73-
const spyResponseGood = sinon.spy(ResponseGood, 'text');
108+
const spyResponse200 = sinon.spy(Response200, 'text');
74109

75110
fetchMock.post(matcher, {});
76111

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

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

104-
spyResponseGood.restore();
139+
spyResponse200.restore();
105140
fetchMock.restore();
106141
});
107142

108143
test.serial('unsupported Response method', async t => {
109-
fetchMock.get(matcher, ResponseGood);
144+
fetchMock.get(matcher, Response200);
110145

111146
const APINamespace = 'rest-api';
112147
const namespace = 'user';

0 commit comments

Comments
 (0)