UNPKG

@juit/lib-fetch-mock

Version:
56 lines (55 loc) 3.08 kB
type FetchFunction = typeof globalThis.fetch; type FetchArguments = Parameters<FetchFunction>; type FetchHandlerResult = Response | number | void | null | undefined; type FetchHandler = (request: Request, fetch: FetchFunction) => FetchHandlerResult | Promise<FetchHandlerResult>; export interface FetchMock { /** Handle requests a specific method and path with the specified handler callback */ on(method: string, path: string | RegExp, handler: FetchHandler): this; /** Handle all requests with the specified handler callback */ handle(handler: FetchHandler): this; /** Intercept mocked requests with an interceptor */ intercept(): () => Promise<DeferredRequest>; /** Reset all handlers and interceptors for this instance */ reset(): this; /** Install this instance as the global `fetch` mock handler */ install(): this; /** Ensure that the mocked `fetch` is restored to its original value */ destroy(): void; } export interface FetchMockConstructor { /** * Create a new {@link FetchMock} instance, optionally resolving relative * request URLs against the specified _base url_. */ new (baseurl?: string | URL | undefined): FetchMock; } export declare const FetchMock: FetchMockConstructor; /** * A `DeferredRequest` is an utility class deferring the `Response` sent for * an _intercepted_ `Request`. */ export interface DeferredRequest extends Readonly<Request> { /** Fail the `Request` with an optional `Error` */ readonly fail: (error?: Error) => void; /** Respond to this intercepted request with a _real_ `fetch` request */ readonly fetch: (...args: FetchArguments) => void; /** Respond to the `Request` with an optional `Response` (defaults to an empty 200 `Response`) */ readonly send: (response?: Response | PromiseLike<Response>) => void; /** Respond to the `Request` with the specified status and an empty body */ readonly sendStatus: (status: number) => void; /** Respond to the `Request` with the specified JSON body and an optional status */ readonly sendJson: (json: any, status?: number) => void; /** Respond to the `Request` with the specified text body and an optional status */ readonly sendText: (text: string, status?: number) => void; /** Respond to the `Request` with the specified binary body and an optional status */ readonly sendData: (data: Uint8Array, status?: number) => void; } /** Create a `Response` with the specified status and an empty body */ export declare function sendStatus(status: number, statusText?: string | undefined): Response; /** Create a `Response` with the specified text body and an optional status */ export declare function sendText(text: string, status?: number): Response; /** Create a `Response` with the specified JSON body and an optional status */ export declare function sendJson(data: unknown, status?: number): Response; /** Create a `Response` with the specified binary body and an optional status */ export declare function sendData(data: Uint8Array, status?: number): Response; export {};