Last active
July 2, 2019 06:18
-
-
Save katai5plate/2ecd5ca55789041e17ba116b5de35bac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type AnyObj = { [key: string]: any }; | |
const M = ( | |
globalWindow: AnyObj, | |
headerParams: { [key: string]: string }, | |
transferList: { url: string, response: AnyObj }[] | |
) => { | |
const cacheFetch = globalWindow.fetch; | |
delete globalWindow.fetch; | |
const headers = new Headers(); | |
Object.entries(headerParams).forEach(([k, v]) => headers.append(k, v)) | |
globalWindow.fetch = (calledUrl: string, calledOpt: AnyObj) => { | |
for (let { url, response } of transferList) { | |
if (calledUrl === url) { | |
return Promise.resolve({ | |
headers, status: 200, ok: true, | |
json: async () => Promise.resolve(response), | |
}); | |
} | |
} | |
return cacheFetch(calledUrl, calledOpt); | |
}; | |
}; | |
export default M; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment