1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/media_types
Vincent LE GOFF dcd01dd025 Eslint fixes (denoland/deno_std#356)
Make warnings fail
Original: 4543b563a9
2019-04-24 07:41:22 -04:00
..
db_1.38.0.json Update mime-db to 1.38.0 (denoland/deno_std#238) 2019-03-05 22:00:39 -05:00
deps.ts Update mime-db to 1.38.0 (denoland/deno_std#238) 2019-03-05 22:00:39 -05:00
mod.ts Add eslint for linting (denoland/deno_std#235) 2019-03-04 19:53:35 -05:00
README.md Replace deno.land/x/ with deno.land/std/ (denoland/deno_std#239) 2019-03-06 10:24:53 -05:00
test.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04:00

media_types

A module that assists in resolving media types and extensions. It consumes the 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:

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:

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:

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:

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:

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:

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

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

Adapted from mime-type.

MIT License.