Skip to content

Commit 7365a02

Browse files
Alexander Vakhitovya-woodcutter
authored andcommitted
perf(fetch): add content body, when throw Error
1. Throw content with Error, check result.body on null or undefined
1 parent 9aa1a4e commit 7365a02

File tree

7 files changed

+31
-14
lines changed

7 files changed

+31
-14
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ jspm_packages
4242

4343
.npmrc
4444
dist
45-
umd
45+
umd
46+
.idea
47+
.DS_Store

src/callAPIMethod.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ export default function callAPI(
2626

2727
return fetch(url, {...defaults.fetchOptions, ...fetchOptions, ...options})
2828
.then( result => {
29+
const promiseCallback = result.body ? result[method]() : new Promise(resolve => resolve(result.body));
2930

30-
if (!result.ok) {
31-
throw new APIError(result.status, result.statusText);
31+
if (result.ok) {
32+
return promiseCallback;
3233
}
3334

34-
return result[method]();
35+
return promiseCallback
36+
.then( body => {
37+
throw new APIError(result.status, result.statusText, body);
38+
});
3539
})
3640
.catch( error => error);
3741
}

src/createAPI.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import applyMiddleware from './applyMiddleware';
55
const createAPI = (
66
resources = {},
77
middleware = [],
8-
APINamespace = '',
9-
fetchOptions = {}
8+
APINamespace,
9+
fetchOptions
1010
) => Object.keys(resources).reduce( (api, resourceId) => {
1111
api[resourceId] = Object.keys(resources[resourceId].methods)
1212
.reduce( (resource, method) => {

src/error.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
function APIError(code, message) {
1+
function APIError(code, message, body) {
22
this.code = code;
33
this.message = (message || '');
4+
this.body = body;
45
}
56

67
Object.setPrototypeOf(APIError.prototype, Error.prototype);

test/callAPIMethod.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ const ResponseGood = new Response(
1313
}
1414
);
1515

16-
const ResponseBad = new Response(null, {
17-
status: 500
18-
});
16+
const ResponseBad = new Response(
17+
null,
18+
{
19+
status: 500
20+
}
21+
);
1922

2023
test.serial('default params', async t => {
2124
fetchMock.get(matcher, {});
@@ -41,7 +44,6 @@ test.serial('200 reponse', async t => {
4144
fetchMock.restore();
4245
});
4346

44-
4547
test.serial('500 reponse', async t => {
4648
fetchMock.get(matcher, ResponseBad);
4749

@@ -79,7 +81,7 @@ test.serial('fetch options', async t => {
7981
credentials: 'omit',
8082
method: 'POST',
8183
mode: 'cors'
82-
}), 'correct options';
84+
}, 'correct options');
8385
t.true(spyResponseGood.calledOnce);
8486

8587
spyResponseGood.restore();

test/createAPI.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test('supports resource prefix insted of namespace', t => {
3333
}
3434
};
3535
const resources = { user };
36-
const API = createAPI(resources, [], 'https://example.com');
36+
const API = createAPI(resources, [], 'https://example.com', {});
3737

3838
API.user.get(1);
3939

@@ -65,7 +65,7 @@ test('general behaviour', t => {
6565

6666
const middleware = [ _ => _, _ => _];
6767

68-
const API = createAPI(resources, middleware, 'https://example.com');
68+
const API = createAPI(resources, middleware, 'https://example.com', {});
6969

7070
t.deepEqual(Object.keys(API), ['user', 'project'], 'resources');
7171

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,14 @@ babel-polyfill@^6.22.0:
808808
core-js "^2.4.0"
809809
regenerator-runtime "^0.10.0"
810810

811+
babel-polyfill@^6.23.0:
812+
version "6.23.0"
813+
resolved "http://storage.mds.yandex.net/get-npm/45674/babel-polyfill-6.23.0.tgz#8364ca62df8eafb830499f699177466c3b03499d"
814+
dependencies:
815+
babel-runtime "^6.22.0"
816+
core-js "^2.4.0"
817+
regenerator-runtime "^0.10.0"
818+
811819
babel-preset-es2015@^6.16.0:
812820
version "6.22.0"
813821
resolved "https://registry.yarnpkg.com/babel-preset-es2015/-/babel-preset-es2015-6.22.0.tgz#af5a98ecb35eb8af764ad8a5a05eb36dc4386835"

0 commit comments

Comments
 (0)