UNPKG

4.27 kBMarkdownView Raw
1<!--
2 -- This file is auto-generated from src/README_js.md. Changes should be made there.
3 -->
4# Mime
5
6[![NPM downloads](https://img.shields.io/npm/dm/mime)](https://www.npmjs.com/package/mime)
7[![Mime CI](https://github.com/broofa/mime/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/broofa/mime/actions/workflows/ci.yml?query=branch%3Amain)
8
9An API for MIME type information.
10
11- All `mime-db` types
12- Compact and dependency-free [![mime's badge](https://deno.bundlejs.com/?q=mime&badge)](https://bundlejs.com/?q=mime)
13- Full TS support
14
15
16> **Important**
17> `mime@4` is currently in **beta**. Changes include:
18> * ESM module support is required. See the [ESM Module FAQ](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
19> * [ES2020](https://caniuse.com/?search=es2020) support is required
20> * Typescript types are built-in
21>
22> To install: ` npm install mime@beta`
23## Install
24
25```bash
26npm install mime
27```
28
29## Quick Start
30
31For the full version (800+ MIME types, 1,000+ extensions):
32
33```javascript
34import mime from 'mime';
35
36mime.getType('txt'); // ⇨ 'text/plain'
37mime.getExtension('text/plain'); // ⇨ 'txt'
38```
39
40### Lite Version [![mime/lite's badge](https://deno.bundlejs.com/?q=mime/lite&badge)](https://bundlejs.com/?q=mime/lite)
41
42`mime/lite` is a drop-in `mime` replacement, stripped of unofficial ("`prs.*`", "`x-*`", "`vnd.*`") types:
43
44```javascript
45import mime from 'mime/lite';
46```
47
48## API
49
50### `mime.getType(pathOrExtension)`
51
52Get mime type for the given file path or extension. E.g.
53
54```javascript
55mime.getType('js'); // ⇨ 'application/javascript'
56mime.getType('json'); // ⇨ 'application/json'
57
58mime.getType('txt'); // ⇨ 'text/plain'
59mime.getType('dir/text.txt'); // ⇨ 'text/plain'
60mime.getType('dir\\text.txt'); // ⇨ 'text/plain'
61mime.getType('.text.txt'); // ⇨ 'text/plain'
62mime.getType('.txt'); // ⇨ 'text/plain'
63```
64
65`null` is returned in cases where an extension is not detected or recognized
66
67```javascript
68mime.getType('foo/txt'); // ⇨ null
69mime.getType('bogus_type'); // ⇨ null
70```
71
72### `mime.getExtension(type)`
73
74Get file extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.
75
76```javascript
77mime.getExtension('text/plain'); // ⇨ 'txt'
78mime.getExtension('application/json'); // ⇨ 'json'
79mime.getExtension('text/html; charset=utf8'); // ⇨ 'html'
80```
81
82### `mime.getAllExtensions(type)`
83
84> **Note**
85> New in `mime@4`
86
87Get all file extensions for the given mime type.
88
89```javascript --run default
90mime.getAllExtensions('image/jpeg'); // ⇨ Set(3) { 'jpeg', 'jpg', 'jpe' }
91```
92
93## Custom `Mime` instances
94
95The default objects exported by `mime` are immutable by design. Mutable versions can be created as follows...
96### new Mime(type map [, type map, ...])
97
98Create a new, custom mime instance. For example, to create a mutable version of the default `mime` instance:
99
100```javascript
101import { Mime } from 'mime/lite';
102
103import standardTypes from 'mime/types/standard.js';
104import otherTypes from 'mime/types/other.js';
105
106const mime = new Mime(standardTypes, otherTypes);
107```
108
109Each argument is passed to the `define()` method, below. For example `new Mime(standardTypes, otherTypes)` is synonomous with `new Mime().define(standardTypes).define(otherTypes)`
110
111### `mime.define(type map [, force = false])`
112
113> **Note**
114> Only available on custom `Mime` instances
115
116Define MIME type -> extensions.
117
118Attempting to map a type to an already-defined extension will `throw` unless the `force` argument is set to `true`.
119
120```javascript
121mime.define({'text/x-abc': ['abc', 'abcd']});
122
123mime.getType('abcd'); // ⇨ 'text/x-abc'
124mime.getExtension('text/x-abc') // ⇨ 'abc'
125```
126
127## Command Line
128
129### Extension -> type
130
131 $ mime scripts/jquery.js
132 application/javascript
133
134### Type -> extension
135
136 $ npx mime -r image/jpeg
137 jpeg
138
139----
140Markdown generated from [src/README_js.md](src/README_js.md) by [<img height="13" src="https://camo.githubusercontent.com/5c7c603cd1e6a43370b0a5063d457e0dabb74cf317adc7baba183acb686ee8d0/687474703a2f2f692e696d6775722e636f6d2f634a4b6f3662552e706e67" />](https://github.com/broofa/runmd)