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 41 42 43 44 45 46 47 48 | 1x 3x 6x 6x 6x 1x 6x 3x 3x 4x 3x 3x 3x 4x 2x 3x | class FetchInterceptor {
constructor() {
this.interceptors = new Map()
window.fetch = (fetch => (...args) => this.interceptor(fetch, ...args))(window.fetch)
}
register(interceptFunctions) {
const uid = new Date().getTime().toString(36) + performance.now()
this.interceptors.set(uid, interceptFunctions)
return { unregister: this.unregister.bind(this, uid) }
}
unregister(uid) {
this.interceptors.delete(uid)
}
clear() {
this.interceptors = new Map()
}
interceptor(fetch, ...args) {
let promise = Promise.resolve(args)
/* Register request interceptors */
this.interceptors.forEach(({ request, requestError }) => {
if (request || requestError) {
promise = promise.then(args => request(...args), requestError)
}
})
/* Register fetch call */
promise = promise.then(args => fetch(...args))
/* Register response interceptors */
this.interceptors.forEach(({ response, responseError }) => {
if (response || responseError) {
promise = promise.then(response, responseError)
}
})
return promise
}
}
export default new FetchInterceptor()
|