diff --git a/src/fetch.js b/src/fetch.js new file mode 100644 index 0000000..14b0275 --- /dev/null +++ b/src/fetch.js @@ -0,0 +1,106 @@ +/** + * Action types + */ + +const FETCH = 'EFFECT_FETCH' + +/** + * Fetch middleware + */ + +function fetchMiddleware ({dispatch, getState}) { + return next => action => + action.type === FETCH + ? g().fetch(action.payload.url, action.payload.params) + .then(checkStatus) + .then(createResponse, createErrorResponse) + : next(action) +} + +/** + * g - Return the global object (in the browser or node) + */ + +function g () { + return typeof window === 'undefined' + ? global + : window +} + +/** + * Create a plain JS response object. Note that 'headers' is still a Headers + * object (https://developer.mozilla.org/en-US/docs/Web/API/Headers), and must be + * read using that API. + */ + +function createResponse (res) { + return deserialize(res).then(value => ({ + url: res.url, + status: res.status, + statusText: res.statusText, + headers: res.headers, + value: value + }), err => { + throw { + value: err + } + }) +} + +/** + * Create the response, then return a new rejected + * promise so the failure chain stays failed. + */ + +function createErrorResponse (res) { + const q = res.headers + ? createResponse(res) + : Promise.resolve(res) + + return q.then(function (res) { throw res }) +} + +/** + * Deserialize the request body + */ + +function deserialize (res) { + const header = res.headers.get('Content-Type') || '' + if (header.indexOf('application/json') > -1) return res.json() + if (header.indexOf('application/ld+json') > -1) return res.json() + if (header.indexOf('application/octet-stream') > -1) return res.arrayBuffer() + return res.text() +} + +/** + * Check the status and reject the promise if it's not in the 200 range + */ + +function checkStatus (res) { + if (res.status >= 200 && res.status < 300) { + return res + } else { + throw res + } +} + +/** + * Action creator + */ + +function fetchActionCreator (url = '', params = {}) { + return { + type: FETCH, + payload: { + url, + params + } + } +} + +export { + fetchMiddleware, + fetchActionCreator, + FETCH +} + diff --git a/src/fetchEncodeJSON.js b/src/fetchEncodeJSON.js index dedcda0..6813cdb 100644 --- a/src/fetchEncodeJSON.js +++ b/src/fetchEncodeJSON.js @@ -2,7 +2,7 @@ * Imports */ -import {FETCH} from '.' +import {FETCH} from './fetch' /** * @see https://github.com/lodash/lodash/blob/4.8.0/lodash.js#L10705 diff --git a/src/index.js b/src/index.js index 68d5c07..b2859c8 100644 --- a/src/index.js +++ b/src/index.js @@ -4,106 +4,7 @@ import 'isomorphic-fetch' import fetchEncodeJSON from './fetchEncodeJSON' - -/** - * Action types - */ - -const FETCH = 'EFFECT_FETCH' - -/** - * Fetch middleware - */ - -function fetchMiddleware ({dispatch, getState}) { - return next => action => - action.type === FETCH - ? g().fetch(action.payload.url, action.payload.params) - .then(checkStatus) - .then(createResponse, createErrorResponse) - : next(action) -} - -/** - * g - Return the global object (in the browser or node) - */ - -function g () { - return typeof window === 'undefined' - ? global - : window -} - -/** - * Create a plain JS response object. Note that 'headers' is still a Headers - * object (https://developer.mozilla.org/en-US/docs/Web/API/Headers), and must be - * read using that API. - */ - -function createResponse (res) { - return deserialize(res).then(value => ({ - url: res.url, - status: res.status, - statusText: res.statusText, - headers: res.headers, - value: value - }), err => { - throw { - value: err - } - }) -} - -/** - * Create the response, then return a new rejected - * promise so the failure chain stays failed. - */ - -function createErrorResponse (res) { - const q = res.headers - ? createResponse(res) - : Promise.resolve(res) - - return q.then(function (res) { throw res }) -} - -/** - * Deserialize the request body - */ - -function deserialize (res) { - const header = res.headers.get('Content-Type') || '' - if (header.indexOf('application/json') > -1) return res.json() - if (header.indexOf('application/ld+json') > -1) return res.json() - if (header.indexOf('application/octet-stream') > -1) return res.arrayBuffer() - return res.text() -} - -/** - * Check the status and reject the promise if it's not in the 200 range - */ - -function checkStatus (res) { - if (res.status >= 200 && res.status < 300) { - return res - } else { - throw res - } -} - -/** - * Action creator - */ - -function fetchActionCreator (url = '', params = {}) { - return { - type: FETCH, - payload: { - url, - params - } - } -} +import { fetchMiddleware, fetchActionCreator, FETCH } from './fetch' /** * Exports