[type-is] add typings (#18950)

This commit is contained in:
Dimitri Benin
2017-08-14 16:58:26 -07:00
committed by Mohamed Hegazy
parent 2bf813a3d1
commit d4f42ab007
4 changed files with 96 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
// Type definitions for type-is 1.6
// Project: https://github.com/jshttp/type-is#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
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;
}
+22
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
+55
View File
@@ -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;
}
});