media-typer is a focused JavaScript utility designed for strictly parsing and formatting media types according to RFC 6838. It enables developers to deconstruct media type strings (e.g., "image/svg+xml") into their `type`, `subtype`, and optional `suffix` components, and conversely, to construct canonical media type strings from these parts. A key differentiator of this package is its explicit design choice to *not* handle media type parameters (such as `charset=utf-8`) or to parse full HTTP `Content-Type` headers, directing users to the `content-type` module for such broader parsing needs. The current stable version is 1.1.0. While releases are infrequent, this indicates a mature, stable, and narrowly scoped utility that receives updates primarily for maintenance or minor feature enhancements like the `test()` function.
npm install media-typerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse, format, and validate media type strings, showcasing the core API and clarifying its scope regarding media type parameters.
Ensure your Node.js environment is `0.8` or newer. If you need to handle parameters or parse from HTTP request/response objects, use the `content-type` package instead. Refactor any direct `parse(req)` or `parse(res)` calls to first extract the Content-Type header string.
If your use case requires parsing media types including their parameters, use the `content-type` package (e.g., `require('content-type').parse('text/html; charset=utf-8')`). `media-typer` is intended for the base media type string only.For parsing full `Content-Type` headers from HTTP requests/responses, including parameters and handling potential invalid formats robustly, use the dedicated `content-type` package. It provides methods like `contentType.parse(req)` or `contentType.parse(res)` for this purpose.
Ensure the input to `typer.parse()` is a valid 'type/subtype' or 'type/subtype+suffix' string without parameters. For `typer.format()`, ensure the object has `type` and `subtype` properties, and an optional `suffix` property, with valid string values.
For CommonJS, use `const typer = require('media-typer')` and then access `typer.parse`. In an ESM context, use `import * as typer from 'media-typer'` or `import typer from 'media-typer'` (depending on your build setup and Node.js version's CJS interop) and access as `typer.parse`.No dependency data recorded yet.