@goa/mime-types
@goa/mime-types is the Fork of The Ultimate Javascript Content-Type utility Written In ES6 And Optimised With JavaScript Compiler. Depends on the mime-db which was not bundled to allow direct upgrades of the database.
The original module has been updated to be used in @goa/koa: Koa web server compiled with Google Closure Compiler using Depack into a single file library (0 dependencies).
yarn add @goa/mime-typesTable Of Contents
- Table Of Contents
- API
lookup(path: string): stringcontentType(type: string): stringextension(type: string): stringcharset(type: string): stringtypesextensions- Copyright
API
The package is available by importing its default function:
import { lookup, contentType, extension, charset, types } from '@goa/mime-types'All functions return false if input is invalid or not found.
lookup(
path: string,
): string
Lookup the content-type associated with a file.
import { lookup } from '@goa/mime-types'
console.log(lookup('json'))
console.log(lookup('.md'))
console.log(lookup('file.html'))
console.log(lookup('folder/file.js'))
console.log(lookup('folder/.htaccess'))
console.log(lookup('cats'))application/json
text/markdown
text/html
application/javascript
false
false
contentType(
type: string,
): string
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.
import { contentType } from '@goa/mime-types'
import { extname } from 'path'
console.log(contentType('markdown'))
console.log(contentType('file.json'))
console.log(contentType('text/html'))
console.log(contentType('text/html; charset=iso-8859-1'))
// from a full path
console.log(contentType(extname('/path/to/file.json')))text/markdown; charset=utf-8
application/json; charset=utf-8
text/html; charset=utf-8
text/html; charset=iso-8859-1
application/json; charset=utf-8
extension(
type: string,
): string
Get the default extension for a content-type.
import { extension } from '@goa/mime-types'
console.log(extension('application/octet-stream'))bin
charset(
type: string,
): string
Lookup the implied default charset of a content-type.
import { charset } from '@goa/mime-types'
console.log(charset('text/markdown'))UTF-8
types
A map of content-types by extension.
import { types } from '@goa/mime-types'
console.log(types['json'])application/jsonextensions
A map of extensions by content-type.
import { extensions } from '@goa/mime-types'
console.log(extensions['application/mp4'])[ 'mp4s', 'm4p' ]
Copyright
Original Work by Jonathan Ong, Douglas Christopher Wilson.
|
© Art Deco for Idio 2019 |
|
|
Tech Nation Visa Sucks |
|---|