@goa/mime-types

1.1.0 • Public • Published

@goa/mime-types

npm version

@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-types

Table Of Contents

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/json

extensions

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 © Art Deco for Idio 2019 Idio Tech Nation Visa Tech Nation Visa Sucks

Readme

Keywords

Package Sidebar

Install

npm i @goa/mime-types

Weekly Downloads

6

Version

1.1.0

License

MIT

Unpacked Size

15.1 kB

Total Files

11

Last publish

Collaborators

  • zvr