From 23c8bbf227ffec759d835f29c126f25298dfb2b7 Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Mon, 16 Apr 2018 17:45:19 -0700 Subject: [PATCH] connect: Add `NextFunction` for `next`, fix `err` parameter (#24883) --- types/connect/connect-tests.ts | 19 +++++++++++++++++-- types/connect/index.d.ts | 7 +++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/types/connect/connect-tests.ts b/types/connect/connect-tests.ts index 5a7590a9a0..669a146a28 100644 --- a/types/connect/connect-tests.ts +++ b/types/connect/connect-tests.ts @@ -4,13 +4,23 @@ import connect = require("connect"); const app = connect(); // log all requests -app.use((req: http.IncomingMessage, res: http.ServerResponse, next: Function) => { +app.use((req: http.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) => { + next(new Error("Something went wrong!")); +}); + +// "Throw" a number +app.use((req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { + next(404); +}); + // Stop on errors -app.use((err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: Function) => { +app.use((err: any, req: http.IncomingMessage, res: http.ServerResponse, next: connect.NextFunction) => { if (err) { return res.end(`Error: ${err}`); } @@ -18,6 +28,11 @@ app.use((err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: next(); }); +// Use legacy `Function` for `next` parameter. +app.use((req: http.IncomingMessage, res: http.ServerResponse, next: Function) => { + next(); +}); + // respond to all requests app.use((req: http.IncomingMessage, res: http.ServerResponse) => { res.end("Hello from Connect!\n"); diff --git a/types/connect/index.d.ts b/types/connect/index.d.ts index 8cebd6ba78..4efddb1e7f 100644 --- a/types/connect/index.d.ts +++ b/types/connect/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for connect v3.4.0 // Project: https://github.com/senchalabs/connect // Definitions by: Maxime LUCE +// Evan Hahn // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -17,9 +18,11 @@ declare function createServer(): createServer.Server; declare namespace createServer { export type ServerHandle = HandleFunction | http.Server; + 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: Function) => void; - export type ErrorHandleFunction = (err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: Function) => 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 HandleFunction = SimpleHandleFunction | NextHandleFunction | ErrorHandleFunction; export interface ServerStackItem {