https://github.com/idiocc/content-type
[fork] Create and parse HTTP Content-Type header according to RFC 7231 Written In ES6 And Optimised With JavaScript Compiler.
https://github.com/idiocc/content-type
Last synced: about 1 year ago
JSON representation
[fork] Create and parse HTTP Content-Type header according to RFC 7231 Written In ES6 And Optimised With JavaScript Compiler.
- Host: GitHub
- URL: https://github.com/idiocc/content-type
- Owner: idiocc
- License: other
- Created: 2019-07-02T12:30:42.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-02T19:52:06.000Z (almost 7 years ago)
- Last Synced: 2025-03-15T19:37:18.676Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://idio.cc
- Size: 29.3 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @goa/content-type
[](https://npmjs.org/package/@goa/content-type)
`@goa/content-type` is a fork of [Create and parse HTTP Content-Type header according to RFC 7231](https://github.com/jshttp/content-type) Written In ES6 And Optimised With [JavaScript Compiler](https://compiler.page).
```sh
yarn add @goa/content-type
```
## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
- [`parse(header: string|http.IncomingMessage|http.ServerResponse): ContentType`](#parseheader-stringhttpincomingmessagehttpserverresponse-contenttype)
* [`_goa.ContentType`](#type-_goacontenttype)
- [`format(obj: ContentType): string`](#formatobj-contenttype-string)
- [Copyright](#copyright)
## API
The package is available by importing its named functions:
```js
import { parse, format } from '@goa/content-type'
```
## `parse(`
`header: string|http.IncomingMessage|http.ServerResponse,`
`): ContentType`
Parse a _Content-Type_ header either from a string, or a request or response objects.
`import('http').IncomingMessage` __`http.IncomingMessage`__
`import('http').ServerResponse` __`http.ServerResponse`__
__`_goa.ContentType`__: The content-type interface.
| Name | Type | Description |
| --------------- | -------------------------------------- | ------------------------------------------------------------------------------------------ |
| __type*__ | string | The type of the content-type. |
| __parameters*__ | !Object<string, string> | An object of the parameters in the media type (name of the parameter will be lower-cased). |
```js
import { parse } from '@goa/content-type'
const res = parse('image/svg+xml; charset=utf-8')
console.log(res)
const asRequest = parse({ headers: { // as IncomingMessage
'content-type': 'image/svg+xml; charset=utf-8',
} })
console.log(asRequest)
const asResponse = parse({ getHeader(header) { // as ServerResponse
if (header == 'content-type')
return 'image/svg+xml; charset=utf-8'
} })
console.log(asResponse)
```
```
ContentType { parameters: { charset: 'utf-8' }, type: 'image/svg+xml' }
ContentType { parameters: { charset: 'utf-8' }, type: 'image/svg+xml' }
ContentType { parameters: { charset: 'utf-8' }, type: 'image/svg+xml' }
```
## `format(`
`obj: ContentType,`
`): string`
Format an object into a _Content-Type_ header. This will return a string of the content type for the given object.
SourceOutput
```js
import { format } from '@goa/content-type'
const res = format({
type: 'image/svg+xml',
parameters: { charset: 'utf-8' },
})
console.log(res)
```
```
image/svg+xml; charset=utf-8
```
## Copyright
Original work by [Douglas Christopher Wilson and contributors](https://github.com/jshttp/content-type).
---
© Art Deco for Idio 2019
Tech Nation Visa Sucks