From daaabe797ecb2e54f03fc9742011a11cd1965413 Mon Sep 17 00:00:00 2001 From: John Jeffery Date: Fri, 3 Apr 2015 20:00:02 +1000 Subject: [PATCH] Add definitions for on-headers (github/jshttp/on-headers) --- on-headers/on-headers-tests.ts | 20 ++++++++++++++++++++ on-headers/on-headers.d.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 on-headers/on-headers-tests.ts create mode 100644 on-headers/on-headers.d.ts diff --git a/on-headers/on-headers-tests.ts b/on-headers/on-headers-tests.ts new file mode 100644 index 0000000000..11f33c4ef0 --- /dev/null +++ b/on-headers/on-headers-tests.ts @@ -0,0 +1,20 @@ +/// + +import http = require('http') +import onHeaders = require('on-headers') + +http.createServer(onRequest) + .listen(3000); + +function onRequest(req: http.ServerRequest, res: http.ServerResponse) { + onHeaders(res, addPoweredBy); + res.setHeader('Content-Type', 'text/plain') + res.end('hello!'); +} + +function addPoweredBy(): void { + // set if not set by end of request + if (!this.getHeader('X-Powered-By')) { + this.setHeader('X-Powered-By', 'Node.js'); + } +} diff --git a/on-headers/on-headers.d.ts b/on-headers/on-headers.d.ts new file mode 100644 index 0000000000..bf873fd826 --- /dev/null +++ b/on-headers/on-headers.d.ts @@ -0,0 +1,33 @@ +// Type definitions for serve-favicon 2.1.6 +// Project: https://github.com/jshttp/on-headers +// Definitions by: John Jeffery +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "on-headers" { + import http = require("http"); + + /** + * This will add the listener to fire when headers are emitted for res. + * The listener is passed the response object as its context (this). + * Headers are considered emitted only once, right before they + * are sent to the client. + * + * When this is called multiple times on the same res, the listeners + * are fired in the reverse order they were added. + * + * @param res HTTP server response object + * @param listener Function to call prior to headers being emitted, + * the response object is passed as this context. + */ + function onHeaders(res: http.ServerResponse, listener: Function):void; + + // Note that this definition might be able to be improved in a future + // version of typescript. At the moment it is not possible to declare + // the type of the 'this' context for a function, but it might be included + // in a future typescript version. + // https://github.com/Microsoft/TypeScript/issues/229 + + export = onHeaders; +} \ No newline at end of file