Add definition for "connect-timeout".

This commit is contained in:
Cyril Schumacher
2015-12-08 10:24:22 +01:00
parent a961bfca17
commit 5111e01478
2 changed files with 64 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
/// <reference path="connect-timeout.d.ts" />
/// <reference path="../body-parser/body-parser.d.ts" />
/// <reference path="../cookie-parser/cookie-parser.d.ts" />
/// <reference path="../express/express.d.ts" />
import express = require("express");
import timeout = require("connect-timeout");
import bodyParser = require("body-parser");
import cookieParser = require("cookie-parser");
// example of using this top-level; note the use of haltOnTimedout
// after every middleware; it will stop the request flow on a timeout
var app = express();
app.use(timeout("5s", { respond: false }));
app.use(bodyParser());
app.use(haltOnTimedout);
app.use(cookieParser());
app.use(haltOnTimedout);
// Add your routes here, etc.
function haltOnTimedout(req, res, next) {
if (!req.timedout) {
next();
}
}
app.listen(3000);
+36
View File
@@ -0,0 +1,36 @@
// Type definitions for connect-timeout
// Project: https://github.com/expressjs/timeout
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../express/express.d.ts" />
declare module Express {
export interface Request {
/**
* @summary Clears the timeout on the request.
*/
clearTimeout(): void;
/**
*
* @return {boolean} true if timeout fired; false otherwise.
*/
timedout(event: string, message: string): boolean;
}
}
declare module "connect-timeout" {
import express = require("express");
interface TimeoutOptions extends Object {
/**
* @summary Controls if this module will "respond" in the form of forwarding an error.
* @type {boolean}
*/
respond: boolean;
}
function timeout(timeout: string, options?: TimeoutOptions): express.RequestHandler;
export = timeout;
}