Skip to main content

latest
Works with
It is unknown whether this package works with Node.js, Deno, Browsers, Cloudflare Workers, Bun
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Published2 years ago (0.215.0)
function contentType
contentType<T extends (string & { }) | KnownExtensionOrType>(extensionOrType: T): Lowercase<T> extends KnownExtensionOrType ? string : string | undefined

Given 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-types was 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, use extname() from std/path/mod.ts to determine the extension and pass it here.

Examples

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

Parameters

extensionOrType: T

Return Type

Lowercase<T> extends KnownExtensionOrType ? string : string | undefined

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/media-types

Import symbol

import { contentType } from "@std/media-types";
or

Import directly with a jsr specifier

import { contentType } from "jsr:@std/media-types";