[fork] Infer the content-type of a request Written In ES6 And Optimised With JavaScript Compiler.
npm install @goa/type-is
@goa/type-is is a fork of Infer the content-type of a request Written In ES6 And Optimised With JavaScript Compiler.
``sh`
yarn add @goa/type-is
- Table Of Contents
- API
- typeis(request: http.IncomingMessage, types: string|Array
- hasBody(request: http.IncomingMessage): boolean
- is(mediaType: string, types: string|Array
- Copyright
The package is available by importing its default and named functions:
`js`
import typeis, { hasBody } from '@goa/type-is'
request: http.IncomingMessage,
types: string|Array
...types: string,
): ?string|booleanChecks if the request is one of the types. If the request has no body, even if there is a _Content-Type_ header, then null is returned. If the _Content-Type_ header is invalid or does not matches any of the types, then false is returned. Otherwise, a string of the type that matched is returned.
The request argument is expected to be a Node.js HTTP request. The types argument is an array of type strings.
Each type in the types array can be one of the following:
- A file extension name such as json. This name will be returned if matched.application/json
- A mime type such as ./
- A mime type with a wildcard such as or /json or application/. The full mime type will be returned if matched.+json
- A suffix such as . This can be combined with a wildcard such as /vnd+json or application/+json. The full mime type will be returned if matched.
| Source | Output |
|---|---|
` const req = { log(typeis(req, ['json'])) // pass types as variable arguments log(typeis(req, ['html'])) | ` false |
request: http.IncomingMessage,
): booleanReturns a _Boolean_ if the given request has a body, regardless of the _Content-Type_ header.
Having a body has no relation to how large the body is (it may be 0 bytes). This is similar to how file existence works. If a body does exist, then this indicates that there is data to read from the Node.js request stream.
| Source | Output |
|---|---|
` log(hasBody({ headers: { log(hasBody({ headers: { log(hasBody({ headers: { | ` |
mediaType: string,
types: string|Array
...types: string,
): ?string|booleanChecks if the mediaType is one of the types. If the mediaType is invalid or does not matches any of the types, then false is returned. Otherwise, a string of the type that matched is returned.
The mediaType argument is expected to be a media type string. The types argument is an array of type strings.
Each type in the types array follows the same rules as described in the typeis section.
| Source | Output |
|---|---|
` const mediaType = 'application/json' log(is(mediaType, ['json'])) // pass types as variable arguments log(is(mediaType, ['html'])) | ` false |
Original Work by Jonathan Ong, Douglas Christopher Wilson and contributors.
---
![]() | © Art Deco for Idio 2019 | alt="Tech Nation Visa"> | Tech Nation Visa Sucks |
|---|