summaryrefslogtreecommitdiff
path: root/std/media_types/README.md
blob: 481c56b849a3ee0084493d1acf0e771c0e819a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# media_types

A module that assists in resolving media types and extensions. It consumes the
[mime-db](https://github.com/jshttp/mime-db) and provides API access to the
information.

## Usage

### `lookup(path)`

Lookup the content type associated with a file. The path can be just the
extension or the full path name. If the content type cannot be determined the
function returns `undefined`:

```ts
import { lookup } from "https://deno.land/std/media_types/mod.ts";

lookup("json"); // "application/json"
lookup(".md"); // "text/markdown"
lookup("folder/file.js"); // "application/javascript"
lookup("folder/.htaccess"); // undefined
```

### `contentType(type)`

Return a full `Content-Type` header value for a given content type or extension.
When an extension is used, `lookup()` is used to resolve the content type first.
A default charset is added if not present. The function will return `undefined`
if the content type cannot be resolved:

```ts
import { contentType } from "https://deno.land/std/media_types/mod.ts";
import * as path from "https://deno.land/std/path/mod.ts";

contentType("markdown"); // "text/markdown; charset=utf-8"
contentType("file.json"); // "application/json; charset=utf-8"
contentType("text/html"); // "text/html; charset=utf-8"
contentType("text/html; charset=iso-8859-1"); // "text/html; charset=iso-8859-1"

contentType(path.extname("/path/to/file.json")); // "application/json; charset=utf-8"
```

### `extension(type)`

Return a default extension for a given content type. If there is not an
appropriate extension, `undefined` is returned:

```ts
import { extension } from "https://deno.land/std/media_types/mod.ts";

extension("application/octet-stream"); // "bin"
```

### `charset(type)`

Lookup the implied default charset for a given content type. If the content type
cannot be resolved, `undefined` is returned:

```ts
import { charset } from "https://deno.land/std/media_types/mod.ts";

charset("text/markdown"); // "UTF-8"
```

### `extensions`

A `Map` of extensions by content type, in priority order:

```ts
import { extensions } from "https://deno.land/std/media_types/mod.ts";

extensions.get("application/javascript"); // [ "js", "mjs" ]
```

### `types`

A `Map` of content types by extension:

```ts
import { types } from "https://deno.land/std/media_types/mod.ts";

types.get("ts"); // "application/javascript"
```

---

Adapted from [mime-type](https://github.com/jshttp/mime-types).

MIT License.