function contentType
contentType<T extends (string & { }) | KnownExtensionOrType>(extensionOrType: T): Lowercase<T> extends KnownExtensionOrType ? string : string | undefinedGiven an extension or media type, return a full Content-Type or
Content-Disposition header value.
The function will treat the extensionOrType as a media type when it
contains a /, otherwise it will process it as an extension, with or without
the leading ..
Returns undefined if unable to resolve the media type.
Note: a side effect of
deno/x/media-typeswas that you could pass a file name (e.g.file.json) and it would return the content type. This behavior is intentionally not supported here. If you want to get an extension for a file name, useextname()fromstd/path/mod.tsto determine the extension and pass it here.
Examples
Example 1
Example 1
import { contentType } from "@std/media-types/content_type"; contentType(".json"); // "application/json; charset=UTF-8" contentType("text/html"); // "text/html; charset=UTF-8" contentType("text/html; charset=UTF-8"); // "text/html; charset=UTF-8" contentType("txt"); // "text/plain; charset=UTF-8" contentType("foo"); // undefined contentType("file.json"); // undefined
Type Parameters
T extends (string & { }) | KnownExtensionOrTypeParameters
extensionOrType: TReturn Type
Lowercase<T> extends KnownExtensionOrType ? string : string | undefined