Skip to content

Commit

Permalink
Make createFetch more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Apr 25, 2017
1 parent 0b1e1e4 commit 6c1b54c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
45 changes: 30 additions & 15 deletions src/createFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,43 @@
* LICENSE.txt file in the root directory of this source tree.
*/

import isoFetch from 'isomorphic-fetch';
/* @flow */

type FetchOptions = {
import fetch from 'isomorphic-fetch';

type Options = {
baseUrl: string,
cookie?: string,
};

function createFetch({ baseUrl, cookie }: FetchOptions) {
return function fetch(url, options) {
const { headers, ...other } = options || {};
return url.startsWith('/') ? isoFetch(`${baseUrl}${url}`, {
method: 'POST',
/**
* Creates a wrapper function around the HTML5 Fetch API that provides
* default arguments to fetch(...) and is intended to reduce the amount
* of boilerplate code in the application.
* https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch
*/
function createFetch({ baseUrl, cookie }: Options) {
// NOTE: Tweak the default options to suite your application needs
const defaults = {
method: 'POST', // handy with GraphQL backends
mode: baseUrl ? 'cors' : 'same-origin',
credentials: baseUrl ? 'include' : 'same-origin',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(cookie ? { Cookie: cookie } : null),
},
};

return (url, options) => ((url.startsWith('/graphql') || url.startsWith('/api')) ?
fetch(`${baseUrl}${url}`, {
...defaults,
...options,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(typeof cookie === 'undefined' ? null : { cookie }),
...headers,
...defaults.headers,
...(options && options.headers),
},
credentials: 'include',
...other,
}) : isoFetch(url, options);
};
}) : fetch(url, options));
}

export default createFetch;
18 changes: 12 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# yarn lockfile v1


"@types/geojson@^1.0.0":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-1.0.2.tgz#b02d10ab028e2928ac592a051aaa4981a1941d03"

JSONStream@^0.8.4:
version "0.8.4"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-0.8.4.tgz#91657dfe6ff857483066132b4618b62e8f4887bd"
Expand Down Expand Up @@ -1313,8 +1317,8 @@ caniuse-api@^1.5.2:
lodash.uniq "^4.5.0"

caniuse-db@^1.0.30000187, caniuse-db@^1.0.30000529, caniuse-db@^1.0.30000634, caniuse-db@^1.0.30000639:
version "1.0.30000659"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000659.tgz#6775220394d464c8efa4a25cafd2ed9b0c60b11c"
version "1.0.30000660"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000660.tgz#d2d57b309dc5a11bb5b46018f51855f7a41efee5"

caseless@~0.11.0:
version "0.11.0"
Expand Down Expand Up @@ -3685,8 +3689,8 @@ jsonpointer@^4.0.0:
resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.0.1.tgz#4fd92cb34e0e9db3c89c8622ecf51f9b978c6cb9"

jsonwebtoken@^7.3.0:
version "7.3.0"
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-7.3.0.tgz#85118d6a70e3fccdf14389f4e7a1c3f9c8a9fbba"
version "7.4.0"
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-7.4.0.tgz#515bf2bba070ec615bad97fd2e945027eb476946"
dependencies:
joi "^6.10.1"
jws "^3.1.4"
Expand Down Expand Up @@ -6495,8 +6499,10 @@ terraformer-wkt-parser@^1.1.0:
terraformer "~1.0.5"

terraformer@~1.0.5:
version "1.0.7"
resolved "https://registry.yarnpkg.com/terraformer/-/terraformer-1.0.7.tgz#d8a19a56fbf25966ea062d21f515b93589702d69"
version "1.0.8"
resolved "https://registry.yarnpkg.com/terraformer/-/terraformer-1.0.8.tgz#51e0ad89746fcf2161dc6f65aa70e42377c8b593"
dependencies:
"@types/geojson" "^1.0.0"

[email protected]:
version "0.6.4"
Expand Down

0 comments on commit 6c1b54c

Please sign in to comment.