|
4 | 4 |
|
5 | 5 | import 'isomorphic-fetch'
|
6 | 6 | import fetchEncodeJSON from './fetchEncodeJSON'
|
7 |
| - |
8 |
| -/** |
9 |
| - * Action types |
10 |
| - */ |
11 |
| - |
12 |
| -const FETCH = 'EFFECT_FETCH' |
13 |
| - |
14 |
| -/** |
15 |
| - * Fetch middleware |
16 |
| - */ |
17 |
| - |
18 |
| -function fetchMiddleware ({dispatch, getState}) { |
19 |
| - return next => action => |
20 |
| - action.type === FETCH |
21 |
| - ? g().fetch(action.payload.url, action.payload.params) |
22 |
| - .then(checkStatus) |
23 |
| - .then(createResponse, createErrorResponse) |
24 |
| - : next(action) |
25 |
| -} |
26 |
| - |
27 |
| -/** |
28 |
| - * g - Return the global object (in the browser or node) |
29 |
| - */ |
30 |
| - |
31 |
| -function g () { |
32 |
| - return typeof window === 'undefined' |
33 |
| - ? global |
34 |
| - : window |
35 |
| -} |
36 |
| - |
37 |
| -/** |
38 |
| - * Create a plain JS response object. Note that 'headers' is still a Headers |
39 |
| - * object (https://developer.mozilla.org/en-US/docs/Web/API/Headers), and must be |
40 |
| - * read using that API. |
41 |
| - */ |
42 |
| - |
43 |
| -function createResponse (res) { |
44 |
| - return deserialize(res).then(value => ({ |
45 |
| - url: res.url, |
46 |
| - status: res.status, |
47 |
| - statusText: res.statusText, |
48 |
| - headers: res.headers, |
49 |
| - value: value |
50 |
| - }), err => { |
51 |
| - throw { |
52 |
| - value: err |
53 |
| - } |
54 |
| - }) |
55 |
| -} |
56 |
| - |
57 |
| -/** |
58 |
| - * Create the response, then return a new rejected |
59 |
| - * promise so the failure chain stays failed. |
60 |
| - */ |
61 |
| - |
62 |
| -function createErrorResponse (res) { |
63 |
| - const q = res.headers |
64 |
| - ? createResponse(res) |
65 |
| - : Promise.resolve(res) |
66 |
| - |
67 |
| - return q.then(function (res) { throw res }) |
68 |
| -} |
69 |
| - |
70 |
| -/** |
71 |
| - * Deserialize the request body |
72 |
| - */ |
73 |
| - |
74 |
| -function deserialize (res) { |
75 |
| - const header = res.headers.get('Content-Type') || '' |
76 |
| - if (header.indexOf('application/json') > -1) return res.json() |
77 |
| - if (header.indexOf('application/ld+json') > -1) return res.json() |
78 |
| - if (header.indexOf('application/octet-stream') > -1) return res.arrayBuffer() |
79 |
| - return res.text() |
80 |
| -} |
81 |
| - |
82 |
| -/** |
83 |
| - * Check the status and reject the promise if it's not in the 200 range |
84 |
| - */ |
85 |
| - |
86 |
| -function checkStatus (res) { |
87 |
| - if (res.status >= 200 && res.status < 300) { |
88 |
| - return res |
89 |
| - } else { |
90 |
| - throw res |
91 |
| - } |
92 |
| -} |
93 |
| - |
94 |
| -/** |
95 |
| - * Action creator |
96 |
| - */ |
97 |
| - |
98 |
| -function fetchActionCreator (url = '', params = {}) { |
99 |
| - return { |
100 |
| - type: FETCH, |
101 |
| - payload: { |
102 |
| - url, |
103 |
| - params |
104 |
| - } |
105 |
| - } |
106 |
| -} |
| 7 | +import { fetchMiddleware, fetchActionCreator, FETCH } from './fetch' |
107 | 8 |
|
108 | 9 | /**
|
109 | 10 | * Exports
|
|
0 commit comments