mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-06 13:11:46 +00:00
[type-is] add typings (#18950)
This commit is contained in:
committed by
Mohamed Hegazy
parent
2bf813a3d1
commit
d4f42ab007
Vendored
+18
@@ -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;
|
||||
}
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
@@ -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;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user