npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@goa/accepts

v2.0.1

Published

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

Downloads

16

Readme

@goa/accepts

npm version

@goa/accepts is a fork of 🏛 Higher-Level Content Negotiation 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.

  • req* !http.IncomingMessage: The request.
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

🔖 View all instance methods in Wiki

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.