Socket
Socket
Sign inDemoInstall

media-typer

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    media-typer

Simple RFC 6838 media type parser and formatter


Version published
Weekly downloads
23M
decreased by-16.17%
Maintainers
1
Install size
10.0 kB
Created
Weekly downloads
 

Package description

What is media-typer?

The media-typer npm package is a simple utility for parsing and formatting media types as per RFC 6838. It allows you to parse media type strings, extract their components, and format an object representing a media type back into a string. It is useful for handling and validating Content-Type and Accept HTTP headers.

What are media-typer's main functionalities?

Parsing media types

This feature allows you to parse a media type string into its constituent parts, such as type, subtype, and parameters. The example code demonstrates how to parse the media type 'application/json'.

const mediaTyper = require('media-typer');
const parsed = mediaTyper.parse('application/json');
console.log(parsed);

Formatting media types

This feature enables you to format an object representing a media type back into a string. The example code shows how to format an object with type 'image' and subtype 'png' into the string 'image/png'.

const mediaTyper = require('media-typer');
const formatted = mediaTyper.format({ type: 'image', subtype: 'png' });
console.log(formatted);

Validating media type names

This feature is used to validate media type strings and will throw an error if the media type is invalid. The example code attempts to parse an invalid media type and catches the error.

const mediaTyper = require('media-typer');
try {
  mediaTyper.parse('invalid/type');
} catch (e) {
  console.error('Invalid media type', e.message);
}

Other packages similar to media-typer

Readme

Source

media-typer

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Simple RFC 6838 media type parser.

This module will parse a given media type into it's component parts, like type, subtype, and suffix. A formatter is also provided to put them back together and the two can be combined to normalize media types into a canonical form.

If you are looking to parse the string that represents a media type and it's parameters in HTTP (for example, the Content-Type header), use the content-type module.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install media-typer

API

var typer = require('media-typer')

typer.parse(string)

var obj = typer.parse('image/svg+xml')

Parse a media type string. This will return an object with the following properties (examples are shown for the string 'image/svg+xml; charset=utf-8'):

  • type: The type of the media type (always lower case). Example: 'image'

  • subtype: The subtype of the media type (always lower case). Example: 'svg'

  • suffix: The suffix of the media type (always lower case). Example: 'xml'

If the given type string is invalid, then a TypeError is thrown.

typer.format(obj)

var obj = typer.format({ type: 'image', subtype: 'svg', suffix: 'xml' })

Format an object into a media type string. This will return a string of the mime type for the given object. For the properties of the object, see the documentation for typer.parse(string).

If any of the given object values are invalid, then a TypeError is thrown.

typer.test(string)

var valid = typer.test('image/svg+xml')

Validate a media type string. This will return true is the string is a well- formatted media type, or false otherwise.

License

MIT

FAQs

Last updated on 25 Apr 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc