UNPKG

nock

Version:

HTTP server mocking and expectations library for Node.js

45 lines (44 loc) • 2.47 kB
import type { ReadStream } from 'node:fs'; import type { Scope, Options } from './scope.ts'; export type DataMatcher = boolean | number | string | null | undefined | RegExp | DataMatcherArray | DataMatcherMap; export type DataMatcherArray = ReadonlyArray<DataMatcher>; export type DataMatcherMap = { [key: string]: DataMatcher; }; export type RequestBodyMatcher = string | Buffer | RegExp | DataMatcherArray | DataMatcherMap | ((body: any) => boolean); export type RequestHeaderMatcher = string | RegExp | ((fieldValue: string | null) => boolean); export type Body = string | Record<string, any>; export type ReplyBody = Body | Buffer | ReadStream; export type ReplyHeaderFunction = (req: Request, body?: string | Buffer) => string | string[] | Promise<string | string[]>; export type ReplyHeaderValue = string | string[] | ReplyHeaderFunction; export type ReplyHeaders = Record<string, ReplyHeaderValue> | Map<string, ReplyHeaderValue> | ReplyHeaderValue[]; export type StatusCode = number; export type ReplyFnResult = readonly [StatusCode] | readonly [StatusCode, ReplyBody] | readonly [StatusCode, ReplyBody, ReplyHeaders]; import { URLSearchParams } from 'node:url'; declare class Interceptor { constructor(scope: Scope, uri: string | RegExp | ((path: string) => boolean), method: string, requestBody?: RequestBodyMatcher, interceptorOptions?: Options); optionally(flag?: boolean): this; passthrough(): Scope; replyWithError(errorMessage: string | Error | Record<string, any>): Scope; reply(statusCode: any, body?: any, rawHeaders?: ReplyHeaders): Scope; replyWithFile(statusCode: number, filePath: string, headers?: ReplyHeaders): Scope; reqheaderMatches(expected: RequestHeaderMatcher, actual: string | null, key: string): boolean; match(request: Request, body: string): string[]; matchOrigin(request: Request): boolean; matchHostName(hostname: string): boolean; matchQuery(options: Record<string, any>): any; filteringPath(...args: any[]): this; markConsumed(): void; matchHeader(name: string, value: RequestHeaderMatcher): this; basicAuth({ user, pass }: { user: string; pass?: string; }): this; query(queries: boolean | string | URLSearchParams | Record<string, any> | ((queryObject: Record<string, any>) => boolean)): this; times(newCounter: number): this; once(): this; twice(): this; thrice(): this; delay(ms: number): this; } export { Interceptor };