diff --git a/types/type-is/index.d.ts b/types/type-is/index.d.ts new file mode 100644 index 0000000000..69b585e093 --- /dev/null +++ b/types/type-is/index.d.ts @@ -0,0 +1,18 @@ +// Type definitions for type-is 1.6 +// Project: https://github.com/jshttp/type-is#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +import { IncomingMessage } from 'http'; + +export = typeIs; + +declare function typeIs(request: IncomingMessage, types: string[]): string | false | null; +declare function typeIs(request: IncomingMessage, ...types: string[]): string | false | null; + +declare namespace typeIs { + function hasBody(request: IncomingMessage): boolean; + function is(mediaType: string, types: string[]): string | false; + function is(mediaType: string, ...types: string[]): string | false; +} diff --git a/types/type-is/tsconfig.json b/types/type-is/tsconfig.json new file mode 100644 index 0000000000..7bd23fd4fb --- /dev/null +++ b/types/type-is/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "type-is-tests.ts" + ] +} diff --git a/types/type-is/tslint.json b/types/type-is/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/type-is/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/type-is/type-is-tests.ts b/types/type-is/type-is-tests.ts new file mode 100644 index 0000000000..219d0c7fb4 --- /dev/null +++ b/types/type-is/type-is-tests.ts @@ -0,0 +1,55 @@ +import http = require('http'); +import typeis = require('type-is'); + +http.createServer((req, res) => { + const istext = typeis(req, ['text/*']); + res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text'); + + // $ExpectType string | false | null + typeis(req, ['json']); + typeis(req, ['application/*']); + typeis(req, ['application/json']); + typeis(req, ['html']); + typeis(req, ['html', 'json']); + // $ExpectType string | false | null + typeis(req, ...['html', 'json']); + + // $ExpectType boolean + typeis.hasBody(req); + if (typeis.hasBody(req)) { + req.on('data', chunk => {}); + } + + const mediaType = 'application/json'; + + // $ExpectType string | false + typeis.is(mediaType, ['json']); + typeis.is(mediaType, ['application/*']); + typeis.is(mediaType, ['application/json']); + typeis.is(mediaType, ['html']); + typeis.is(mediaType, ['html', 'json']); + // $ExpectType string | false + typeis.is(mediaType, ...['html', 'json']); +}); + +import express = require('express'); +const app = express(); + +app.use((req, res, next) => { + if (!typeis.hasBody(req)) { + return next(); + } + + switch (typeis(req, ['urlencoded', 'json', 'multipart'])) { + case 'urlencoded': + throw new Error('implement urlencoded body parsing'); + case 'json': + throw new Error('implement json body parsing'); + case 'multipart': + throw new Error('implement multipart body parsing'); + default: + res.statusCode = 415; + res.end(); + break; + } +});