Skip to content

Instantly share code, notes, and snippets.

@MRezaSafari
Created August 11, 2023 22:56
Show Gist options
  • Save MRezaSafari/22d2b467522845fbbbdfaed1a9303fce to your computer and use it in GitHub Desktop.
Save MRezaSafari/22d2b467522845fbbbdfaed1a9303fce to your computer and use it in GitHub Desktop.
fetch interceptor
export const interceptor = async () => {
const { fetch: originalFetch } = window;
window.fetch = async (...args) => {
let [resource, config] = args;
const URLcutOff = "https://myapi.com";
const trailing = resource.slice(URLcutOff.length);
console.log(trailing);
if (trailing.startsWith("/posts")) {
resource = `https://myapi.com`;
}
const response = await originalFetch(resource, config);
return response;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment