mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Add definitions for on-headers (github/jshttp/on-headers)
This commit is contained in:
parent
2520bce9a8
commit
daaabe797e
20
on-headers/on-headers-tests.ts
Normal file
20
on-headers/on-headers-tests.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/// <reference path="on-headers.d.ts"/>
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
33
on-headers/on-headers.d.ts
vendored
Normal file
33
on-headers/on-headers.d.ts
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
// Type definitions for serve-favicon 2.1.6
|
||||
// Project: https://github.com/jshttp/on-headers
|
||||
// Definitions by: John Jeffery <https://github.com/jjeffery/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user