/** * Definition of [[`Filter`]], the basic building block * @packageDocumentation */ import { Concat, Tuple } from "./types"; import { Error } from "./error"; import { Request } from "."; /** * Return type of [[`FilterFn`]] */ export interface FilterRet { /** * Extracted variables */ tuple: T; /** * Weight */ weight: number; /** * Depth */ depth: number; } /** * Function run by [[`Filter`]] for each request * * @typeParam T - Extracted variables */ export declare type FilterFn = (request: Request, weight: number, depth: number) => Promise>; /** * The base building block for all routes routes * * Filters extract variables from a request * and can be chained using the various combinator methods * * @typeParam T - Variables extracted by the filter (must have a known length) */ export declare class Filter { /** * Function run for each request going through this filter */ readonly run: FilterFn; /** * Creates a new filter * * Unless you need a lot of flexibility, prefer the simpler [[`filter`]] function for cleaner code. * You also need to wrap the rejections manually when using this constructor, * which can be done using the [[`asError`]] function. * * @param f - [[`run`]] function */ constructor(f: FilterFn); /** * Creates a new filter that runs the current filter, then the provided filter, * then combines their extracted variables * * @param f - Other filter */ and(f: Filter): Filter>; /** * Creates a filter that runs the current filter, * or the provided filter if the current one fails * * @param f - Other filter */ or(f: Filter): Filter; /** * Creates a filter that maps the previously extracted variables to new ones * * @param fn - Mapping function */ map(fn: Map): Filter; /** * Creates a filter that runs the current filter and handle possible errors * * @param fn - Recovery function */ recover(fn: Recover): Filter; /** * Creates a filter that wraps the current filter * * @param fn - Wrapper function */ with(fn: With): Filter; } /** * Mapping function used to convert previously extracted variables to something else * * @typeParam T - Previously extracted variables * @typeParam U - Mapped variables * * @returns - Either a function that returns the mapped variables or the mapped variables directly */ export declare type Map = (...args: T) => Promise | U>; /** * Recovery function used to recover from errors * * @typeParam T - Extracted variables * * @returns New filter */ export declare type Recover = (error: Error) => Promise | T>; /** * Wrapping function used to wrap some functionality around a filter * * @typeParam T - Extracted variables * * @returns Wrapped filter */ export declare type With = (filter: Filter) => Promise>; /** * Creates a new filter using the providing function * * @typeParam T - Extracted variables (must have a known length) * * @param fn - Function to pass requests through * @param weight - Weight of the filter (defaults to 1) */ export declare function filter(fn: (request: Request) => Promise, weight?: number): Filter; /** * Creates a new filter immediately extracting the provided variables * * @typeParam T - Extracted variables (must have a known length) * * @param value - Extracted variables * @param weight - Weight of the filter (defaults to 1) */ export declare function filter(value: T, weight?: number): Filter;