From 1e92c152ee1b7af9b39f01789de37e2e463fd0de Mon Sep 17 00:00:00 2001 From: Cao Jiannan Date: Sat, 9 Jul 2016 02:51:18 +0800 Subject: [PATCH 01/12] enhance IRouter merge use into just one case --- .../express-serve-static-core.d.ts | 117 ++++++++---------- 1 file changed, 55 insertions(+), 62 deletions(-) diff --git a/express-serve-static-core/express-serve-static-core.d.ts b/express-serve-static-core/express-serve-static-core.d.ts index 707f506e2a..daa7b6cd0f 100644 --- a/express-serve-static-core/express-serve-static-core.d.ts +++ b/express-serve-static-core/express-serve-static-core.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Express 4.x +// Type definitions for Express 4.x // Project: http://expressjs.com // Definitions by: Boris Yankov // 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 { - (name: string | RegExp, ...handlers: RequestHandlerParams[]): T; + (path: PathParams, ...handlers: RequestHandler[]): T; + (path: PathParams, ...handlers: RequestHandlerParams[]): T; } - interface IRouter extends RequestHandler { + interface IRouterHandler { + (...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; - get: IRouterMatcher; - post: IRouterMatcher; - put: IRouterMatcher; - delete: IRouterMatcher; - patch: IRouterMatcher; - options: IRouterMatcher; - head: IRouterMatcher; + all: IRouterMatcher; + get: IRouterMatcher; + post: IRouterMatcher; + put: IRouterMatcher; + delete: IRouterMatcher; + patch: IRouterMatcher; + options: IRouterMatcher; + head: IRouterMatcher; + + use: IRouterHandler & IRouterMatcher; - 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; + get: IRouterHandler; + post: IRouterHandler; + put: IRouterHandler; + delete: IRouterHandler; + patch: IRouterHandler; + options: IRouterHandler; + head: IRouterHandler; + } - export interface Router extends IRouter { } + 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, 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; /** * 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; - } } From 5fd6d6b4eaabda87d19ad13b7d6709443617ddd8 Mon Sep 17 00:00:00 2001 From: Cao Jiannan Date: Sat, 9 Jul 2016 02:52:18 +0800 Subject: [PATCH 02/12] expose IRouterHandler --- express/express.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/express/express.d.ts b/express/express.d.ts index 4ab7eb1c90..5f65b60519 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -38,8 +38,9 @@ declare module "express" { interface Express extends core.Express { } interface Handler extends core.Handler { } interface IRoute extends core.IRoute { } - interface IRouter extends core.IRouter { } + interface IRouter extends core.IRouter { } interface IRouterMatcher extends core.IRouterMatcher { } + interface IRouterHandler extends core.IRouterHandler { } interface MediaType extends core.MediaType { } interface NextFunction extends core.NextFunction { } interface Request extends core.Request { } From e53106d303bde4e5ffa298f8e609e5907f4d13ac Mon Sep 17 00:00:00 2001 From: Cao Jiannan Date: Sat, 9 Jul 2016 10:01:54 +0800 Subject: [PATCH 03/12] update param, add `;` --- .../express-serve-static-core.d.ts | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/express-serve-static-core/express-serve-static-core.d.ts b/express-serve-static-core/express-serve-static-core.d.ts index daa7b6cd0f..3bfe0a7daf 100644 --- a/express-serve-static-core/express-serve-static-core.d.ts +++ b/express-serve-static-core/express-serve-static-core.d.ts @@ -29,9 +29,9 @@ declare module "express-serve-static-core" { (err: any, req: Request, res: Response, next: NextFunction): any; } - type PathParams = string | RegExp | (string | RegExp)[] + type PathParams = string | RegExp | (string | RegExp)[]; - type RequestHandlerParams = RequestHandler | ErrorRequestHandler + type RequestHandlerParams = RequestHandler | ErrorRequestHandler; interface IRouterMatcher { (path: PathParams, ...handlers: RequestHandler[]): T; @@ -73,11 +73,12 @@ declare module "express-serve-static-core" { * @param name * @param fn */ - 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): this; + 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 + // deprecated since express 4.11.0 + (callback: (name: string, matcher: RegExp) => RequestParamHandler): this; + }; /** * Special-cased "all" method, applying the given route `path`, @@ -807,7 +808,7 @@ declare module "express-serve-static-core" { 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, Express.Application { @@ -872,6 +873,13 @@ declare module "express-serve-static-core" { */ set(setting: string, val: any): Application; get: {(name: string): any;} & IRouterMatcher; + + 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 + // deprecated since express 4.11.0 + param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this; + }; /** * Return the app's absolute pathname From ccfd178bb4f9e78f2238e5add93b7b2ed20d0389 Mon Sep 17 00:00:00 2001 From: Cao Jiannan Date: Sat, 9 Jul 2016 10:20:53 +0800 Subject: [PATCH 04/12] Update param declare --- .../express-serve-static-core.d.ts | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/express-serve-static-core/express-serve-static-core.d.ts b/express-serve-static-core/express-serve-static-core.d.ts index 3bfe0a7daf..4e3bee4691 100644 --- a/express-serve-static-core/express-serve-static-core.d.ts +++ b/express-serve-static-core/express-serve-static-core.d.ts @@ -31,7 +31,7 @@ declare module "express-serve-static-core" { type PathParams = string | RegExp | (string | RegExp)[]; - type RequestHandlerParams = RequestHandler | ErrorRequestHandler; + type RequestHandlerParams = RequestHandler | ErrorRequestHandler | (RequestHandler | ErrorRequestHandler)[]; interface IRouterMatcher { (path: PathParams, ...handlers: RequestHandler[]): T; @@ -73,12 +73,10 @@ declare module "express-serve-static-core" { * @param name * @param fn */ - 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 - // deprecated since express 4.11.0 - (callback: (name: string, matcher: RegExp) => RequestParamHandler): this; - }; + 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 + // deprecated since express 4.11.0 + param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this; /** * Special-cased "all" method, applying the given route `path`, @@ -874,12 +872,10 @@ declare module "express-serve-static-core" { set(setting: string, val: any): Application; get: {(name: string): any;} & IRouterMatcher; - 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 - // deprecated since express 4.11.0 - param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this; - }; + param(name: string[], handler: RequestParamHandler): this; + 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): this; /** * Return the app's absolute pathname From 203818d338c3454f13298bfe743755260b90382a Mon Sep 17 00:00:00 2001 From: Cao Jiannan Date: Sun, 10 Jul 2016 23:16:00 +0800 Subject: [PATCH 05/12] remove unnecessary user property of request leave space to user defined user type --- express-serve-static-core/express-serve-static-core.d.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/express-serve-static-core/express-serve-static-core.d.ts b/express-serve-static-core/express-serve-static-core.d.ts index 4e3bee4691..a3f707c83a 100644 --- a/express-serve-static-core/express-serve-static-core.d.ts +++ b/express-serve-static-core/express-serve-static-core.d.ts @@ -384,10 +384,6 @@ declare module "express-serve-static-core" { params: any; - user: any; - - authenticatedUser: any; - /** * Clear cookie `name`. * From cb4960033f11e56f51d527e55e3e6fac2be6dbd1 Mon Sep 17 00:00:00 2001 From: Cao Jiannan Date: Mon, 11 Jul 2016 22:57:27 +0800 Subject: [PATCH 06/12] add user to Request for http-errors req.user should be declared by developer. --- http-errors/http-errors-tests.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts index 4403259008..37dc4b3cd8 100644 --- a/http-errors/http-errors-tests.ts +++ b/http-errors/http-errors-tests.ts @@ -4,6 +4,12 @@ import * as createError from 'http-errors'; import * as express from 'express'; +declare namespace Express { + export interface Request { + user: any + } +} + var app = express(); app.use(function (req, res, next) { From 6245fffb1898ed4b786b330654e46a86ec20248b Mon Sep 17 00:00:00 2001 From: Cao Jiannan Date: Mon, 11 Jul 2016 22:57:51 +0800 Subject: [PATCH 07/12] optional user --- http-errors/http-errors-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts index 37dc4b3cd8..d1400bbe4c 100644 --- a/http-errors/http-errors-tests.ts +++ b/http-errors/http-errors-tests.ts @@ -6,7 +6,7 @@ import * as express from 'express'; declare namespace Express { export interface Request { - user: any + user?: any } } From a591dbfa8e5ee74d3e1a0a0b6ec28601746ccdcc Mon Sep 17 00:00:00 2001 From: "Cao, Jiannan" Date: Mon, 11 Jul 2016 23:16:06 +0800 Subject: [PATCH 08/12] add user to http-errors.d.ts --- http-errors/http-errors-tests.ts | 6 ------ http-errors/http-errors.d.ts | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts index d1400bbe4c..4403259008 100644 --- a/http-errors/http-errors-tests.ts +++ b/http-errors/http-errors-tests.ts @@ -4,12 +4,6 @@ import * as createError from 'http-errors'; import * as express from 'express'; -declare namespace Express { - export interface Request { - user?: any - } -} - var app = express(); app.use(function (req, res, next) { diff --git a/http-errors/http-errors.d.ts b/http-errors/http-errors.d.ts index 48d7469ba4..745e313995 100644 --- a/http-errors/http-errors.d.ts +++ b/http-errors/http-errors.d.ts @@ -3,6 +3,12 @@ // Definitions by: Tanguy Krotoff // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +declare namespace Express { + export interface Request { + user?: any + } +} + declare module 'http-errors' { namespace createHttpError { From dcd4fb55facbfecac80e4a476bcdb2adf5763ab9 Mon Sep 17 00:00:00 2001 From: "Cao, Jiannan" Date: Mon, 11 Jul 2016 23:36:44 +0800 Subject: [PATCH 09/12] move req.user to http-errors-tests.d.ts --- http-errors/http-errors-tests.d.ts | 5 +++++ http-errors/http-errors-tests.ts | 2 +- http-errors/http-errors.d.ts | 6 ------ 3 files changed, 6 insertions(+), 7 deletions(-) create mode 100644 http-errors/http-errors-tests.d.ts diff --git a/http-errors/http-errors-tests.d.ts b/http-errors/http-errors-tests.d.ts new file mode 100644 index 0000000000..f6fad5ac4e --- /dev/null +++ b/http-errors/http-errors-tests.d.ts @@ -0,0 +1,5 @@ +declare namespace Express { + export interface Request { + user?: any + } +} diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts index 4403259008..e5e0c97567 100644 --- a/http-errors/http-errors-tests.ts +++ b/http-errors/http-errors-tests.ts @@ -1,5 +1,6 @@ /// /// +/// import * as createError from 'http-errors'; import * as express from 'express'; @@ -11,7 +12,6 @@ app.use(function (req, res, next) { next(); }); - /* Examples taken from https://github.com/jshttp/http-errors/blob/1.3.1/test/test.js */ // createError(status) diff --git a/http-errors/http-errors.d.ts b/http-errors/http-errors.d.ts index 745e313995..48d7469ba4 100644 --- a/http-errors/http-errors.d.ts +++ b/http-errors/http-errors.d.ts @@ -3,12 +3,6 @@ // Definitions by: Tanguy Krotoff // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare namespace Express { - export interface Request { - user?: any - } -} - declare module 'http-errors' { namespace createHttpError { From 49a98e044371c8d491cf0e451a8b61898ace2f77 Mon Sep 17 00:00:00 2001 From: "Cao, Jiannan" Date: Tue, 12 Jul 2016 00:02:46 +0800 Subject: [PATCH 10/12] add comment line --- http-errors/http-errors-tests.d.ts | 1 + http-errors/http-errors-tests.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/http-errors/http-errors-tests.d.ts b/http-errors/http-errors-tests.d.ts index f6fad5ac4e..9005c7ef41 100644 --- a/http-errors/http-errors-tests.d.ts +++ b/http-errors/http-errors-tests.d.ts @@ -1,3 +1,4 @@ +// Type definitions for http-errors-tests.ts declare namespace Express { export interface Request { user?: any diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts index e5e0c97567..f8e0ea9b8c 100644 --- a/http-errors/http-errors-tests.ts +++ b/http-errors/http-errors-tests.ts @@ -1,6 +1,6 @@ /// /// -/// +/// import * as createError from 'http-errors'; import * as express from 'express'; From 1f418be731a07eee688e3897899eff6766cf9cc8 Mon Sep 17 00:00:00 2001 From: "Cao, Jiannan" Date: Tue, 12 Jul 2016 01:23:42 +0800 Subject: [PATCH 11/12] declare Express in ambient environment --- http-errors/http-errors-tests.d.ts | 6 ------ http-errors/http-errors-tests.ts | 9 ++++++++- 2 files changed, 8 insertions(+), 7 deletions(-) delete mode 100644 http-errors/http-errors-tests.d.ts diff --git a/http-errors/http-errors-tests.d.ts b/http-errors/http-errors-tests.d.ts deleted file mode 100644 index 9005c7ef41..0000000000 --- a/http-errors/http-errors-tests.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Type definitions for http-errors-tests.ts -declare namespace Express { - export interface Request { - user?: any - } -} diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts index f8e0ea9b8c..61fb9091bf 100644 --- a/http-errors/http-errors-tests.ts +++ b/http-errors/http-errors-tests.ts @@ -1,12 +1,19 @@ /// /// -/// import * as createError from 'http-errors'; 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(); From 0dd6ead8f045e40c78caf49404ccc4240c0c7c98 Mon Sep 17 00:00:00 2001 From: "Cao, Jiannan" Date: Tue, 12 Jul 2016 02:36:08 +0800 Subject: [PATCH 12/12] merge param --- .../express-serve-static-core.d.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/express-serve-static-core/express-serve-static-core.d.ts b/express-serve-static-core/express-serve-static-core.d.ts index a3f707c83a..0851ddad9a 100644 --- a/express-serve-static-core/express-serve-static-core.d.ts +++ b/express-serve-static-core/express-serve-static-core.d.ts @@ -20,17 +20,17 @@ declare module "express-serve-static-core" { 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 { @@ -93,7 +93,7 @@ declare module "express-serve-static-core" { patch: IRouterMatcher; options: IRouterMatcher; head: IRouterMatcher; - + use: IRouterHandler & IRouterMatcher; route(prefix: PathParams): IRoute; @@ -867,9 +867,8 @@ declare module "express-serve-static-core" { */ set(setting: string, val: any): Application; get: {(name: string): any;} & IRouterMatcher; - - param(name: string[], handler: RequestParamHandler): this; - param(name: string, handler: RequestParamHandler): 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;