mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
[on-headers] improve typings, enable strict null checks & linting
This commit is contained in:
parent
d4f42ab007
commit
c02474e3d9
16
types/on-headers/index.d.ts
vendored
16
types/on-headers/index.d.ts
vendored
@ -1,17 +1,17 @@
|
||||
// Type definitions for serve-favicon 2.1.6
|
||||
// Type definitions for on-headers 1.0
|
||||
// Project: https://github.com/jshttp/on-headers
|
||||
// Definitions by: John Jeffery <https://github.com/jjeffery/>
|
||||
// BendingBender <https://github.com/BendingBender>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
|
||||
import http = require("http");
|
||||
import { ServerResponse } from '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
|
||||
* 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
|
||||
@ -21,12 +21,6 @@ import http = require("http");
|
||||
* @param listener Function to call prior to headers being emitted,
|
||||
* the response object is passed as this context.
|
||||
*/
|
||||
declare 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
|
||||
declare function onHeaders(res: ServerResponse, listener: (this: ServerResponse) => void): void;
|
||||
|
||||
export = onHeaders;
|
||||
|
||||
@ -1,18 +1,13 @@
|
||||
import http = require('http')
|
||||
import onHeaders = require('on-headers')
|
||||
import * as http from 'http';
|
||||
import onHeaders = require('on-headers');
|
||||
|
||||
http.createServer(onRequest)
|
||||
.listen(3000);
|
||||
http.createServer((req, res) => {
|
||||
onHeaders(res, function addPoweredBy() {
|
||||
if (!this.getHeader('X-Powered-By')) {
|
||||
this.setHeader('X-Powered-By', 'Node.js');
|
||||
}
|
||||
});
|
||||
|
||||
function onRequest(req: http.IncomingMessage, res: http.ServerResponse) {
|
||||
onHeaders(res, addPoweredBy);
|
||||
res.setHeader('Content-Type', 'text/plain')
|
||||
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');
|
||||
}
|
||||
}
|
||||
}).listen(3000);
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": false,
|
||||
"strictNullChecks": false,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
@ -19,4 +19,4 @@
|
||||
"index.d.ts",
|
||||
"on-headers-tests.ts"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
1
types/on-headers/tslint.json
Normal file
1
types/on-headers/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user