diff --git a/response-time/index.d.ts b/response-time/index.d.ts index 8ab91c86ec..5f3e86e6bb 100644 --- a/response-time/index.d.ts +++ b/response-time/index.d.ts @@ -1,8 +1,9 @@ -// Type definitions for response-time 2.2.0 +// Type definitions for response-time 2.3.2 // Project: https://github.com/expressjs/response-time -// Definitions by: Uros Smolnik +// Definitions by: Uros Smolnik , TonyYang // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + /* =================== USAGE =================== import responseTime = require('response-time'); @@ -11,27 +12,32 @@ =============================================== */ +/// -import express = require('express'); +import http = require("http"); + + +export = responseTime; /** * Response time header for node.js * Returns middleware that adds a X-Response-Time header to responses. */ -declare function responseTime(options?: { - /** - * The fixed number of digits to include in the output, which is always in milliseconds, defaults to 3 (ex: 2.300ms). - */ - digits?: number; - /** - * The name of the header to set, defaults to X-Response-Time. - */ - header?: string; - /** - * Boolean to indicate if units of measurement suffix should be added to the output, defaults to true (ex: 2.300ms vs 2.300). - */ - suffix?: boolean; -}): express.RequestHandler; +declare function responseTime(options?: responseTime.ResponseTimeOptions): + (request: http.IncomingMessage, response: http.ServerResponse, callback: (err: any) => void) => any; +declare function responseTime(fn: responseTime.ResponseTimeFunction): + (request: http.IncomingMessage, response: http.ServerResponse, callback: (err: any) => void) => any; -export = responseTime; + +declare namespace responseTime { + export interface ResponseTimeOptions { + digits?: number; + header?: string; + suffix?: boolean; + } + + export interface ResponseTimeFunction { + (request: http.IncomingMessage, response: http.ServerResponse, time: number ): any; + } +} diff --git a/response-time/response-time-tests.ts b/response-time/response-time-tests.ts index 8c5e9595dd..b6a9b4a2af 100644 --- a/response-time/response-time-tests.ts +++ b/response-time/response-time-tests.ts @@ -1,11 +1,41 @@ - -import express = require('express'); import responseTime = require('response-time'); -var app = express(); -app.use(responseTime()); -app.use(responseTime({ - digits: 3, - header: 'X-Response-Time', - suffix: true -})); + +//////////////////////////////////////////////////////////////////////////////////// +// expressconnect tests https://github.com/expressjs/response-time#expressconnect // +//////////////////////////////////////////////////////////////////////////////////// +import express = require('express') +namespace express_connect_tests { + const app = express() + app.use(responseTime()) +} + + +////////////////////////////////////////////////////////////////////////////////////////////// +// vanilla http server tests https://github.com/expressjs/response-time#vanilla-http-server // +////////////////////////////////////////////////////////////////////////////////////////////// +import http = require('http') +namespace vanilla_http_server_tests { + // create "middleware" + var _responseTime = responseTime() + http.createServer(function (req, res) { + _responseTime(req, res, function (err) { + if (err) return console.log(err); + + // respond to request + res.setHeader('content-type', 'text/plain') + res.end('hello, world!') + }) + }) +} + + +////////////////////////////////////////////////////////////////////////////////////////////////// +// response time metrics tests https://github.com/expressjs/response-time#response-time-metrics // +////////////////////////////////////////////////////////////////////////////////////////////////// +namespace response_time_metrics_tests { + const app = express() + app.use(responseTime(function (req, res, time) { + let num: number = time; + })); +}