@goa/content-type
v1.0.1
Published
[fork] Create and parse HTTP Content-Type header according to RFC 7231 Written In ES6 And Optimised With JavaScript Compiler.
Downloads
13
Readme
@goa/content-type
@goa/content-type
is a fork of Create and parse HTTP Content-Type header according to RFC 7231 Written In ES6 And Optimised With JavaScript Compiler.
yarn add @goa/content-type
Table Of Contents
- Table Of Contents
- API
parse(header: string|http.IncomingMessage|http.ServerResponse): ContentType
format(obj: ContentType): string
- Copyright
API
The package is available by importing its named functions:
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). |
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.
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.