>;
}
/**
* Represents an incoming HTTP request on the server; An extension of the built-in
* `IncomingMessage` type
*
* @template P Any URL params that will be parsed out by the router
*/
export interface Request extends IncomingMessage {
pathname?: string;
querystring?: string;
params?: P;
glob?: string;
}
/**
* Represents an outgoing response to an HTTP request; An extension of the built-in
* `ServerResponse` type.
*/
export interface Response extends ServerResponse {
}
/**
* The input parameter for a server middleware function
*
* @template P Any URL params that will be parsed out by the router
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
export interface MiddlewareInput
extends RouterMiddwareInput {
req: Request
& Req;
res: Response;
}
declare type AnyInput = MiddlewareInput;
declare type AnyRouter = Router, AnyInput>;
/**
* An HTTP server with a built-in middleware pipeline and router
*/
export declare class Server {
readonly server: HttpServer;
protected readonly _router: AnyRouter;
protected readonly _pipeline: MiddlewarePipeline;
/**
* @param server The underlying HTTP server to listen for requests on
* @param router The underlying router to use
*/
constructor(server: HttpServer, router: AnyRouter);
/**
* Registers a middleware function in the top-level middleware pipeline.
* Any middlewares registered here will run for every incoming request.
*
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
use(middleware: MiddlewareFunction>): this;
/**
* Registers an error middleware function in the top-level middleware pipeline.
*
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
catch(middleware: ErrorMiddlewareFunction>): this;
/**
* Registers an endpoint on the router.
*
* @param method The HTTP method to catch requests for
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
route(method: string, route: string): Route>;
/**
* Registers a GET endpoint on the router.
*
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
get(route: string): Route>;
/**
* Registers a HEAD endpoint on the router.
*
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
head(route: string): Route>;
/**
* Registers a POST endpoint on the router.
*
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
post(route: string): Route>;
/**
* Registers a PUT endpoint on the router.
*
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
put(route: string): Route>;
/**
* Registers a PATCH endpoint on the router.
*
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
patch(route: string): Route>;
/**
* Registers a DELETE endpoint on the router.
*
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
delete(route: string): Route>;
/**
* Registers a OPTIONS endpoint on the router.
*
* @param route The endpoint route pattern to listen for
* @template P Any URL params that will be parsed out by the router (should only be used
* on middlewares registered after the router)
* @template Req Any additional extensions that will exist on the request object, useful
* for registering the result of a body parser, query string parser, etc.
*/
options(route: string): Route>;
/**
* Returns a middleware function that passes the request through the registered router.
* This should be registered as a top-level middleware at the point in the main pipeline
* when you want endpoints to run.
*
* ```typescript
* const options = {
* // ...
* };
*
* server.use(server.router(options));
* ```
*
* @param options The options used when generating the router endpoint
*/
router(options: RouterOptions): ({ req, res }: MiddlewareInput) => Promise;
/**
* The method registered to the HTTP server on('request') event. Simply passes
* the request and response objects into the top-level middleware pipeline.
*/
protected onRequest(req: Request, res: Response): void;
}
export {};