[Fork] The Ultimate Javascript Content-Type utility Written In ES6 And Optimised With JavaScript Compiler.
npm install @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).
``sh`
yarn add @goa/mime-types
- Table Of Contents
- API
- lookup(path: string): string
- contentType(type: string): string
- extension(type: string): string
- charset(type: string): string
- types
- extensions
- Copyright
The package is available by importing its default function:
`js`
import { lookup, contentType, extension, charset, types } from '@goa/mime-types'
All functions return false if input is invalid or not found.
path: string,
): stringLookup the content-type associated with a file.
`js
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
type: string,
): stringCreate 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.
`js
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
type: string,
): stringGet the default extension for a content-type.
`js
import { extension } from '@goa/mime-types'
console.log(extension('application/octet-stream'))
```
bin
type: string,
): stringLookup the implied default charset of a content-type.
`js
import { charset } from '@goa/mime-types'
console.log(charset('text/markdown'))
```
UTF-8
A map of content-types by extension.
`js
import { types } from '@goa/mime-types'
console.log(types['json'])
```
application/json
A map of extensions by content-type.
`js
import { extensions } from '@goa/mime-types'
console.log(extensions['application/mp4'])
````
[ 'mp4s', 'm4p' ]
Original Work by Jonathan Ong, Douglas Christopher Wilson.
---
![]() | © Art Deco for Idio 2019 | alt="Tech Nation Visa" /> | Tech Nation Visa Sucks |
|---|