Skip to content

Commit 85c413a

Browse files
author
Anton Konev
committed
fix: pass body to APIError
1 parent bd6194c commit 85c413a

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/callAPIMethod.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default function callAPI(
4747
return fetch(url, accumulatedFetchOptions)
4848
.then( response => response[type || method]()
4949
.catch( () => {
50-
if (!response.ok) throw new APIError(response.status, response.statusText);
50+
if (!response.ok) throw new APIError(response.status, response.statusText, response.body);
5151

5252
return response.body || null;
5353
})
@@ -65,15 +65,14 @@ export default function callAPI(
6565
'type',
6666
'url',
6767
'useFinalURL',
68-
'bodyUsed',
69-
'body'
68+
'bodyUsed'
7069
].reduce((res, key) => {
7170
res[key] = response[key];
7271

7372
return res;
7473
}, {});
7574

76-
return Object.assign({}, writableResponse, { body: result });
75+
return { ...writableResponse, ...{ body: result } };
7776
}
7877

7978
return result;

test/callAPIMethod.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import APIError from '../src/error';
77
const matcher = '*';
88

99
const Response200 = new Response(
10-
JSON.stringify({ sample: 'data'}),
10+
{ sample: 'data'},
1111
{
1212
status: 200
1313
}
@@ -21,7 +21,7 @@ const Response201 = new Response(
2121
);
2222

2323
const Response400 = new Response(
24-
JSON.stringify({ sample: 'not found'}),
24+
{ sample: 'not found'},
2525
{
2626
status: 400
2727
}
@@ -172,13 +172,13 @@ test.serial('returns full response object', async t => {
172172

173173
const APINamespace = 'rest-api';
174174
const namespace = 'user';
175-
const fetchOptions = { method: 'POST'};
175+
const fetchOptions = { method: 'POST' };
176176
const methodOptions = { path: 'path' };
177177
const responseOptions = { fullResponse: true };
178178

179179
const result = await callAPIMethod(APINamespace, namespace, fetchOptions, methodOptions, responseOptions);
180180
const mockedResult = {
181-
body: '{"sample":"data"}',
181+
body: { sample: 'data' },
182182
bodyUsed: true,
183183
headers: (new Response()).headers,
184184
ok: true,

0 commit comments

Comments
 (0)