Documentation / v1.9.0
HTTP media types without the guesswork.
content-types-lite provides a curated set of type-safe constants and focused utilities for working with Content-Type headers. It does not infer types from filenames or inspect file contents.
Installation
#Install the package
npm install content-types-liteQuick start
#Start with a constant
import contentTypes, {
JSON,
formatContentType,
isJsonLike,
matchesMediaType,
parseContentType,
} from 'content-types-lite'
JSON // 'application/json'
contentTypes.PDF // 'application/pdf'
formatContentType(JSON, { charset: 'utf-8' })
// 'application/json; charset=utf-8'const { JSON, withCharset } = require('content-types-lite')Reference
#Constants & types
JSONapplication/jsonYAMLapplication/yamlMSGPACKapplication/vnd.msgpackPROTOBUFapplication/protobufLegacy aliases for interoperability
YAML_LEGACY → application/x-yaml
MSGPACK_LEGACY → application/x-msgpack
PROTOBUF_LEGACY → application/x-protobuf
NDJSON → application/x-ndjson
Format
#Format header values safely
formatContentType(mediaType, parameters?)→ stringimport {
formatContentType,
normalizeContentType,
withCharset,
} from 'content-types-lite/format'
withCharset('text/html')
// 'text/html; charset=utf-8'
formatContentType('text/plain', { title: 'hello world' })
// 'text/plain; title="hello world"'
normalizeContentType('Text/HTML; Charset="utf-8"')
// 'text/html; charset=utf-8'Parse
#Parse strictly
parseContentType returns a normalized, immutable result. Duplicate parameters, malformed quoted strings, and invalid media types return null.parseContentType(input)→ object | nullgetMediaType(input)→ string | nullimport {
getMediaType,
parseContentType,
} from 'content-types-lite/parse'
parseContentType('text/html; charset=utf-8')
// { type: 'text/html', parameters: { charset: 'utf-8' } }
getMediaType('Text/HTML; Charset=UTF-8')
// 'text/html'
parseContentType('invalid') // nullMatch
#Match exact types and ranges
matchesMediaType(input, pattern)→ booleanimport { matchesMediaType } from 'content-types-lite/match'
matchesMediaType('image/avif', 'image/*') // true
matchesMediaType(
'application/problem+json',
'application/*+json',
) // trueGuards
#Ask useful questions
import { isJsonLike, isTextual } from 'content-types-lite/guards'
isJsonLike('application/vnd.api+json') // true
isTextual('application/yaml') // trueGuide
#Multipart form data
When sending a FormData body, do not manually set Content-Type: multipart/form-data. fetch or XMLHttpRequest must generate the boundary so it matches the encoded body.
The constant is intended for parsing, server-side generation where a boundary is supplied separately, and integrations that explicitly require the bare media type.
Scope
#What this package does not do
Constants, header parsing, formatting, matching, and validation.
Filename inference, exhaustive MIME databases, and file-content inspection.