createHttpInterceptor
function createHttpInterceptor(): HttpInterceptor;Defined in: intercept-http.ts:232
Create an HTTP interceptor that patches fetch and XMLHttpRequest
to capture requests/responses based on configurable rules. Each transport is
patched only if the runtime has it: on a server (no XMLHttpRequest) only
fetch is patched, and restore() puts back exactly what was replaced.
Returns
Section titled “Returns”An HttpInterceptor with addRule, clearAll, and restore methods.
Example
Section titled “Example”const interceptor = createHttpInterceptor();
const handle = interceptor.addRule({ path: "/api/users", onIntercept: (e) => console.log(e.status, e.response.body),});
// With custom matchinginterceptor.addRule({ match: (url) => url.includes("/api/") && !url.includes("/health"), method: "POST", onIntercept: (e) => analytics.track("api_call", e),});
// Listen via EventTargethandle.events.addEventListener("intercept", (e) => { console.log((e as CustomEvent<InterceptEvent>).detail);});
// Cleanuphandle.stop(); // remove one ruleinterceptor.clearAll(); // remove all rulesinterceptor.restore(); // remove all rules + restore original fetch/XHR