import { CreateResponse , ISerializedRequest , ISerializedResponse , OnSerializedRequest , } from "unmock-core/dist/interfaces" ; import buildFetch from "./fetch" ; export { buildFetch } ; export type Listener = ( req : ISerializedRequest , respond : ( res : ISerializedResponse ) => void ) => void ; export type Fetch = ( url : RequestInfo , init ? : RequestInit ) => Promise < Response > ; declare namespace global { let fetch ; } declare namespace window { let fetch ; } let fetchInterceptor ; /**
 * Fill global `fetch` object with mock fetch.
 */ export class FetchInterceptor { public readonly fetch : Fetch ; private originalFetch ? : { where : any ; fetch : any ; } ; constructor ( onSerializedRequest ) { this . fetch = buildFetch ( onSerializedRequest ) ; if ( typeof global !== "undefined" ) { this . originalFetch = { where : global , fetch : global . fetch , } ; global . fetch = this . fetch ; } else if ( typeof window !== "undefined" ) { this . originalFetch = { where : window , fetch : window . fetch } ; window . fetch = this . fetch ; } } public disable ( ) { if ( this . originalFetch ) { this . originalFetch . where . fetch = this . originalFetch . fetch ; this . originalFetch = undefined ; } } } export default { param onSerializedRequest on ( onSerializedRequest ) { this . off ( ) ; fetchInterceptor = new FetchInterceptor ( onSerializedRequest ) ; return fetchInterceptor ; } , /**
     * Stop intercepting `fetch`, restore original `fetch`.
     */ off ( ) { if ( fetchInterceptor ) { fetchInterceptor . disable ( ) ; fetchInterceptor = undefined ; } } , } ;