1 | # media-typer
|
---|
2 |
|
---|
3 | [![NPM Version][npm-image]][npm-url]
|
---|
4 | [![NPM Downloads][downloads-image]][downloads-url]
|
---|
5 | [![Node.js Version][node-version-image]][node-version-url]
|
---|
6 | [![Build Status][travis-image]][travis-url]
|
---|
7 | [![Test Coverage][coveralls-image]][coveralls-url]
|
---|
8 |
|
---|
9 | Simple RFC 6838 media type parser
|
---|
10 |
|
---|
11 | ## Installation
|
---|
12 |
|
---|
13 | ```sh
|
---|
14 | $ npm install media-typer
|
---|
15 | ```
|
---|
16 |
|
---|
17 | ## API
|
---|
18 |
|
---|
19 | ```js
|
---|
20 | var typer = require('media-typer')
|
---|
21 | ```
|
---|
22 |
|
---|
23 | ### typer.parse(string)
|
---|
24 |
|
---|
25 | ```js
|
---|
26 | var obj = typer.parse('image/svg+xml; charset=utf-8')
|
---|
27 | ```
|
---|
28 |
|
---|
29 | Parse a media type string. This will return an object with the following
|
---|
30 | properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`):
|
---|
31 |
|
---|
32 | - `type`: The type of the media type (always lower case). Example: `'image'`
|
---|
33 |
|
---|
34 | - `subtype`: The subtype of the media type (always lower case). Example: `'svg'`
|
---|
35 |
|
---|
36 | - `suffix`: The suffix of the media type (always lower case). Example: `'xml'`
|
---|
37 |
|
---|
38 | - `parameters`: An object of the parameters in the media type (name of parameter always lower case). Example: `{charset: 'utf-8'}`
|
---|
39 |
|
---|
40 | ### typer.parse(req)
|
---|
41 |
|
---|
42 | ```js
|
---|
43 | var obj = typer.parse(req)
|
---|
44 | ```
|
---|
45 |
|
---|
46 | Parse the `content-type` header from the given `req`. Short-cut for
|
---|
47 | `typer.parse(req.headers['content-type'])`.
|
---|
48 |
|
---|
49 | ### typer.parse(res)
|
---|
50 |
|
---|
51 | ```js
|
---|
52 | var obj = typer.parse(res)
|
---|
53 | ```
|
---|
54 |
|
---|
55 | Parse the `content-type` header set on the given `res`. Short-cut for
|
---|
56 | `typer.parse(res.getHeader('content-type'))`.
|
---|
57 |
|
---|
58 | ### typer.format(obj)
|
---|
59 |
|
---|
60 | ```js
|
---|
61 | var obj = typer.format({type: 'image', subtype: 'svg', suffix: 'xml'})
|
---|
62 | ```
|
---|
63 |
|
---|
64 | Format an object into a media type string. This will return a string of the
|
---|
65 | mime type for the given object. For the properties of the object, see the
|
---|
66 | documentation for `typer.parse(string)`.
|
---|
67 |
|
---|
68 | ## License
|
---|
69 |
|
---|
70 | [MIT](LICENSE)
|
---|
71 |
|
---|
72 | [npm-image]: https://img.shields.io/npm/v/media-typer.svg?style=flat
|
---|
73 | [npm-url]: https://npmjs.org/package/media-typer
|
---|
74 | [node-version-image]: https://img.shields.io/badge/node.js-%3E%3D_0.6-brightgreen.svg?style=flat
|
---|
75 | [node-version-url]: http://nodejs.org/download/
|
---|
76 | [travis-image]: https://img.shields.io/travis/jshttp/media-typer.svg?style=flat
|
---|
77 | [travis-url]: https://travis-ci.org/jshttp/media-typer
|
---|
78 | [coveralls-image]: https://img.shields.io/coveralls/jshttp/media-typer.svg?style=flat
|
---|
79 | [coveralls-url]: https://coveralls.io/r/jshttp/media-typer
|
---|
80 | [downloads-image]: https://img.shields.io/npm/dm/media-typer.svg?style=flat
|
---|
81 | [downloads-url]: https://npmjs.org/package/media-typer
|
---|