type-is
Version:
Infer the content-type of a request.
64 lines (63 loc) • 2.38 kB
TypeScript
/*!
* type-is
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
import { ContentType } from "content-type";
/**
* Node.js HTTP request shape.
*/
export interface RequestLike {
headers: Record<string, string | string[] | undefined>;
}
/**
* Check if a request has a request body. A request with a body must either have
* `transfer-encoding` or `content-length` headers set.
*/
export declare function hasBody(req: RequestLike): boolean;
/**
* The default behavior of `lookup` handles only a few common shorthands.
*/
export declare function DEFAULT_LOOKUP(value: string): string | string[] | undefined;
export interface NormalizeOptions {
lookup?: (value: string) => string | string[] | undefined;
}
/**
* Normalize MIME type by:
*
* - If the string contains a `/`, then it is returned as the type.
* - If the string starts with `+` (so it is a `+suffix` shorthand like `+json`), then it is expanded to contain the complete wildcard notation of `*\/*+suffix`.
* - Else the string is assumed to be a file extension and the mapped media type is returned, or the original input if there is no mapping.
*/
export declare function normalize(value: string, options?: NormalizeOptions): string | string[];
/**
* Compile an expected mime type into a reusable matcher.
*/
export declare function match(expected: string): (actual: string) => boolean;
export type ParameterValue = (key: string, value: string) => string;
/**
* Normalize a parameter value for comparison.
*/
export declare function DEFAULT_PARAMETER_VALUE(key: string, value: string): string;
export interface TypeIsOptions extends NormalizeOptions {
parameterValue?: ParameterValue;
}
export declare class TypeIs {
private readonly hasParameters;
private readonly patterns;
private readonly parameterValue;
/**
* Compile a list of expected mime types into a reusable matcher.
*/
constructor(types: readonly string[], options?: TypeIsOptions);
/**
* Check whether a content type matches one of the configured types.
*/
is(value: string): string | undefined;
/**
* Check whether a request body matches one of the configured types.
*/
request(req: RequestLike): string | undefined;
contentType(contentType: Pick<ContentType, "type" | "parameters">): string | undefined;
}