From c02474e3d92855ddfa2fcaa794a4e8cbceacf405 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Tue, 15 Aug 2017 09:51:52 +0200 Subject: [PATCH] [on-headers] improve typings, enable strict null checks & linting --- types/on-headers/index.d.ts | 16 +++++----------- types/on-headers/on-headers-tests.ts | 25 ++++++++++--------------- types/on-headers/tsconfig.json | 6 +++--- types/on-headers/tslint.json | 1 + 4 files changed, 19 insertions(+), 29 deletions(-) create mode 100644 types/on-headers/tslint.json diff --git a/types/on-headers/index.d.ts b/types/on-headers/index.d.ts index ec76911be0..b5af9ee6d4 100644 --- a/types/on-headers/index.d.ts +++ b/types/on-headers/index.d.ts @@ -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 +// BendingBender // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// - -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; diff --git a/types/on-headers/on-headers-tests.ts b/types/on-headers/on-headers-tests.ts index a69f404afe..f2aa88cb69 100644 --- a/types/on-headers/on-headers-tests.ts +++ b/types/on-headers/on-headers-tests.ts @@ -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); diff --git a/types/on-headers/tsconfig.json b/types/on-headers/tsconfig.json index 0426c92551..815122ee4e 100644 --- a/types/on-headers/tsconfig.json +++ b/types/on-headers/tsconfig.json @@ -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" ] -} \ No newline at end of file +} diff --git a/types/on-headers/tslint.json b/types/on-headers/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/on-headers/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }