/** * MSW-like fetch mocking utilities for testing API clients. * * This module provides an API similar to MSW (Mock Service Worker) but uses * Vitest's vi.stubGlobal() under the hood - no additional dependencies required. * * TODO: Consider migrating to MSW (https://mswjs.io/) when the team is comfortable * adding the dependency. The API is intentionally similar to make migration easy: * * Before (this module): * import { http, HttpResponse, setupServer } from "./utils/mock-fetch"; * * After (MSW): * import { http, HttpResponse } from "msw"; * import { setupServer } from "msw/node"; * * @see https://mswjs.io/docs/getting-started */ export interface RequestInfo { url: string; method: string; headers: Headers; params: Record; request: Request; } export type RequestHandler = (info: RequestInfo) => Response | Promise; export interface HttpHandler { method: string; path: string | RegExp; handler: RequestHandler; } export declare const HttpResponse: { /** * Creates a JSON response. * * @example * HttpResponse.json({ id: "123", name: "Test" }) * HttpResponse.json({ error: "Not found" }, { status: 404 }) */ json(body: unknown, init?: ResponseInit): Response; /** * Creates a text response. */ text(body: string, init?: ResponseInit): Response; /** * Creates an error response. */ error(): Response; }; /** * Options for creating a paginated endpoint handler. */ export interface PaginatedHandlerOptions { /** The items to paginate through */ items: T[]; /** Default page size if not specified in request (default: 25) */ defaultPageSize?: number; /** Optional callback invoked on each request (useful for tracking request count) */ onRequest?: () => void; } /** * Creates a handler that returns paginated responses from an array of items. * * Reads `page` and `pageSize` from query parameters and returns the appropriate * slice of items with correct pagination metadata. * * @example * ```ts * const items = [{ id: "1" }, { id: "2" }, { id: "3" }]; * let requestCount = 0; * * server.use( * http.get("/items", createPaginatedHandler({ * items, * defaultPageSize: 2, * onRequest: () => requestCount++, * })) * ); * ``` */ export declare function createPaginatedHandler(options: PaginatedHandlerOptions): RequestHandler; export declare const http: { /** * Creates a GET request handler. * * @example * http.get("/opportunities/:id", ({ params }) => { * return HttpResponse.json({ id: params.id }); * }) */ get(path: string | RegExp, handler: RequestHandler): HttpHandler; /** * Creates a POST request handler. */ post(path: string | RegExp, handler: RequestHandler): HttpHandler; /** * Creates a PUT request handler. */ put(path: string | RegExp, handler: RequestHandler): HttpHandler; /** * Creates a PATCH request handler. */ patch(path: string | RegExp, handler: RequestHandler): HttpHandler; /** * Creates a DELETE request handler. */ delete(path: string | RegExp, handler: RequestHandler): HttpHandler; /** * Creates a handler that matches any HTTP method. */ all(path: string | RegExp, handler: RequestHandler): HttpHandler; }; /** * Creates a mock server with the given request handlers. * API is similar to MSW's setupServer for easy future migration. * * @example * const server = setupServer( * http.get("/opportunities/:id", ({ params }) => { * return HttpResponse.json({ id: params.id, title: "Test Grant" }); * }), * http.get("/opportunities", () => { * return HttpResponse.json({ data: [] }); * }) * ); * * beforeAll(() => server.listen()); * afterEach(() => server.resetHandlers()); * afterAll(() => server.close()); */ export declare function setupServer(...handlers: HttpHandler[]): { /** * Starts intercepting fetch requests. */ listen(): void; /** * Stops intercepting fetch requests and restores the original fetch. */ close(): void; /** * Resets handlers to the initial handlers passed to setupServer. */ resetHandlers(): void; /** * Adds runtime request handlers (prepended to give them priority). * Useful for overriding handlers in specific tests. * * @example * it("handles server errors", async () => { * server.use( * http.get("/opportunities/:id", () => { * return HttpResponse.json({ error: "Server error" }, { status: 500 }); * }) * ); * // This test will now get 500 errors * }); */ use(...runtimeHandlers: HttpHandler[]): void; }; //# sourceMappingURL=mock-fetch.d.ts.map