The ultimate javascript content-type utility, written entirely in TypeScript
This package has no dependencies and uses no node.js specific APIs. This works in any JavaScript runtime, including Node.js and the Browser.
You can install this via NPM:
$ npm install @alshdavid/mime-types
All mime types are based on an object containing all the mime types (known as the mime-db), feel free to open a PR to expand the list of mime types.
This package offers imports for CommonJS, ES Modules. Node will automatically select the import type relevant for it. Bundlers like Parcel, Webpack, Rollup will likewise consume the package via ES Modules.
var mime = require('@alshdavid/mime-types')
import * as mime from '@alshdavid/mime-types'
All functions return a tuple (JavaScript array)
type Result = [result: string, found: boolean]
e.g.
const [ result, found ] = mime.lookup('json')
Lookup the content-type associated with a file.
mime.lookup('json') // ['application/json', true]
mime.lookup('.md') // ['text/markdown', true]
mime.lookup('file.html') // ['text/html', true]
mime.lookup('folder/file.js') // ['application/javascript', true]
mime.lookup('folder/.htaccess') // ['', false]
mime.lookup('cats') // ['', false]
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.
mime.contentType('markdown') // ['text/x-markdown; charset=utf-8', true]
mime.contentType('file.json') // ['application/json; charset=utf-8', true]
mime.contentType('text/html') // ['text/html; charset=utf-8', true]
mime.contentType('text/html; charset=iso-8859-1') // ['text/html; charset=iso-8859-1', true]
// from a full path
mime.contentType(path.extname('/path/to/file.json')) // ['application/json; charset=utf-8', true]
Get the default extension for a content-type.
mime.extension('application/octet-stream') // ['bin', true]
Lookup the implied default charset of a content-type.
mime.charset('text/markdown') // ['UTF-8', true]
If you wish to manually instantiate the mime-type parser, you can do so like this:
import { MimeTypesParser } from '@alshdavid/mime-types'
const mimeTypesParser = new MimeTypesParser()
// Methods are all identical to available functions
mimeTypesParser.lookup('file.html')
mimeTypesParser.contentType('text/html')
mimeTypesParser.extension('application/octet-stream')
mimeTypesParser.charset('text/markdown')
import { MimeTypesParser, mimeDatabase, MimeDatabase } from '@alshdavid/mime-types'
const customMimeDatabase: MimeDatabase = {
"application/my-custom-type": {}
}
const mimeTypesParser = new MimeTypesParser({
database: { ...mimeDatabase, ...customMimeDatabase }
})