An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# @goa/content-type

[![npm version](https://badge.fury.io/js/%40goa%2Fcontent-type.svg)](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


© Art Deco for Idio 2019


Idio




Tech Nation Visa


Tech Nation Visa Sucks