From 7408c5dffca6ee45d421321ad4c0bb3b413e6a46 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Tue, 17 Dec 2019 14:09:53 +0100 Subject: [PATCH] [connect] Add missing property originalUrl to req parameter (#40776) This adds a new class IncomingMessage which extends from http.IncomingMessage and adds the originalUrl property which was previously missing. This new class is used for the req parameter in HandleFunction. The originalUrl property is added to req here: https://github.com/senchalabs/connect/blob/3.4.0/index.js#L133 --- types/connect/connect-tests.ts | 18 ++++++++++++------ types/connect/index.d.ts | 10 +++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/types/connect/connect-tests.ts b/types/connect/connect-tests.ts index 669a146a28..c72ca38645 100644 --- a/types/connect/connect-tests.ts +++ b/types/connect/connect-tests.ts @@ -4,23 +4,23 @@ import connect = require("connect"); const app = connect(); // log all requests -app.use((req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { +app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { console.log(req, res); next(); }); // "Throw" an Error -app.use((req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { +app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { next(new Error("Something went wrong!")); }); // "Throw" a number -app.use((req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { +app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { next(404); }); // Stop on errors -app.use((err: any, req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { +app.use((err: any, req: connect.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { if (err) { return res.end(`Error: ${err}`); } @@ -29,15 +29,21 @@ app.use((err: any, req: http.IncomingMessage, res: http.ServerResponse, next: co }); // Use legacy `Function` for `next` parameter. -app.use((req: http.IncomingMessage, res: http.ServerResponse, next: Function) => { +app.use((req: connect.IncomingMessage, res: http.ServerResponse, next: Function) => { next(); }); // respond to all requests -app.use((req: http.IncomingMessage, res: http.ServerResponse) => { +app.use((req: connect.IncomingMessage, res: http.ServerResponse) => { res.end("Hello from Connect!\n"); }); +// Allow http.IncomingMessage as the type for req +app.use((req: http.IncomingMessage, res: http.ServerResponse) => { + console.log(req, res); + res.end(); +}); + //create node.js http server and listen on port http.createServer(app).listen(3000); diff --git a/types/connect/index.d.ts b/types/connect/index.d.ts index 4efddb1e7f..945db0f6ae 100644 --- a/types/connect/index.d.ts +++ b/types/connect/index.d.ts @@ -18,11 +18,15 @@ declare function createServer(): createServer.Server; declare namespace createServer { export type ServerHandle = HandleFunction | http.Server; + export class IncomingMessage extends http.IncomingMessage { + originalUrl?: http.IncomingMessage["url"]; + } + type NextFunction = (err?: any) => void; - export type SimpleHandleFunction = (req: http.IncomingMessage, res: http.ServerResponse) => void; - export type NextHandleFunction = (req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => void; - export type ErrorHandleFunction = (err: any, req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => void; + export type SimpleHandleFunction = (req: IncomingMessage, res: http.ServerResponse) => void; + export type NextHandleFunction = (req: IncomingMessage, res: http.ServerResponse, next: NextFunction) => void; + export type ErrorHandleFunction = (err: any, req: IncomingMessage, res: http.ServerResponse, next: NextFunction) => void; export type HandleFunction = SimpleHandleFunction | NextHandleFunction | ErrorHandleFunction; export interface ServerStackItem {