@whykhamist/mime-types
v1.0.2
Published
The ultimate javascript content-type utility.
Downloads
27
Readme
mime-types
The ultimate javascript content-type utility.
Forked from mime-types
Written in typescript
Created MimeTypes Class
Changed function names
| old | new | | ----------- | --------------------------------- | | lookup | getMime | | contentType | getContentType | | extension | getExtension | | charset | getCharset |
New Function
Adding Types
All mime types are based on mime-db, so open a PR there if you'd like to add mime types.
API
Getting Started
Installation
$ npm i @whykhamist/mime-types
or
$ yarn add @whykhamist/mime-types
CJS
const mime = require("@whykhamist/mime-types").default;
mime.getMime(...)
OR
// const MimeTypes = require("@whykhamist/mime-types").MimeTypes;
// const MimeDb = require("@whykhamist/mime-types").MimeDb;
const { MimeTypes, MimeDb } = require("@whykhamist/mime-types");
const mime = new MimeTypes(MimeDb);
mime.getMime(...)
OR
const { types, extensions, typeSets, getMime, getMimes, getContentType, getExtension, getCharset } = require("@whykhamist/mime-types");
ESM
import mime from "@whykhamist/mime-types";
mime.getMime(...)
OR
import { MimeTypes, MimeDb } from "@whykhamist/mime-types";
const mime = new MimeTypes(MimeDb);
mime.getMime(...)
OR
const { types, extensions, typeSets, getMime, getMimes, getContentType, getExtension, getCharset } from "@whykhamist/mime-types";
Others
import db from "mime-db/db.json";
import { MimeTypes } from "@whykhamist/mime-types";
import { MimeDatabase } from "mime-db";
const mime = new MimeTypes(db as MimeDatabase);
extname
By default, the class uses regex to extract the file extension from a path string. If you want to use other function for the extraction (ex. Node.js path.extname), then create a new
MimeTypes
instance and add the function as a second paramenter
import { extname } from "path";
const mime = new MimeTypes(db, extname);
Make sure that the function takes a string (path) and returns the file extension. Must return any falsy value if no extension could be identified.
Regex used
/^.+\.([^.]+)$/
Properties
types
A map of content-types by extension.
const type = types["mp3"];
// "audio/mpeg"
typeSets
A map of array of content types by extension.
const types = typeSets["mp3"];
// [ 'audio/mp3', 'audio/mpeg' ]
extensions
A map of extensions by content-type.
const exts = extensions["text/x-c"];
// [ "c", "cc", "cxx", "cpp", "h", "hh", "dic" ]
Methods
All functions return false
if input is invalid or not found.
getMime
Lookup the content-type associated with a file.
getMime("json"); // 'application/json'
getMime(".md"); // 'text/markdown'
getMime("file.html"); // 'text/html'
getMime("folder/file.js"); // 'application/javascript'
getMime("folder/.htaccess"); // false
getMime("cats"); // false
getMimes
Find all content-types that are associated with a file.
getMimes("mp3"); // [ "audio/mpeg", "audio/mp3" ]
getMimes("path/to/file.rtf"); // [ "application/rtf", "text/rtf" ]
getMimes("c:\\path\\to\\file.bmp"); // [ "image/x-ms-bmp", "image/bmp", ]
getContentType
Create a full content-type header given a content-type or extension.
When given an extension, mime.lookup
is used to get the matching
content-type, otherwise the given content-type is used. Then if the
content-type does not already have a charset
parameter, mime.charset
is used to get the default charset and add to the returned content-type.
getContentType("markdown"); // 'text/x-markdown; charset=utf-8'
getContentType("file.json"); // 'application/json; charset=utf-8'
getContentType("text/html"); // 'text/html; charset=utf-8'
getContentType("text/html; charset=iso-8859-1"); // 'text/html; charset=iso-8859-1'
// from a full path
getContentType(path.extname("/path/to/file.json")); // 'application/json; charset=utf-8'
getExtension
Get the default extension for a content-type.
getExtension("application/octet-stream"); // 'bin'
getCharset
Lookup the implied default charset of a content-type.
getCharset("text/markdown"); // 'UTF-8'