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 {