[on-headers] improve typings, enable strict null checks & linting

This commit is contained in:
Dimitri Benin 2017-08-15 09:51:52 +02:00
parent d4f42ab007
commit c02474e3d9
4 changed files with 19 additions and 29 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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"
]
}
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }