Skip to content

Commit ee11cbf

Browse files
author
Johan Björklund
committed
Remove circular dependency
1 parent 0756cea commit ee11cbf

File tree

3 files changed

+108
-101
lines changed

3 files changed

+108
-101
lines changed

src/fetch.js

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

src/fetchEncodeJSON.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Imports
33
*/
44

5-
import {FETCH} from '.'
5+
import {FETCH} from './fetch'
66

77
/**
88
* @see https://github.com/lodash/lodash/blob/4.8.0/lodash.js#L10705

src/index.js

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,7 @@
44

55
import 'isomorphic-fetch'
66
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'
1078

1089
/**
10910
* Exports

0 commit comments

Comments
 (0)