From 33b37a1a62548d9a626bd883b562180bcd26b8f1 Mon Sep 17 00:00:00 2001 From: Maxime LUCE Date: Thu, 22 Oct 2015 00:39:42 +0200 Subject: [PATCH 1/4] Create initial connect typings. --- connect/connect.d.ts | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 connect/connect.d.ts diff --git a/connect/connect.d.ts b/connect/connect.d.ts new file mode 100644 index 0000000000..5485d1d81a --- /dev/null +++ b/connect/connect.d.ts @@ -0,0 +1,89 @@ +// Type definitions for connect v3.4.0. +// Project: https://github.com/senchalabs/connect +// Definitions by: Maxime LUCE +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "connect" { + import * as http from "http"; + + /** + * Create a new connect server. + * @public + */ + function createServer(): createServer.Server; + + module createServer { + export type ServerHandle = HandleFunction | http.Server; + export type HandleFunction = (req: http.IncomingMessage, res: http.ServerResponse, next: Function) => void; + + export interface ServerStackItem { + route: string; + handle: ServerHandle; + } + + export interface Server extends NodeJS.EventEmitter { + (req: http.IncomingMessage, res: http.ServerResponse, next: Function) => void; + + route: string; + stack: ServerStackItem[]; + + /** + * Utilize the given middleware `handle` to the given `route`, + * defaulting to _/_. This "route" is the mount-point for the + * middleware, when given a value other than _/_ the middleware + * is only effective when that segment is present in the request's + * pathname. + * + * For example if we were to mount a function at _/admin_, it would + * be invoked on _/admin_, and _/admin/settings_, however it would + * not be invoked for _/_, or _/posts_. + * + * @public + */ + use(fn: HandleFunction): Server; + use(route: string, fn: HandleFunction): Server; + + /** + * Handle server requests, punting them down + * the middleware stack. + * + * @private + */ + handle(req: http.IncomingMessage, res: http.ServerResponse, next: Function): void; + + + /** + * Listen for connections. + * + * This method takes the same arguments + * as node's `http.Server#listen()`. + * + * HTTP and HTTPS: + * + * If you run your application both as HTTP + * and HTTPS you may wrap them individually, + * since your Connect "server" is really just + * a JavaScript `Function`. + * + * var connect = require('connect') + * , http = require('http') + * , https = require('https'); + * + * var app = connect(); + * + * http.createServer(app).listen(80); + * https.createServer(options, app).listen(443); + * + * @api public + */ + listen(port: number, hostname?: string, backlog?: number, callback?: Function): http.Server; + listen(port: number, hostname?: string, callback?: Function): http.Server; + listen(path: string, callback?: Function): http.Server; + listen(handle: any, listeningListener?: Function): http.Server; + } + } + + export = createServer; +} From d90b63ede980d5b3dd5962f6881b8b1a0b8286d9 Mon Sep 17 00:00:00 2001 From: Maxime LUCE Date: Thu, 22 Oct 2015 00:50:07 +0200 Subject: [PATCH 2/4] Add tests for connect typings --- connect/connect-tests.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 connect/connect-tests.ts diff --git a/connect/connect-tests.ts b/connect/connect-tests.ts new file mode 100644 index 0000000000..c368a3b93d --- /dev/null +++ b/connect/connect-tests.ts @@ -0,0 +1,32 @@ +/// + +import * as http from "http"; +import * as connect from "connect"; + +const app = connect(); + +// log all requests +app.use((req, res, next) => { + console.log(req, res); + next(); +}); + +// Stop on errors +app.use((err, req, res, next) => { + if (err) { + return res.end(`Error: ${err}`); + } + + next(); +}); + +// respond to all requests +app.use((req, res) => { + res.end("Hello from Connect!\n"); +}); + +//create node.js http server and listen on port +http.createServer(app).listen(3000); + +//create node.js http server and listen on port using connect shortcut +app.listen(3000); From 962ede5f1cc9b5f2dd54b2ea53d20363afd92bb3 Mon Sep 17 00:00:00 2001 From: Maxime LUCE Date: Thu, 22 Oct 2015 00:52:14 +0200 Subject: [PATCH 3/4] Fix HandleFunction typing issue --- connect/connect.d.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/connect/connect.d.ts b/connect/connect.d.ts index 5485d1d81a..a61cd9281e 100644 --- a/connect/connect.d.ts +++ b/connect/connect.d.ts @@ -16,8 +16,13 @@ declare module "connect" { module createServer { export type ServerHandle = HandleFunction | http.Server; - export type HandleFunction = (req: http.IncomingMessage, res: http.ServerResponse, next: Function) => void; - + + export interface HandleFunction { + (req: http.IncomingMessage, res: http.ServerResponse): void; + (req: http.IncomingMessage, res: http.ServerResponse, next: Function): void; + (err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: Function): void; + } + export interface ServerStackItem { route: string; handle: ServerHandle; @@ -25,10 +30,10 @@ declare module "connect" { export interface Server extends NodeJS.EventEmitter { (req: http.IncomingMessage, res: http.ServerResponse, next: Function) => void; - + route: string; stack: ServerStackItem[]; - + /** * Utilize the given middleware `handle` to the given `route`, * defaulting to _/_. This "route" is the mount-point for the @@ -44,7 +49,7 @@ declare module "connect" { */ use(fn: HandleFunction): Server; use(route: string, fn: HandleFunction): Server; - + /** * Handle server requests, punting them down * the middleware stack. @@ -52,8 +57,7 @@ declare module "connect" { * @private */ handle(req: http.IncomingMessage, res: http.ServerResponse, next: Function): void; - - + /** * Listen for connections. * From 9d4364560ec6422b18c9b86735413ad04bf5b93c Mon Sep 17 00:00:00 2001 From: Maxime LUCE Date: Thu, 22 Oct 2015 01:08:06 +0200 Subject: [PATCH 4/4] Fix typings issues --- connect/connect-tests.ts | 6 +++--- connect/connect.d.ts | 15 +++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/connect/connect-tests.ts b/connect/connect-tests.ts index c368a3b93d..e8665f8f91 100644 --- a/connect/connect-tests.ts +++ b/connect/connect-tests.ts @@ -6,13 +6,13 @@ import * as connect from "connect"; const app = connect(); // log all requests -app.use((req, res, next) => { +app.use((req: http.IncomingMessage, res: http.ServerResponse, next: Function) => { console.log(req, res); next(); }); // Stop on errors -app.use((err, req, res, next) => { +app.use((err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: Function) => { if (err) { return res.end(`Error: ${err}`); } @@ -21,7 +21,7 @@ app.use((err, req, res, next) => { }); // respond to all requests -app.use((req, res) => { +app.use((req: http.IncomingMessage, res: http.ServerResponse) => { res.end("Hello from Connect!\n"); }); diff --git a/connect/connect.d.ts b/connect/connect.d.ts index a61cd9281e..575a341b60 100644 --- a/connect/connect.d.ts +++ b/connect/connect.d.ts @@ -1,4 +1,4 @@ -// Type definitions for connect v3.4.0. +// Type definitions for connect v3.4.0 // Project: https://github.com/senchalabs/connect // Definitions by: Maxime LUCE // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -16,12 +16,11 @@ declare module "connect" { module createServer { export type ServerHandle = HandleFunction | http.Server; - - export interface HandleFunction { - (req: http.IncomingMessage, res: http.ServerResponse): void; - (req: http.IncomingMessage, res: http.ServerResponse, next: Function): void; - (err: Error, req: http.IncomingMessage, res: http.ServerResponse, next: Function): 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 HandleFunction = SimpleHandleFunction | NextHandleFunction | ErrorHandleFunction; export interface ServerStackItem { route: string; @@ -29,7 +28,7 @@ declare module "connect" { } export interface Server extends NodeJS.EventEmitter { - (req: http.IncomingMessage, res: http.ServerResponse, next: Function) => void; + (req: http.IncomingMessage, res: http.ServerResponse, next?: Function): void; route: string; stack: ServerStackItem[];