/// import { BodyDefinition, HeaderDefinition, HeadersDefinition, LineInfo, PathDefinition, QueryDefinition, QueryParameterDefinition, RequestInfo, ResponseHeaders, Template } from './@types'; import { METHODS } from './constants'; /** * Object holding the route definitions, requests matchers and response templates */ export declare class Route { private method; private path; private body; private headers; private query; private response; private logDiffOn501Request; private calls; private maxCalls; private initializePath; constructor(method: METHODS, path: PathDefinition); /** * Return the route definition, helpful to find why the route didn't match the request */ getDefinition(): { method: METHODS; path: string | symbol | RegExp | import("./@types").MatchFunction; headers: symbol | Record; query: symbol | Record; body: symbol | RegExp | import("./@types").JsonValue | import("./@types").MatchFunction | import("./@types").BodyDefinitionObject | import("./@types").BodyDefinitionArray | undefined; }; setInitializerPath(lineInfo: LineInfo | null): void; getInitializerPath(): LineInfo | null; /** * @return Route method */ getMethod(): METHODS; /** * @return The route path definition */ getPath(): PathDefinition; /** * Set the headers definition. * * @example * ```typescript * // Bypass all headers * stubborn * .get('/bypass') * .setHeaders(WILDCARD); * * stubborn * .get('/match') * .setHeaders({ * // Match using `===` * exact: 'match', * // Match using `/^match/.test(value)` * regexp: /^match/, * // Match using `func(value);` * func: (value) => 'match' === value * }); * ``` * @param headers Headers definition */ setHeaders(headers: HeadersDefinition): this; /** * @return Route headers definition */ getHeaders(): HeadersDefinition; /** * Set a specific header definition. * * @example * ```typescript * stubborn * .get('/match') * .setHeader('Authorization', 'BEARER'); * ``` * @param header Header name * @param definition Header definition */ setHeader(header: string, definition: HeaderDefinition): this; /** * Set the query parameters definition. * * @example * ```typescript * // Bypass all query parameters * stubborn * .get('/bypass') * .setQueryParameters(WILDCARD); * * stubborn * .get('/match') * .setQueryParameters({ * // Match using `===` * exact: 'match', * // Match using `/^match/.test(value)` * regexp: /^match/, * // Match using `func(value);` * func: (value) => 'match' === value * }); * ``` * @param query Query parameters definition */ setQueryParameters(query: QueryDefinition): this; /** * @return Query parameters definitions */ getQueryParameters(): QueryDefinition; /** * Set a specific query parameter * * @example * ```typescript * stubborn * .get('/match') * .setQueryParameter('page', '100'); * ``` * @param queryParameter Query parameters name * @param definition Query parameter definition */ setQueryParameter(queryParameter: string, definition: QueryParameterDefinition): this; /** * Set the body definition. Body can be a string or a complex object mixing * * @example * ```typescript * // Bypass all query parameters * stubborn * .get('/bypass') * .setBody(WILDCARD); * * stubborn * .get('/simple') * .setBody('text body'); * * stubborn * .get('/object') * .setBody({ * // Match using `===` * exact: 'match', * // Match using `/^match/.test(value)` * regexp: /^match/, * // Match using `func(value);` * func: (value) => 'match' === value, * object: { reg: /\d+/, bypass: WILDCARD }, * arr: [{ item: '1' }, { item: /2/ }] * }); * ``` * @param body Body definition */ setBody(body: BodyDefinition): this; /** * @return Request body definition */ getBody(): BodyDefinition; /** * Set the response status code * @param statusCode HTTP status code */ setResponseStatusCode(statusCode: number): this; /** * Get the response status code * @return HTTP status code */ getResponseStatusCode(): number; /** * Set the response body * * If response is an Object, values can be a function which will receive the request as parameter * * @example * ```typescript * // String body * stubborn.get('/resource') * .setResponseBody('Hello world'); * * // Objects as JSON body * stubborn.get('/resource') * .setResponseBody({ key: 'value' }); * * // Template function * stubborn.get('/resource') * .setResponseBody((request) => ({ * page: req.param.page * })); * * // Template in sub keys * stubborn.get('/resource') * .setResponseBody({ * key: { * sub1: 'val1', * sub2: (req: Request, scope: any) => `sub1=${scope.sub1}` * } * }); * ``` * @param body Body replied if route is matched */ setResponseBody(body: Template | Buffer): this; /** * @return Response body template */ getResponseBody(): Template | Buffer; /** * Set the response headers * key/value object. If value is a function it will receive the request as parameter * * @example * ```typescript * stubborn.get('/resource') * .setResponseHeaders({ * 'Content-type': 'application/json', * 'Custom-Header': (req) => `${req.header.custom}-response` * }); * ``` * @param headers Response header template */ setResponseHeaders(headers: ResponseHeaders): this; /** * @return The response headers template */ getResponseHeaders(): Record; /** * Set a response header * * @example * ```typescript * stubborn.get('/resource') * .setResponseHeader('Content-type', 'application/json'); * ``` * @param header Header name * @param value Header template */ setResponseHeader(header: string, value: string): this; /** * @example * ```typescript * const route = stubborn.get('/resource'); * expect(route.countCalls()).toBe(0); * * await got(`${stubborn.getOrigin()}/resource`); * expect(route.countCalls()).toBe(1); * ``` * * @returns The number of times the route has been called */ countCalls(): number; /** * Get information of a specific request call * @example * ```typescript * const route = stubborn.get('/resource'); * expect(route.countCalls()).toBe(0); * await got(`${stubborn.getOrigin()}/resource`); * expect(route.getCall(0)).toBe(1); * ``` * * @param index Index of the call * @returns The request received */ getCall(index: number): RequestInfo; /** * Add a request to the call stack * * This method is used internaly by the router every time the route is matched by the incomming request * @param req */ addCall(req: RequestInfo): void; /** * Set log diff on request for this route * * If this method is called and a request does not match any route, stubborn will output the diff between the current route and the request */ logDiffOn501(): this; /** * Get weather stubborn should output the diff with the current route if the request does not match any route */ shouldLogDiffOn501(): LineInfo | null; getLogDiffPosition(): LineInfo | null; /** * Specify the number of times the route should match a request before being removed from the router * ex: setting { times: 1 } and sending twice the same request will result to a 501 on the second request * * @param times * @return this */ removeRouteAfterMatching({ times }: { times: number; }): this; hasReachMaxCalls(): boolean; }