Skip to content

Commit 19a4b6f

Browse files
author
Alexander Vakhitov
committed
fix: review
1 parent a03047b commit 19a4b6f

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/callAPIMethod.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ export default function callAPI(
2525
const url = formatURL(APINamespace, namespace, path, query);
2626

2727
return fetch(url, {...defaults.fetchOptions, ...fetchOptions, ...options})
28-
.then( result => {
29-
const promiseCallback = result[method]();
30-
31-
if (result.ok) {
32-
return promiseCallback;
33-
}
34-
35-
return promiseCallback
28+
.then( result => result[method]()
3629
.catch( () => {
37-
throw new APIError(result.status, result.statusText);
30+
if (!result.ok) {
31+
throw new APIError(result.status, result.statusText);
32+
}
33+
34+
return result.body || null;
3835
})
3936
.then( body => {
40-
throw new APIError(result.status, result.statusText, body);
41-
});
42-
})
37+
if (!result.ok) {
38+
throw new APIError(result.status, result.statusText, body);
39+
}
40+
41+
return body;
42+
}))
4343
.catch( error => error);
4444
}

test/callAPIMethod.spec.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ 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 Response201 = new Response(
17+
null,
18+
{
19+
status: 201
20+
}
21+
);
22+
1623
const Response400 = new Response(
1724
JSON.stringify({ sample: 'not found'}),
1825
{
@@ -43,14 +50,23 @@ test.serial('default params', async t => {
4350
});
4451

4552
test.serial('200 reponse', async t => {
46-
fetchMock.get(matcher, ResponseGood);
53+
fetchMock.get(matcher, Response200);
4754
const result = await callAPIMethod();
4855

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

5158
fetchMock.restore();
5259
});
5360

61+
test.serial('201 reponse', async t => {
62+
fetchMock.get(matcher, Response201);
63+
const result = await callAPIMethod();
64+
65+
t.is(result, null, 'correct response body');
66+
67+
fetchMock.restore();
68+
});
69+
5470
test.serial('400 reponse', async t => {
5571
fetchMock.get(matcher, Response400);
5672

@@ -74,7 +90,7 @@ test.serial('500 reponse', async t => {
7490
});
7591

7692
test.serial('fetch options', async t => {
77-
fetchMock.post(matcher, ResponseGood);
93+
fetchMock.post(matcher, Response200);
7894

7995
const APINamespace = 'rest-api';
8096
const namespace = 'user';
@@ -87,7 +103,7 @@ test.serial('fetch options', async t => {
87103
},
88104
method: 'text'
89105
};
90-
const spyResponseGood = sinon.spy(ResponseGood, 'text');
106+
const spyResponse200 = sinon.spy(Response200, 'text');
91107

92108
fetchMock.post(matcher, {});
93109

@@ -100,14 +116,14 @@ test.serial('fetch options', async t => {
100116
method: 'POST',
101117
mode: 'cors'
102118
}, 'correct options');
103-
t.true(spyResponseGood.calledOnce);
119+
t.true(spyResponse200.calledOnce);
104120

105-
spyResponseGood.restore();
121+
spyResponse200.restore();
106122
fetchMock.restore();
107123
});
108124

109125
test.serial('unsupported Response method', async t => {
110-
fetchMock.get(matcher, ResponseGood);
126+
fetchMock.get(matcher, Response200);
111127

112128
const APINamespace = 'rest-api';
113129
const namespace = 'user';

0 commit comments

Comments
 (0)