Skip to content

Commit 094cbda

Browse files
author
Alexander Vakhitov
committed
fix: resolved conflicts
2 parents 19a4b6f + 0d946d1 commit 094cbda

File tree

4 files changed

+87
-47
lines changed

4 files changed

+87
-47
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,20 @@ Query-like object.
6767

6868
Method to be called on `fetch`'s response.
6969

70+
##### headers {Object|Headers}
71+
72+
Additional headers to be sent to the server
73+
74+
##### body {string|FormData}
75+
76+
Requests's body
77+
78+
7079
Alternatively you can use provided shortcuts for [every HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
7180

7281
**Example:**
7382
```js
74-
import { GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH } from '../src/shortcuts';
83+
import { GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH } from 'unity-api';
7584

7685
const userResource = {
7786
namespace: 'user',
@@ -88,13 +97,11 @@ const userResource = {
8897
formData.append('firstname', firstname);
8998
formData.append('lastname', lastname);
9099

91-
return {
100+
return POST({
92101
path: [id, 'edit'],
93-
options: {
94-
method: 'POST',
95-
body: formData
96-
}
97-
};
102+
headers: { 'x-csrf-token': 'blah' }
103+
body: formData
104+
});
98105
},
99106

100107
// DELETE: /api/user/1

src/callAPIMethod.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,33 @@ export default function callAPI(
2020
methodOptions = {}
2121
) {
2222

23-
const { path=[], query={}, options={}, method='json' } = methodOptions;
23+
const { path=[], query={}, options={}, method='json', headers, body } = methodOptions;
2424

2525
const url = formatURL(APINamespace, namespace, path, query);
2626

27-
return fetch(url, {...defaults.fetchOptions, ...fetchOptions, ...options})
28-
.then( result => result[method]()
27+
const accumulatedFetchOptions = {
28+
...defaults.fetchOptions,
29+
...fetchOptions,
30+
...options
31+
};
32+
if (headers) accumulatedFetchOptions.headers = headers;
33+
if (body) accumulatedFetchOptions.body = body;
34+
35+
return fetch(url, accumulatedFetchOptions)
36+
.then( response => response[method]()
2937
.catch( () => {
30-
if (!result.ok) {
31-
throw new APIError(result.status, result.statusText);
38+
if (!response.ok) {
39+
throw new APIError(response.status, response.statusText);
3240
}
3341

34-
return result.body || null;
42+
return response.body || null;
3543
})
36-
.then( body => {
37-
if (!result.ok) {
38-
throw new APIError(result.status, result.statusText, body);
44+
.then( result => {
45+
if (!response.ok) {
46+
throw new APIError(response.status, response.statusText, result);
3947
}
4048

41-
return body;
49+
return result;
4250
}))
4351
.catch( error => error);
4452
}

test/callAPIMethod.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ test.serial('fetch options', async t => {
9494

9595
const APINamespace = 'rest-api';
9696
const namespace = 'user';
97-
const fetchOptions = { method: 'POST'};
97+
const fetchOptions = { method: 'GET'};
9898
const methodOptions = {
9999
path: 'path',
100100
query: { edit: true },
101101
options: {
102-
credentials: 'omit'
102+
credentials: 'omit',
103+
method: 'POST'
103104
},
104105
method: 'text'
105106
};
@@ -118,6 +119,22 @@ test.serial('fetch options', async t => {
118119
}, 'correct options');
119120
t.true(spyResponse200.calledOnce);
120121

122+
const newMethodOptions = {
123+
...methodOptions,
124+
headers: new Headers(),
125+
body: 'body'
126+
};
127+
await callAPIMethod(APINamespace, fetchOptions, namespace, newMethodOptions);
128+
129+
t.deepEqual(fetchMock.lastOptions(matcher), {
130+
cache: 'default',
131+
credentials: 'omit',
132+
method: 'POST',
133+
mode: 'cors',
134+
headers: newMethodOptions.headers,
135+
body: newMethodOptions.body
136+
}, 'correct options');
137+
121138
spyResponse200.restore();
122139
fetchMock.restore();
123140
});

yarn.lock

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -800,15 +800,7 @@ babel-plugin-transform-strict-mode@^6.22.0:
800800
babel-runtime "^6.22.0"
801801
babel-types "^6.22.0"
802802

803-
babel-polyfill@^6.22.0:
804-
version "6.22.0"
805-
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.22.0.tgz#1ac99ebdcc6ba4db1e2618c387b2084a82154a3b"
806-
dependencies:
807-
babel-runtime "^6.22.0"
808-
core-js "^2.4.0"
809-
regenerator-runtime "^0.10.0"
810-
811-
babel-polyfill@^6.23.0:
803+
babel-polyfill@^6.22.0, babel-polyfill@^6.23.0:
812804
version "6.23.0"
813805
resolved "http://storage.mds.yandex.net/get-npm/45674/babel-polyfill-6.23.0.tgz#8364ca62df8eafb830499f699177466c3b03499d"
814806
dependencies:
@@ -1579,7 +1571,7 @@ detect-indent@4.0.0, detect-indent@^4.0.0:
15791571
dependencies:
15801572
repeating "^2.0.0"
15811573

1582-
diff@^3.0.0, diff@^3.0.1:
1574+
diff@^3.0.0, diff@^3.0.1, diff@^3.1.0:
15831575
version "3.2.0"
15841576
resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9"
15851577

@@ -2012,11 +2004,11 @@ form-data@~2.1.1:
20122004
combined-stream "^1.0.5"
20132005
mime-types "^2.1.12"
20142006

2015-
formatio@1.1.1:
2016-
version "1.1.1"
2017-
resolved "https://registry.yarnpkg.com/formatio/-/formatio-1.1.1.tgz#5ed3ccd636551097383465d996199100e86161e9"
2007+
formatio@1.2.0:
2008+
version "1.2.0"
2009+
resolved "https://registry.yarnpkg.com/formatio/-/formatio-1.2.0.tgz#f3b2167d9068c4698a8d51f4f760a39a54d818eb"
20182010
dependencies:
2019-
samsam "~1.1"
2011+
samsam "1.x"
20202012

20212013
fs-exists-sync@^0.1.0:
20222014
version "0.1.0"
@@ -2953,9 +2945,9 @@ log-driver@1.2.5:
29532945
version "1.2.5"
29542946
resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.5.tgz#7ae4ec257302fd790d557cb10c97100d857b0056"
29552947

2956-
lolex@1.3.2:
2957-
version "1.3.2"
2958-
resolved "https://registry.yarnpkg.com/lolex/-/lolex-1.3.2.tgz#7c3da62ffcb30f0f5a80a2566ca24e45d8a01f31"
2948+
lolex@^1.6.0:
2949+
version "1.6.0"
2950+
resolved "https://registry.yarnpkg.com/lolex/-/lolex-1.6.0.tgz#3a9a0283452a47d7439e72731b9e07d7386e49f6"
29592951

29602952
longest@^1.0.1:
29612953
version "1.0.1"
@@ -3125,6 +3117,10 @@ nan@^2.3.0:
31253117
version "2.5.1"
31263118
resolved "https://registry.yarnpkg.com/nan/-/nan-2.5.1.tgz#d5b01691253326a97a2bbee9e61c55d8d60351e2"
31273119

3120+
native-promise-only@^0.8.1:
3121+
version "0.8.1"
3122+
resolved "https://registry.yarnpkg.com/native-promise-only/-/native-promise-only-0.8.1.tgz#20a318c30cb45f71fe7adfbf7b21c99c1472ef11"
3123+
31283124
natural-compare@^1.4.0:
31293125
version "1.4.0"
31303126
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@@ -3874,9 +3870,9 @@ rx@^4.1.0:
38743870
version "4.1.0"
38753871
resolved "https://registry.yarnpkg.com/rx/-/rx-4.1.0.tgz#a5f13ff79ef3b740fe30aa803fb09f98805d4782"
38763872

3877-
samsam@1.1.2, samsam@~1.1:
3878-
version "1.1.2"
3879-
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.1.2.tgz#bec11fdc83a9fda063401210e40176c3024d1567"
3873+
samsam@1.x, samsam@^1.1.3:
3874+
version "1.2.1"
3875+
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.2.1.tgz#edd39093a3184370cb859243b2bdf255e7d8ea67"
38803876

38813877
semver-diff@^2.0.0:
38823878
version "2.1.0"
@@ -3916,14 +3912,18 @@ signal-exit@^3.0.0, signal-exit@^3.0.1, signal-exit@^3.0.2:
39163912
version "3.0.2"
39173913
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
39183914

3919-
sinon@^1.17.6:
3920-
version "1.17.7"
3921-
resolved "https://registry.yarnpkg.com/sinon/-/sinon-1.17.7.tgz#4542a4f49ba0c45c05eb2e9dd9d203e2b8efe0bf"
3915+
sinon@^2.0.0:
3916+
version "2.0.0"
3917+
resolved "https://registry.yarnpkg.com/sinon/-/sinon-2.0.0.tgz#48337fa489069e530ad7e2f44f07b0ae93d37b24"
39223918
dependencies:
3923-
formatio "1.1.1"
3924-
lolex "1.3.2"
3925-
samsam "1.1.2"
3926-
util ">=0.10.3 <1"
3919+
diff "^3.1.0"
3920+
formatio "1.2.0"
3921+
lolex "^1.6.0"
3922+
native-promise-only "^0.8.1"
3923+
path-to-regexp "^1.7.0"
3924+
samsam "^1.1.3"
3925+
text-encoding "0.6.4"
3926+
type-detect "^4.0.0"
39273927

39283928
slash@^1.0.0:
39293929
version "1.0.0"
@@ -4166,6 +4166,10 @@ test-exclude@^3.3.0:
41664166
read-pkg-up "^1.0.1"
41674167
require-main-filename "^1.0.1"
41684168

4169+
text-encoding@0.6.4:
4170+
version "0.6.4"
4171+
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19"
4172+
41694173
text-extensions@^1.0.0:
41704174
version "1.4.0"
41714175
resolved "https://registry.yarnpkg.com/text-extensions/-/text-extensions-1.4.0.tgz#c385d2e80879fe6ef97893e1709d88d9453726e9"
@@ -4240,6 +4244,10 @@ type-check@~0.3.2:
42404244
dependencies:
42414245
prelude-ls "~1.1.2"
42424246

4247+
type-detect@^4.0.0:
4248+
version "4.0.0"
4249+
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.0.tgz#62053883542a321f2f7b25746dc696478b18ff6b"
4250+
42434251
typedarray@^0.0.6:
42444252
version "0.0.6"
42454253
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
@@ -4323,7 +4331,7 @@ util-deprecate@~1.0.1:
43234331
version "1.0.2"
43244332
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
43254333

4326-
"util@>=0.10.3 <1", util@^0.10.3:
4334+
util@^0.10.3:
43274335
version "0.10.3"
43284336
resolved "https://registry.yarnpkg.com/util/-/util-0.10.3.tgz#7afb1afe50805246489e3db7fe0ed379336ac0f9"
43294337
dependencies:

0 commit comments

Comments
 (0)