JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q72081F
  • License MIT

[fork] Higher-Level Content Negotiation In ES6 Optimised With Google Closure Compiler.

Package Exports

  • @goa/accepts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@goa/accepts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@goa/accepts

npm version

@goa/accepts is a fork of Higher-Level Content Negotiation In ES6 Optimised With JavaScript Compiler. It already includes mime-types database and is bundled into a single JS file for faster execution.

The original module has been updated to be used in @goa/koa: Koa web server compiled with Google Closure Compiler using Depack into a single file library (0 dependencies).

yarn add @goa/accepts

Table Of Contents

API

The package is available by importing its default function:

import Accepts from '@goa/accepts'

class Accepts

The instances of this class allow to negotiate languages, charsets, encoding and types and additionally:

  • Allows types as an array or arguments list, i.e. (['text/html', 'application/json']) as well as ('text/html', 'application/json');
  • Allows type shorthands such as json;
  • Returns false when no types match;
  • Treats 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

_goa.Accepts: Higher-Level Content Negotiation.

Name Type Description
types* function((string | !Array<string>), ...string) Check if the given type(s) is acceptable, returning the best match when true, otherwise undefined, in which case you should respond with 406 "Not Acceptable".
The type value may be a single mime type string such as "application/json", the extension name such as "json" or an array ["json", "html", "text/plain"]. When a list or array is given the best match, if any is returned. Examples:
  • [Accept: text/html]
  • this.types('html') => "html"
  • [Accept: text/, application/json]
  • this.types('html') => "html"
    this.types('text/html') => "text/html"
    this.types('json', 'text') => "json"
    this.types('application/json') => "application/json"
  • [Accept: text/, application/json]
  • this.types('image/png') => undefined
    this.types('png') => undefined
  • [Accept: text/*;q=.5, application/json]
  • this.types(['html', 'json']) => "json"
    this.types('html', 'json') => "json"
    type* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Check if the given type(s) is acceptable, returning the best match when true, otherwise undefined, in which case you should respond with 406 "Not Acceptable".
    The type value may be a single mime type string such as "application/json", the extension name such as "json" or an array ["json", "html", "text/plain"]. When a list or array is given the best match, if any is returned. Examples:
  • [Accept: text/html]
  • this.types('html') => "html"
  • [Accept: text/, application/json]
  • this.types('html') => "html"
    this.types('text/html') => "text/html"
    this.types('json', 'text') => "json"
    this.types('application/json') => "application/json"
  • [Accept: text/, application/json]
  • this.types('image/png') => undefined
    this.types('png') => undefined
  • [Accept: text/*;q=.5, application/json]
  • this.types(['html', 'json']) => "json"
    this.types('html', 'json') => "json"
    encodings* function((string | !Array<string>), ...string) Return accepted encodings or best fit based on encodings. Given Accept-Encoding: gzip, deflate an array sorted by quality is returned: ['gzip', 'deflate'].
    encoding* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Return accepted encodings or best fit based on encodings. Given Accept-Encoding: gzip, deflate an array sorted by quality is returned: ['gzip', 'deflate'].
    charsets* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Return accepted charsets or best fit based on charsets. Given Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5 an array sorted by quality is returned: ['utf-8', 'utf-7', 'iso-8859-1'].
    charset* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Return accepted charsets or best fit based on charsets. Given Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5 an array sorted by quality is returned: ['utf-8', 'utf-7', 'iso-8859-1'].
    languages* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Return accepted languages or best fit based on langs. Given Accept-Language: en;q=0.8, es, pt an array sorted by quality is returned: ['es', 'pt', 'en'].
    langs* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Return accepted languages or best fit based on langs. Given Accept-Language: en;q=0.8, es, pt an array sorted by quality is returned: ['es', 'pt', 'en'].
    lang* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Return accepted languages or best fit based on langs. Given Accept-Language: en;q=0.8, es, pt an array sorted by quality is returned: ['es', 'pt', 'en'].
    language* function((string | !Array<string>), ...string): (string | !Array<string> | boolean) Return accepted languages or best fit based on langs. Given Accept-Language: en;q=0.8, es, pt an array sorted by quality is returned: ['es', 'pt', 'en'].

    Original work, documentation and testing by Jonathan Ong and Douglas Christopher Wilson.


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