https://github.com/idiocc/accepts
Higher-Level Content Negotiation In ES6 Optimised With Google Closure Compiler.
https://github.com/idiocc/accepts
closure-compiler idio
Last synced: 8 months ago
JSON representation
Higher-Level Content Negotiation In ES6 Optimised With Google Closure Compiler.
- Host: GitHub
- URL: https://github.com/idiocc/accepts
- Owner: idiocc
- License: agpl-3.0
- Created: 2019-05-05T04:46:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-19T06:58:46.000Z (over 6 years ago)
- Last Synced: 2025-09-30T10:56:21.725Z (9 months ago)
- Topics: closure-compiler, idio
- Language: JavaScript
- Homepage: https://www.idio.cc
- Size: 286 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: COPYING
Awesome Lists containing this project
README
# @goa/accepts
[](https://www.npmjs.com/package/@goa/accepts)
`@goa/accepts` is a fork of 🏛 [Higher-Level Content Negotiation](https://github.com/jshttp/accepts/) In ES6 Optimised With [JavaScript Compiler](https://compiler.page).
```sh
yarn add @goa/accepts
```
## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
- [class Accepts](#class-accepts)
* [`constructor(req): Accepts`](#constructorreq-httpincomingmessage-accepts)
- [Copyright & License](#copyright--license)
## API
The package is available by importing its default class:
```js
import Accepts from '@goa/accepts'
```
## class Accepts
The instances of this class allow to negotiate languages, charsets, encoding and types and additionally:
- Allow types as an array or arguments list, i.e. `(['text/html', 'application/json'])` as well as `('text/html', 'application/json')`;
- Allow type shorthands such as json;
- Return false when no types match;
- Treat non-existent headers as *.
### constructor(
`req: !http.IncomingMessage,`
): Accepts
Create a new Accepts object for the given request from a client.
- req*
!http.IncomingMessage: The request.
```js
import Accepts from '@goa/accepts'
import { createServer } from 'http'
import aqt from '@rqt/aqt'
function app(req, res) {
const accept = new Accepts(req)
// the order of this list is significant; should be server preferred order
switch (accept.type(['json', 'html'])) {
case 'json':
res.setHeader('Content-Type', 'application/json')
res.write('{"hello":"world!"}')
break
case 'html':
res.setHeader('Content-Type', 'text/html')
res.write('hello, world!')
break
default:
// the fallback is text/plain, so no need to specify it above
res.setHeader('Content-Type', 'text/plain')
res.write('hello, world!')
break
}
res.end()
}
const server = createServer(app)
server.listen(0, async () => {
const url = `http://localhost:${server.address().port}`
let { body, headers } = await aqt(url, {
headers: { 'accept': 'application/json' },
})
console.log('Response:', body, '\tType:', headers['content-type'])
;({ body, headers } = await aqt(url, {
headers: { 'accept': 'text/html' },
}))
console.log('Response:', body, '\tType:', headers['content-type'])
;({ body, headers } = await aqt(url, {
headers: { 'accept': 'text/plain' },
}))
console.log('Response:', body, '\tType:', headers['content-type'])
server.close()
})
```
```
Response: { hello: 'world!' } Type: application/json
Response: hello, world! Type: text/html
Response: hello, world! Type: text/plain
```
[🔖 View all instance methods in Wiki](../../wiki/Accepts)
## Copyright & License
GNU Affero General Public License v3.0
[Original work, documentation and testing](https://github.com/jshttp/accepts) by Jonathan Ong and Douglas Christopher Wilson under MIT license found in [COPYING](COPYING).
---
© Art Deco for Idio 2019
Tech Nation Visa Sucks