包详细信息

@goa/accepts

idiocc104AGPL-3.02.0.1

Higher-Level Content Negotiation In ES6 Optimised With JavaScript Compiler.

accepts, goa, content, negotiation

自述文件

@goa/accepts

npm version

@goa/accepts is a fork of <kbd>🏛 Higher-Level Content Negotiation</kbd> In ES6 Optimised With JavaScript Compiler.

yarn add @goa/accepts

Table Of Contents

API

The package is available by importing its default class:

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.

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('<b>hello, world!</b>')
    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: <b>hello, world!</b>     Type: text/html
Response: hello, world!     Type: text/plain

<kbd>🔖 View all instance methods in Wiki</kbd>

Copyright & License

GNU Affero General Public License v3.0

Original work, documentation and testing by Jonathan Ong and Douglas Christopher Wilson under MIT license found in COPYING.


Art Deco © Art Deco for Idio 2019 Idio Tech Nation Visa Tech Nation Visa Sucks

更新日志

19 December 2019

2.0.1

  • [package] Publish the typedefs.json file.

2.0.0

  • [types] Compile types using functions and publish typedefs.json.
  • [license] Update to AGPL-3.0.

26 July 2019

1.2.2

  • [doc] Update doc.

25 July 2019

1.2.1

  • [fix] Publish the compile directory.

1.2.0

  • [doc] Write Wiki with Typal and Documentary.
  • [deps] Upd @goa/mime-types to not bundle mime-db.

11 May 2019

1.1.0

  • [fix] Use in the documentation.
  • [types] Publish types/index.js for access in other packages.

1.0.4

  • [doc] Allow empty arguments to functions, change documented undefined to false.

9 May 2019

1.0.3

  • [deps] Move mime-types to @goa/mime-types.

5 May 2019

1.0.2

  • [doc] Fix JSDoc param to constructor.

1.0.1

  • [deps] Move @rqt/aqt to dev-dependencies.

1.0.0

  • [package] Publish version v1.0.0.

0.0.0