Skip to main content
Home

Utility functions for media types (MIME types)

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
94%
Published
a year ago (1.1.0)
function contentType
contentType<T extends (string & { }) | KnownExtensionOrType>(extensionOrType: T): Lowercase<T> extends KnownExtensionOrType ? string : string | undefined

Returns the full Content-Type or Content-Disposition header value for the given extension or media type.

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.

Examples

Usage

import { contentType } from "@std/media-types/content-type";
import { assertEquals } from "@std/assert";

assertEquals(contentType(".json"), "application/json; charset=UTF-8");
assertEquals(contentType("text/html"), "text/html; charset=UTF-8");
assertEquals(contentType("text/html; charset=UTF-8"), "text/html; charset=UTF-8");
assertEquals(contentType("txt"), "text/plain; charset=UTF-8");
assertEquals(contentType("foo"), undefined);
assertEquals(contentType("file.json"), undefined);

Type Parameters

T extends (string & { }) | KnownExtensionOrType

Type of the extension or media type to resolve.

Parameters

extensionOrType: T

The extension or media type to resolve.

Return Type

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

The full Content-Type or Content-Disposition header value, or undefined if unable to resolve the media type.

New Ticket: 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";

Add Package

pnpm i jsr:@std/media-types
or (using pnpm 10.8 or older)
pnpm dlx jsr add @std/media-types

Import symbol

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

Add Package

yarn add jsr:@std/media-types
or (using Yarn 4.8 or older)
yarn dlx jsr add @std/media-types

Import symbol

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

Add Package

vlt install jsr:@std/media-types

Import symbol

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

Add Package

npx jsr add @std/media-types

Import symbol

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

Add Package

bunx jsr add @std/media-types

Import symbol

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