From 791bfc6d506c08731948117150c03bd796ca0110 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Wed, 16 Aug 2017 02:39:28 +0200 Subject: [PATCH 1/2] [content-type] improve typings, enable strict null checks & linting --- types/content-type/content-type-tests.ts | 29 +++++++++++---------- types/content-type/index.d.ts | 33 ++++++++++++++---------- types/content-type/tsconfig.json | 4 +-- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/types/content-type/content-type-tests.ts b/types/content-type/content-type-tests.ts index 0705d440c1..4be9519a94 100644 --- a/types/content-type/content-type-tests.ts +++ b/types/content-type/content-type-tests.ts @@ -1,18 +1,19 @@ -import contentType = require('content-type'); -import express = require('express'); +/// -let obj = contentType.parse('image/svg+xml; charset=utf-8'); +import * as contentType from 'content-type'; +import * as http from 'http'; -console.log(obj.type); // => 'image/svg+xml' -console.log(obj.parameters.charset); // => 'utf-8' +const mediaType = contentType.parse('image/svg+xml; charset=utf-8'); +mediaType; // $ExpectType ParsedMediaType +mediaType.type; // $ExpectType string +mediaType.parameters; // $ExpectType { [key: string]: string; } -let req: express.Request; -obj = contentType.parse(req); +http.createServer((req, res) => { + contentType.parse(req); + contentType.parse(res); +}); -let res: express.Response; -obj = contentType.parse(res); - -let str: string = contentType.format({type: 'image/svg+xml'}); - -let media: contentType.MediaType; -contentType.format(media); +// $ExpectType string +contentType.format({type: 'image/svg+xml'}); +contentType.format({type: 'image/svg+xml', parameters: {charset: 'utf-8'}}); +contentType.format(mediaType); diff --git a/types/content-type/index.d.ts b/types/content-type/index.d.ts index ec60de446f..3842dd3a47 100644 --- a/types/content-type/index.d.ts +++ b/types/content-type/index.d.ts @@ -1,21 +1,26 @@ // Type definitions for content-type 1.1 // Project: https://www.npmjs.com/package/content-type // Definitions by: Hiroki Horiuchi +// BendingBender // Definitions: https://github.com/borisyankov/DefinitelyTyped -import * as express from 'express'; +export function parse(input: ReqLike | ResLike | string): ParsedMediaType; +export function format(obj: MediaType): string; -declare var ct: ct.StaticFunctions; -export = ct; - -declare namespace ct { - interface StaticFunctions { - parse(input: express.Request | express.Response | string): MediaType; - format(obj: MediaType): string; - } - - interface MediaType { - type: string; - parameters?: any; - } +export interface ParsedMediaType { + type: string; + parameters: {[key: string]: string}; +} + +export interface MediaType { + type: string; + parameters?: {[key: string]: string}; +} + +export interface ReqLike { + headers: {[header: string]: string | string[]}; +} + +export interface ResLike { + getHeader(name: string): number | string | string[] | undefined; } diff --git a/types/content-type/tsconfig.json b/types/content-type/tsconfig.json index 4eaa2f42f4..3361989225 100644 --- a/types/content-type/tsconfig.json +++ b/types/content-type/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -19,4 +19,4 @@ "index.d.ts", "content-type-tests.ts" ] -} \ No newline at end of file +} From a18173b40c26f85ed6b146039f8982c85fe57105 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Thu, 17 Aug 2017 11:32:00 +0200 Subject: [PATCH 2/2] [content-type] rename interfaces --- types/content-type/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/content-type/index.d.ts b/types/content-type/index.d.ts index 3842dd3a47..5ff4ae7ff1 100644 --- a/types/content-type/index.d.ts +++ b/types/content-type/index.d.ts @@ -4,7 +4,7 @@ // BendingBender // Definitions: https://github.com/borisyankov/DefinitelyTyped -export function parse(input: ReqLike | ResLike | string): ParsedMediaType; +export function parse(input: RequestLike | ResponseLike | string): ParsedMediaType; export function format(obj: MediaType): string; export interface ParsedMediaType { @@ -17,10 +17,10 @@ export interface MediaType { parameters?: {[key: string]: string}; } -export interface ReqLike { +export interface RequestLike { headers: {[header: string]: string | string[]}; } -export interface ResLike { +export interface ResponseLike { getHeader(name: string): number | string | string[] | undefined; }