import { Layer, LayerOptions } from './layer'; import type { IHttpServerComponent } from '@well-known-components/interfaces'; /** @public */ export type RouterOptions = Partial<{ methods: IHttpServerComponent.HTTPMethod[]; prefix: string; routerPath: string; sensitive: boolean; strict: boolean; }>; /** @public */ export type AllowedMethodOptions = Partial<{ throw: boolean; notImplemented: NewableFunction; methodNotAllowed: NewableFunction; }>; /** @public */ export type RoutedContext = IHttpServerComponent.PathAwareContext & { captures: string[]; matched?: Layer[]; routerPath?: string; }; /** @public */ export type RoutePathSignature = (path: T, ...middlewares: Array>>) => Router; /** * Create a new router. * * @example * * Basic usage: * * ```javascript * const app = createTestServerComponent(); * const router = new Router(); * * router.get('/', (ctx, next) => { * // ctx.router available * }); * * app * .use(router.routes()) * .use(router.allowedMethods()); * ``` * @public */ export declare class Router implements IHttpServerComponent.MethodHandlers { opts: RouterOptions; methods: (IHttpServerComponent.HTTPMethod | string)[]; stack: Layer[]; constructor(opts?: RouterOptions); connect: RoutePathSignature; delete: RoutePathSignature; get: RoutePathSignature; head: RoutePathSignature; options: RoutePathSignature; patch: RoutePathSignature; post: RoutePathSignature; put: RoutePathSignature; trace: RoutePathSignature; /** * Use given middleware. * * Middleware run in the order they are defined by `.use()`. They are invoked * sequentially, requests start at the first middleware and work their way * "down" the middleware stack. * * @example * * ```javascript * // session middleware will run before authorize * router * .use(session()) * .use(authorize()); * * // use middleware only with given path * router.use('/users', userAuth()); * * // or with an array of paths * router.use(['/users', '/admin'], userAuth()); * * app.use(router.routes()); * ``` * * @param path - * @param middleware - */ use(...middlewares: IHttpServerComponent.IRequestHandler>[]): this; use

(route: P, ...middlewares: IHttpServerComponent.IRequestHandler>[]): this; /** * Set the path prefix for a Router instance that was already initialized. * * @example * * ```javascript * router.prefix('/things/:thing_id') * ``` * * @param prefix - */ prefix(prefix: string): this; /** * Returns router middleware which dispatches a route matching the request. */ middleware(): IHttpServerComponent.IRequestHandler; /** * Returns separate middleware for responding to `OPTIONS` requests with * an `Allow` header containing the allowed methods, as well as responding * with `405 Method Not Allowed` and `501 Not Implemented` as appropriate. * * @example * * ```javascript * const app = createTestServerComponent(); * const router = new Router(); * * app.use(router.routes()); * app.use(router.allowedMethods()); * ``` * * **Example with [Boom](https://github.com/hapijs/boom)** * * ```javascript * const Boom = require('boom'); * * const app = createTestServerComponent(); * const router = new Router(); * * app.use(router.routes()); * app.use(router.allowedMethods({ * throw: true, * notImplemented: () => new Boom.notImplemented(), * methodNotAllowed: () => new Boom.methodNotAllowed() * })); * ``` * * @param options - */ allowedMethods(options?: AllowedMethodOptions): IHttpServerComponent.IRequestHandler; /** * Register route with all methods. * * @param name - Optional. * @param path - * @param middleware - You may also pass multiple middleware. * @param callback - */ all(path: T, middleware: IHttpServerComponent.IRequestHandler>): this; /** * Redirect `source` to `destination` URL with optional 30x status `code`. * * Both `source` and `destination` can be route names. * * ```javascript * router.redirect('/login', 'sign-in'); * ``` * * This is equivalent to: * * ```javascript * router.all('/login', ctx => { * ctx.redirect('/sign-in'); * ctx.status = 301; * }); * ``` * * @param source - URL or route name. * @param destination - URL or route name. * @param code - HTTP status code (default: 301). */ redirect(source: string, destination: string, code?: number): this; /** * Create and register a route. * * @param path - Path string. * @param methods - Array of HTTP verbs. * @param middleware - Multiple middleware also accepted. */ register(path: Path, methods: ReadonlyArray, middleware: IHttpServerComponent.IRequestHandler, opts?: LayerOptions): Layer; /** * Match given `path` and return corresponding routes. * * @param path - * @param method - */ match(path: string, method: string): { path: Layer[]; pathAndMethod: Layer[]; route: boolean; }; }