Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 20x 20x 20x 18x 18x 18x 16x 2x 1x 1x 20x 1x 20x 1x | /* global fetch */
import 'whatwg-fetch'
import cloneDeep from 'lodash/cloneDeep'
import defaults from 'lodash/defaultsDeep'
import fork from '@r14c/async-utils/fork'
import isFunction from 'lodash/isFunction'
import pick from 'lodash/pick'
const withDefaults = (options) => pick(defaults({}, options, {
credentials: 'same-origin'
}), ['headers', 'body', 'method', 'credentials', 'signal'])
let interceptors = []
let onError
let fetchWrapper = (url, options = {}) => {
return fork(cloneDeep(options), interceptors).then((request) => {
return fetch(url, withDefaults(request))
.then((response) => {
if (response.status >= 200 && response.status < 400) {
return response
} else if (isFunction(onError)) {
onError(response, request)
} else {
throw new Error(response.statusText, {response, request})
}
})
})
}
fetchWrapper.addInterceptor = (fn) => {
interceptors.push(fn)
}
fetchWrapper.onError = (fn) => {
onError = fn
}
export default fetchWrapper
|