mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-05 14:59:37 +08:00
Merge pull request #19003 from BendingBender/content-type
[content-type] improve typings, enable strict null checks & linting
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
import contentType = require('content-type');
|
||||
import express = require('express');
|
||||
/// <reference types="node" />
|
||||
|
||||
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);
|
||||
|
||||
33
types/content-type/index.d.ts
vendored
33
types/content-type/index.d.ts
vendored
@@ -1,21 +1,26 @@
|
||||
// Type definitions for content-type 1.1
|
||||
// Project: https://www.npmjs.com/package/content-type
|
||||
// Definitions by: Hiroki Horiuchi <https://github.com/horiuchi>
|
||||
// BendingBender <https://github.com/BendingBender>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
import * as express from 'express';
|
||||
export function parse(input: RequestLike | ResponseLike | 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 RequestLike {
|
||||
headers: {[header: string]: string | string[]};
|
||||
}
|
||||
|
||||
export interface ResponseLike {
|
||||
getHeader(name: string): number | string | string[] | undefined;
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user