mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 23:07:06 +00:00
Merge pull request #10025 from frogcjn/master
Express - enhance Router (squashed)
This commit is contained in:
+58
-66
@@ -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 | (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,10 @@ 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, handler: RequestParamHandler): 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;
|
||||
// deprecated since express 4.11.0
|
||||
param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this;
|
||||
|
||||
/**
|
||||
* Special-cased "all" method, applying the given route `path`,
|
||||
@@ -79,30 +85,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>;
|
||||
|
||||
route(path: string | RegExp): IRoute;
|
||||
use: IRouterHandler<this> & IRouterMatcher<this>;
|
||||
|
||||
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;
|
||||
@@ -374,10 +384,6 @@ declare module "express-serve-static-core" {
|
||||
|
||||
params: any;
|
||||
|
||||
user: any;
|
||||
|
||||
authenticatedUser: any;
|
||||
|
||||
/**
|
||||
* Clear cookie `name`.
|
||||
*
|
||||
@@ -782,33 +788,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;
|
||||
(req: Request, res: Response, next: NextFunction, value: any, name: string): any;
|
||||
}
|
||||
|
||||
interface Application extends IRouter<Application>, Express.Application {
|
||||
interface Application extends IRouter, Express.Application {
|
||||
/**
|
||||
* Initialize the server.
|
||||
*
|
||||
@@ -869,10 +866,11 @@ 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>;
|
||||
|
||||
param(name: string | string[], handler: RequestParamHandler): 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): this;
|
||||
|
||||
/**
|
||||
* Return the app's absolute pathname
|
||||
@@ -1017,8 +1015,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 +1062,4 @@ declare module "express-serve-static-core" {
|
||||
|
||||
response: Response;
|
||||
}
|
||||
|
||||
interface RequestHandler {
|
||||
(req: Request, res: Response, next: NextFunction): any;
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+2
-1
@@ -38,8 +38,9 @@ declare module "express" {
|
||||
interface Express extends core.Express { }
|
||||
interface Handler extends core.Handler { }
|
||||
interface IRoute extends core.IRoute { }
|
||||
interface IRouter<T> extends core.IRouter<T> { }
|
||||
interface IRouter extends core.IRouter { }
|
||||
interface IRouterMatcher<T> extends core.IRouterMatcher<T> { }
|
||||
interface IRouterHandler<T> extends core.IRouterHandler<T> { }
|
||||
interface MediaType extends core.MediaType { }
|
||||
interface NextFunction extends core.NextFunction { }
|
||||
interface Request extends core.Request { }
|
||||
|
||||
@@ -6,12 +6,19 @@ import * as express from 'express';
|
||||
|
||||
var app = express();
|
||||
|
||||
declare global {
|
||||
namespace Express {
|
||||
export interface Request {
|
||||
user?: any
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
if (!req.user) return next(createError(401, 'Please login to view this page.'));
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
/* Examples taken from https://github.com/jshttp/http-errors/blob/1.3.1/test/test.js */
|
||||
|
||||
// createError(status)
|
||||
|
||||
Reference in New Issue
Block a user