https://github.com/idiocc/mime-types
[Fork] The Ultimate Javascript Content-Type utility Written In ES6 And Optimised With JavaScript Compiler.
https://github.com/idiocc/mime-types
Last synced: about 1 year ago
JSON representation
[Fork] The Ultimate Javascript Content-Type utility Written In ES6 And Optimised With JavaScript Compiler.
- Host: GitHub
- URL: https://github.com/idiocc/mime-types
- Owner: idiocc
- License: other
- Created: 2019-05-09T07:14:05.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-12T14:57:03.000Z (about 7 years ago)
- Last Synced: 2024-12-27T09:18:32.910Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://idio.cc
- Size: 72.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @goa/mime-types
[](https://npmjs.org/package/@goa/mime-types)
`@goa/mime-types` is the [Fork](https://github.com/jshttp/mime-types) of The Ultimate Javascript Content-Type utility Written In ES6 And Optimised With JavaScript Compiler. Depends on the [`mime-db`](https://github.com/jshttp/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`](https://artdecocode.com/goa/): _Koa_ web server compiled with _Google Closure Compiler_ using [**Depack**](https://artdecocode.com/depack/) into a single file library (0 dependencies).
```sh
yarn add @goa/mime-types
```
## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
- [`lookup(path: string): string`](#lookuppath-string-string)
- [`contentType(type: string): string`](#contenttypetype-string-string)
- [`extension(type: string): string`](#extensiontype-string-string)
- [`charset(type: string): string`](#charsettype-string-string)
- [`types`](#types)
- [`extensions`](#extensions)
- [Copyright](#copyright)
## API
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.
## `lookup(`
`path: string,`
`): string`
Lookup 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
```
## `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.
```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
```
## `extension(`
`type: string,`
`): string`
Get the default extension for a content-type.
```js
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.
```js
import { charset } from '@goa/mime-types'
console.log(charset('text/markdown'))
```
```
UTF-8
```
## `types`
A map of content-types by extension.
```js
import { types } from '@goa/mime-types'
console.log(types['json'])
```
```
application/json
```
## `extensions`
A map of extensions by content-type.
```js
import { extensions } from '@goa/mime-types'
console.log(extensions['application/mp4'])
```
```
[ 'mp4s', 'm4p' ]
```
## Copyright
Original Work by [Jonathan Ong, Douglas Christopher Wilson](https://github.com/jshttp/mime-types).
---
© Art Deco for Idio 2019
Tech Nation Visa Sucks