From cb2beb72e4dce051c3a091e60798329e27d39a61 Mon Sep 17 00:00:00 2001 From: George Valotasios Date: Tue, 10 Dec 2013 11:05:18 +0100 Subject: [PATCH 1/7] Experimenting with Router definition --- express/express-tests.ts | 11 +++++++++++ express/express.d.ts | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/express/express-tests.ts b/express/express-tests.ts index 27ba253c1..a0f10f6e9 100644 --- a/express/express-tests.ts +++ b/express/express-tests.ts @@ -1246,6 +1246,16 @@ if (!module.parent) { ////// +var router = new express.Router(); + +router.get('/', function (req, resp, next?) { + resp.send('response from router'); + resp.end(); + if (next) { + next(); + } +}); + function test_general() { app.use(function (err, req, res, next) { @@ -1478,4 +1488,5 @@ function test_middleware() { app.use(express.cookieSession()); app.use(express.directory('public')); app.use(express.static('public')); + app.use(router.middleware); } diff --git a/express/express.d.ts b/express/express.d.ts index 12456ac8e..268598358 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -62,6 +62,14 @@ declare module "express" { new (method: string, path: string, callbacks: Function[], options: any): Route; } + export class Router { + new (options: any): Router; + + middleware (): any; + + get(path: string, ...handlers: RequestFunction[]): Router; + } + interface Handler { (req: Request, res: Response, next?: Function): void; } From 183a9c40acca2d7d0d1998c8f07115c737154e8b Mon Sep 17 00:00:00 2001 From: George Valotasios Date: Tue, 10 Dec 2013 11:11:13 +0100 Subject: [PATCH 2/7] The options of the Router are optional --- express/express.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/express/express.d.ts b/express/express.d.ts index 268598358..9cd6e4596 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -63,7 +63,7 @@ declare module "express" { } export class Router { - new (options: any): Router; + new (options?: any): Router; middleware (): any; From 27207fc221eca60ecaebbdb14292af49e8c5e674 Mon Sep 17 00:00:00 2001 From: George Valotasios Date: Fri, 13 Dec 2013 11:08:42 +0100 Subject: [PATCH 3/7] Better definition of the express Router class --- express/express.d.ts | 147 +++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 60 deletions(-) diff --git a/express/express.d.ts b/express/express.d.ts index 9cd6e4596..dc5ddc730 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -62,12 +62,96 @@ declare module "express" { new (method: string, path: string, callbacks: Function[], options: any): Route; } - export class Router { + interface IRouter { + /** + * Map the given param placeholder `name`(s) to the given callback(s). + * + * Parameter mapping is used to provide pre-conditions to routes + * which use normalized placeholders. For example a _:user_id_ parameter + * could automatically load a user's information from the database without + * any additional code, + * + * The callback uses the samesignature as middleware, the only differencing + * being that the value of the placeholder is passed, in this case the _id_ + * of the user. Once the `next()` function is invoked, just like middleware + * it will continue on to execute the route, or subsequent parameter functions. + * + * app.param('user_id', function(req, res, next, id){ + * User.find(id, function(err, user){ + * if (err) { + * next(err); + * } else if (user) { + * req.user = user; + * next(); + * } else { + * next(new Error('failed to load user')); + * } + * }); + * }); + * + * @param name + * @param fn + */ + param(name: string, fn: Function): IRouter; + + param(name: any[], fn: Function): IRouter; + + /** + * Special-cased "all" method, applying the given route `path`, + * middleware, and callback to _every_ HTTP method. + * + * @param path + * @param fn + */ + all(path: string, fn?: (req: Request, res: Response, next: Function) => any): IRouter; + + all(path: string, ...callbacks: Function[]): void; + + get(name: string, ...handlers: RequestFunction[]): IRouter; + + get(name: RegExp, ...handlers: RequestFunction[]): IRouter; + + post(name: string, ...handlers: RequestFunction[]): IRouter; + + post(name: RegExp, ...handlers: RequestFunction[]): IRouter; + + put(name: string, ...handlers: RequestFunction[]): IRouter; + + put(name: RegExp, ...handlers: RequestFunction[]): IRouter; + + del(name: string, ...handlers: RequestFunction[]): IRouter; + + del(name: RegExp, ...handlers: RequestFunction[]): IRouter; + } + + export class Router implements IRouter { new (options?: any): Router; middleware (): any; - get(path: string, ...handlers: RequestFunction[]): Router; + param(name: string, fn: Function): Router; + + param(name: any[], fn: Function): Router; + + all(path: string, fn?: (req: Request, res: Response, next: Function) => any): Router; + + all(path: string, ...callbacks: Function[]): void; + + get(name: string, ...handlers: RequestFunction[]): Router; + + get(name: RegExp, ...handlers: RequestFunction[]): Router; + + post(name: string, ...handlers: RequestFunction[]): Router; + + post(name: RegExp, ...handlers: RequestFunction[]): Router; + + put(name: string, ...handlers: RequestFunction[]): Router; + + put(name: RegExp, ...handlers: RequestFunction[]): Router; + + del(name: string, ...handlers: RequestFunction[]): Router; + + del(name: RegExp, ...handlers: RequestFunction[]): Router; } interface Handler { @@ -804,7 +888,7 @@ declare module "express" { (req: Request, res: Response, next: Function): any; } - interface Application { + interface Application extends IRouter { /** * Initialize the server. * @@ -861,38 +945,6 @@ declare module "express" { */ engine(ext: string, fn: Function): Application; - /** - * Map the given param placeholder `name`(s) to the given callback(s). - * - * Parameter mapping is used to provide pre-conditions to routes - * which use normalized placeholders. For example a _:user_id_ parameter - * could automatically load a user's information from the database without - * any additional code, - * - * The callback uses the samesignature as middleware, the only differencing - * being that the value of the placeholder is passed, in this case the _id_ - * of the user. Once the `next()` function is invoked, just like middleware - * it will continue on to execute the route, or subsequent parameter functions. - * - * app.param('user_id', function(req, res, next, id){ - * User.find(id, function(err, user){ - * if (err) { - * next(err); - * } else if (user) { - * req.user = user; - * next(); - * } else { - * next(new Error('failed to load user')); - * } - * }); - * }); - * - * @param name - * @param fn - */ - param(name: string, fn: Function): Application; - - param(name: any[], fn: Function): Application; /** * Assign `setting` to `val`, or return `setting`'s value. @@ -1014,16 +1066,6 @@ declare module "express" { configure(fn: Function): Application; - /** - * Special-cased "all" method, applying the given route `path`, - * middleware, and callback to _every_ HTTP method. - * - * @param path - * @param fn - */ - all(path: string, fn?: (req: Request, res: Response, next: Function) => any): Application; - - all(path: string, ...callbacks: Function[]): void; /** * Render the given view `name` name with `options` @@ -1044,21 +1086,6 @@ declare module "express" { render(name: string, callback: (err: Error, html: string) => void): void; - get(name: string, ...handlers: RequestFunction[]): any; - - get(name: RegExp, ...handlers: RequestFunction[]): any; - - post(name: string, ...handlers: RequestFunction[]): any; - - post(name: RegExp, ...handlers: RequestFunction[]): any; - - put(name: string, ...handlers: RequestFunction[]): any; - - put(name: RegExp, ...handlers: RequestFunction[]): any; - - del(name: string, ...handlers: RequestFunction[]): any; - - del(name: RegExp, ...handlers: RequestFunction[]): any; /** * Listen for connections. From 6c53d6db838df0f93e9d270fd30a4db2023c3df4 Mon Sep 17 00:00:00 2001 From: George Valotasios Date: Fri, 13 Dec 2013 11:21:17 +0100 Subject: [PATCH 4/7] Redifine IRouter method signatures with in the Application interface to return an Application object --- express/express.d.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/express/express.d.ts b/express/express.d.ts index dc5ddc730..69601df9e 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -1135,6 +1135,32 @@ declare module "express" { * simply by removing them from this object. */ routes: any; + + + /** + * IRouter definition + */ + param(name: string, fn: Function): Application; + + param(name: any[], fn: Function): Application; + + all(path: string, fn?: (req: Request, res: Response, next: Function) => any): Application; + + get(name: string, ...handlers: RequestFunction[]): Application; + + get(name: RegExp, ...handlers: RequestFunction[]): Application; + + post(name: string, ...handlers: RequestFunction[]): Application; + + post(name: RegExp, ...handlers: RequestFunction[]): Application; + + put(name: string, ...handlers: RequestFunction[]): Application; + + put(name: RegExp, ...handlers: RequestFunction[]): Application; + + del(name: string, ...handlers: RequestFunction[]): Application; + + del(name: RegExp, ...handlers: RequestFunction[]): Application; } interface Express extends Application { @@ -1797,6 +1823,7 @@ declare module "express" { function urlencoded(): any; function multipart(): any; + } export = e; From bdd3ad1e629579de50eb42665e3ee66e83c8159d Mon Sep 17 00:00:00 2001 From: George Valotasios Date: Fri, 13 Dec 2013 13:13:11 +0100 Subject: [PATCH 5/7] Make use of generics for IRouter so that we do not have to override everyting at the Application interface --- express/express.d.ts | 54 ++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/express/express.d.ts b/express/express.d.ts index 69601df9e..488cc1aa5 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -62,7 +62,7 @@ declare module "express" { new (method: string, path: string, callbacks: Function[], options: any): Route; } - interface IRouter { + interface IRouter { /** * Map the given param placeholder `name`(s) to the given callback(s). * @@ -92,9 +92,9 @@ declare module "express" { * @param name * @param fn */ - param(name: string, fn: Function): IRouter; + param(name: string, fn: Function): T; - param(name: any[], fn: Function): IRouter; + param(name: any[], fn: Function): T; /** * Special-cased "all" method, applying the given route `path`, @@ -103,28 +103,28 @@ declare module "express" { * @param path * @param fn */ - all(path: string, fn?: (req: Request, res: Response, next: Function) => any): IRouter; + all(path: string, fn?: (req: Request, res: Response, next: Function) => any): T; all(path: string, ...callbacks: Function[]): void; - get(name: string, ...handlers: RequestFunction[]): IRouter; + get(name: string, ...handlers: RequestFunction[]): T; - get(name: RegExp, ...handlers: RequestFunction[]): IRouter; + get(name: RegExp, ...handlers: RequestFunction[]): T; - post(name: string, ...handlers: RequestFunction[]): IRouter; + post(name: string, ...handlers: RequestFunction[]): T; - post(name: RegExp, ...handlers: RequestFunction[]): IRouter; + post(name: RegExp, ...handlers: RequestFunction[]): T; - put(name: string, ...handlers: RequestFunction[]): IRouter; + put(name: string, ...handlers: RequestFunction[]): T; - put(name: RegExp, ...handlers: RequestFunction[]): IRouter; + put(name: RegExp, ...handlers: RequestFunction[]): T; - del(name: string, ...handlers: RequestFunction[]): IRouter; + del(name: string, ...handlers: RequestFunction[]): T; - del(name: RegExp, ...handlers: RequestFunction[]): IRouter; + del(name: RegExp, ...handlers: RequestFunction[]): T; } - export class Router implements IRouter { + export class Router implements IRouter { new (options?: any): Router; middleware (): any; @@ -888,7 +888,7 @@ declare module "express" { (req: Request, res: Response, next: Function): any; } - interface Application extends IRouter { + interface Application extends IRouter { /** * Initialize the server. * @@ -1135,32 +1135,6 @@ declare module "express" { * simply by removing them from this object. */ routes: any; - - - /** - * IRouter definition - */ - param(name: string, fn: Function): Application; - - param(name: any[], fn: Function): Application; - - all(path: string, fn?: (req: Request, res: Response, next: Function) => any): Application; - - get(name: string, ...handlers: RequestFunction[]): Application; - - get(name: RegExp, ...handlers: RequestFunction[]): Application; - - post(name: string, ...handlers: RequestFunction[]): Application; - - post(name: RegExp, ...handlers: RequestFunction[]): Application; - - put(name: string, ...handlers: RequestFunction[]): Application; - - put(name: RegExp, ...handlers: RequestFunction[]): Application; - - del(name: string, ...handlers: RequestFunction[]): Application; - - del(name: RegExp, ...handlers: RequestFunction[]): Application; } interface Express extends Application { From e9402f9f235c6cbc5752d28ad502e3541726ac43 Mon Sep 17 00:00:00 2001 From: George Valotasios Date: Fri, 13 Dec 2013 14:22:49 +0100 Subject: [PATCH 6/7] Removed the extends from the IRouter definition --- express/express.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/express/express.d.ts b/express/express.d.ts index 488cc1aa5..942193376 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -62,7 +62,7 @@ declare module "express" { new (method: string, path: string, callbacks: Function[], options: any): Route; } - interface IRouter { + interface IRouter { /** * Map the given param placeholder `name`(s) to the given callback(s). * From 47bcdb5cd827c975562319d9c6ca86b7d89aeaf9 Mon Sep 17 00:00:00 2001 From: George Valotasios Date: Mon, 16 Dec 2013 15:27:51 +0100 Subject: [PATCH 7/7] Just a dummy eol to cause travis to rebuild --- express/express.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/express/express.d.ts b/express/express.d.ts index 942193376..59c0df357 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -1802,3 +1802,4 @@ declare module "express" { export = e; } +