Skip to content

Instantly share code, notes, and snippets.

@katai5plate
Last active July 2, 2019 06:18
Show Gist options
  • Save katai5plate/2ecd5ca55789041e17ba116b5de35bac to your computer and use it in GitHub Desktop.
Save katai5plate/2ecd5ca55789041e17ba116b5de35bac to your computer and use it in GitHub Desktop.
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