mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 23:07:06 +00:00
enhance IRouter
merge use into just one case
This commit is contained in:
+55
-62
@@ -1,4 +1,4 @@
|
||||
// Type definitions for Express 4.x
|
||||
// Type definitions for Express 4.x
|
||||
// Project: http://expressjs.com
|
||||
// Definitions by: Boris Yankov <https://github.com/borisyankov/>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
@@ -17,26 +17,33 @@ declare namespace Express {
|
||||
declare module "express-serve-static-core" {
|
||||
import * as http from "http";
|
||||
|
||||
type RequestHandlerParams = RequestHandler | RequestHandler[];
|
||||
|
||||
interface IRoute {
|
||||
path: string;
|
||||
stack: any;
|
||||
all(...handler: RequestHandlerParams[]): IRoute;
|
||||
get(...handler: RequestHandlerParams[]): IRoute;
|
||||
post(...handler: RequestHandlerParams[]): IRoute;
|
||||
put(...handler: RequestHandlerParams[]): IRoute;
|
||||
delete(...handler: RequestHandlerParams[]): IRoute;
|
||||
patch(...handler: RequestHandlerParams[]): IRoute;
|
||||
options(...handler: RequestHandlerParams[]): IRoute;
|
||||
head(...handler: RequestHandlerParams[]): IRoute;
|
||||
interface NextFunction {
|
||||
(err?: any): void;
|
||||
}
|
||||
|
||||
interface RequestHandler {
|
||||
(req: Request, res: Response, next: NextFunction): any;
|
||||
}
|
||||
|
||||
interface ErrorRequestHandler {
|
||||
(err: any, req: Request, res: Response, next: NextFunction): any;
|
||||
}
|
||||
|
||||
type PathParams = string | RegExp | (string | RegExp)[]
|
||||
|
||||
type RequestHandlerParams = RequestHandler | ErrorRequestHandler
|
||||
|
||||
interface IRouterMatcher<T> {
|
||||
(name: string | RegExp, ...handlers: RequestHandlerParams[]): T;
|
||||
(path: PathParams, ...handlers: RequestHandler[]): T;
|
||||
(path: PathParams, ...handlers: RequestHandlerParams[]): T;
|
||||
}
|
||||
|
||||
interface IRouter<T> extends RequestHandler {
|
||||
interface IRouterHandler<T> {
|
||||
(...handlers: RequestHandler[]): T;
|
||||
(...handlers: RequestHandlerParams[]): T;
|
||||
}
|
||||
|
||||
interface IRouter extends RequestHandler {
|
||||
/**
|
||||
* Map the given param placeholder `name`(s) to the given callback(s).
|
||||
*
|
||||
@@ -66,11 +73,11 @@ declare module "express-serve-static-core" {
|
||||
* @param name
|
||||
* @param fn
|
||||
*/
|
||||
param(name: string, handler: RequestParamHandler): T;
|
||||
param(name: string, matcher: RegExp): T;
|
||||
param(name: string, mapper: (param: any) => any): T;
|
||||
param(name: string | string[], handler: RequestParamHandler): this;
|
||||
param(name: string | string[], matcher: RegExp): this;
|
||||
param(name: string | string[], mapper: (param: any) => any): this;
|
||||
// Alternatively, you can pass only a callback, in which case you have the opportunity to alter the app.param() API
|
||||
param(callback: (name: string, matcher: RegExp) => RequestParamHandler): T;
|
||||
param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this;
|
||||
|
||||
/**
|
||||
* Special-cased "all" method, applying the given route `path`,
|
||||
@@ -79,30 +86,34 @@ declare module "express-serve-static-core" {
|
||||
* @param path
|
||||
* @param fn
|
||||
*/
|
||||
all: IRouterMatcher<T>;
|
||||
get: IRouterMatcher<T>;
|
||||
post: IRouterMatcher<T>;
|
||||
put: IRouterMatcher<T>;
|
||||
delete: IRouterMatcher<T>;
|
||||
patch: IRouterMatcher<T>;
|
||||
options: IRouterMatcher<T>;
|
||||
head: IRouterMatcher<T>;
|
||||
all: IRouterMatcher<this>;
|
||||
get: IRouterMatcher<this>;
|
||||
post: IRouterMatcher<this>;
|
||||
put: IRouterMatcher<this>;
|
||||
delete: IRouterMatcher<this>;
|
||||
patch: IRouterMatcher<this>;
|
||||
options: IRouterMatcher<this>;
|
||||
head: IRouterMatcher<this>;
|
||||
|
||||
use: IRouterHandler<this> & IRouterMatcher<this>;
|
||||
|
||||
route(path: string | RegExp): IRoute;
|
||||
|
||||
use(...handler: RequestHandlerParams[]): T;
|
||||
use(handler: ErrorRequestHandler | RequestHandlerParams): T;
|
||||
use(path: string, ...handler: RequestHandlerParams[]): T;
|
||||
use(path: string, handler: ErrorRequestHandler | RequestHandlerParams): T;
|
||||
use(path: string[], ...handler: RequestHandlerParams[]): T;
|
||||
use(path: string[], handler: ErrorRequestHandler): T;
|
||||
use(path: RegExp, ...handler: RequestHandlerParams[]): T;
|
||||
use(path: RegExp, handler: ErrorRequestHandler): T;
|
||||
use(path: string, router: Router): T;
|
||||
route(prefix: PathParams): IRoute;
|
||||
}
|
||||
|
||||
interface IRoute {
|
||||
path: string;
|
||||
stack: any;
|
||||
all: IRouterHandler<this>;
|
||||
get: IRouterHandler<this>;
|
||||
post: IRouterHandler<this>;
|
||||
put: IRouterHandler<this>;
|
||||
delete: IRouterHandler<this>;
|
||||
patch: IRouterHandler<this>;
|
||||
options: IRouterHandler<this>;
|
||||
head: IRouterHandler<this>;
|
||||
}
|
||||
|
||||
export interface Router extends IRouter<Router> { }
|
||||
export interface Router extends IRouter { }
|
||||
|
||||
interface CookieOptions {
|
||||
maxAge?: number;
|
||||
@@ -782,33 +793,24 @@ declare module "express-serve-static-core" {
|
||||
locals: any;
|
||||
|
||||
charset: string;
|
||||
|
||||
|
||||
/**
|
||||
* Adds the field to the Vary response header, if it is not there already.
|
||||
* Examples:
|
||||
*
|
||||
*
|
||||
* res.vary('User-Agent').render('docs');
|
||||
*
|
||||
*/
|
||||
vary(field: string): Response;
|
||||
}
|
||||
|
||||
interface NextFunction {
|
||||
(err?: any): void;
|
||||
}
|
||||
|
||||
interface ErrorRequestHandler {
|
||||
(err: any, req: Request, res: Response, next: NextFunction): any;
|
||||
}
|
||||
|
||||
|
||||
interface Handler extends RequestHandler { }
|
||||
|
||||
interface RequestParamHandler {
|
||||
(req: Request, res: Response, next: NextFunction, param: any): any;
|
||||
}
|
||||
|
||||
interface Application extends IRouter<Application>, Express.Application {
|
||||
interface Application extends IRouter, Express.Application {
|
||||
/**
|
||||
* Initialize the server.
|
||||
*
|
||||
@@ -869,10 +871,7 @@ declare module "express-serve-static-core" {
|
||||
* @param val
|
||||
*/
|
||||
set(setting: string, val: any): Application;
|
||||
get: {
|
||||
(name: string): any; // Getter
|
||||
(name: string | RegExp, ...handlers: RequestHandlerParams[]): Application;
|
||||
};
|
||||
get: {(name: string): any;} & IRouterMatcher<this>;
|
||||
|
||||
/**
|
||||
* Return the app's absolute pathname
|
||||
@@ -1017,8 +1016,6 @@ declare module "express-serve-static-core" {
|
||||
listen(path: string, callback?: Function): http.Server;
|
||||
listen(handle: any, listeningListener?: Function): http.Server;
|
||||
|
||||
route(path: string | RegExp): IRoute;
|
||||
|
||||
router: string;
|
||||
|
||||
settings: any;
|
||||
@@ -1066,8 +1063,4 @@ declare module "express-serve-static-core" {
|
||||
|
||||
response: Response;
|
||||
}
|
||||
|
||||
interface RequestHandler {
|
||||
(req: Request, res: Response, next: NextFunction): any;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user