Socket
Socket
Sign inDemoInstall

@tinyhttp/accepts

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tinyhttp/accepts - npm Package Compare versions

Comparing version 2.0.5 to 2.0.6

5

package.json
{
"name": "@tinyhttp/accepts",
"description": "accepts rewrite in TypeScript",
"version": "2.0.5",
"version": "2.0.6",
"homepage": "https://tinyhttp.v1rtl.site",

@@ -33,4 +33,3 @@ "funding": {

"build": "rollup -c"
},
"readme": "# @tinyhttp/accepts\n\n> [`accepts`](https://github.com/jshttp/accepts) rewrite in TypeScript.\n\nHigher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator).\nExtracted from [koa](https://www.npmjs.com/package/koa) for general use.\n\nIn addition to negotiator, it allows:\n\n- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])`\n as well as `('text/html', 'application/json')`.\n- Allows type shorthands such as `json`.\n- Returns `false` when no types match\n- Treats non-existent headers as `*`\n\n## Install\n\n```sh\npnpm i @tinyhttp/accepts\n```\n\n## API\n\n```ts\nimport { Accepts } from '@tinyhttp/accepts'\n```\n\n### accepts(req)\n\nCreate a new `Accepts` object for the given `req`.\n\n#### `.charset(charsets)`\n\nReturn the first accepted charset. If nothing in `charsets` is accepted,\nthen `false` is returned.\n\n#### `.charsets()`\n\nReturn the charsets that the request accepts, in the order of the client's\npreference (most preferred first).\n\n#### `.encoding(encodings)`\n\nReturn the first accepted encoding. If nothing in `encodings` is accepted,\nthen `false` is returned.\n\n#### `.encodings()`\n\nReturn the encodings that the request accepts, in the order of the client's\npreference (most preferred first).\n\n#### `.language(languages)`\n\nReturn the first accepted language. If nothing in `languages` is accepted,\nthen `false` is returned.\n\n#### `.languages()`\n\nReturn the languages that the request accepts, in the order of the client's\npreference (most preferred first).\n\n#### `.type(types)`\n\nReturn the first accepted type (and it is returned as the same text as what\nappears in the `types` array). If nothing in `types` is accepted, then `false`\nis returned.\n\nThe `types` array can contain full MIME types or file extensions. Any value\nthat is not a full MIME types is passed to `require('mime-types').lookup`.\n\n#### `.types()`\n\nReturn the types that the request accepts, in the order of the client's\npreference (most preferred first).\n\n## Example\n\nThis simple example shows how to use `accepts` to return a different typed\nrespond body based on what the client wants to accept. The server lists it's\npreferences in order and will get back the best match between the client and\nserver.\n\n```ts\nimport Accepts from '@tinyhttp/accepts'\nimport { createServer } from 'http'\n\ncreateServer((req, res) => {\n const accept = new Accepts(req)\n\n // the order of this list is significant; should be server preferred order\n switch (accept.type(['json', 'html'])) {\n case 'json':\n res.setHeader('Content-Type', 'application/json')\n res.write('{\"hello\":\"world!\"}')\n break\n case 'html':\n res.setHeader('Content-Type', 'text/html')\n res.write('<b>hello, world!</b>')\n break\n default:\n // the fallback is text/plain, so no need to specify it above\n res.setHeader('Content-Type', 'text/plain')\n res.write('hello, world!')\n break\n }\n\n res.end()\n}).listen(3000)\n```\n\nYou can test this out with the cURL program:\n\n```sh\ncurl -I -H 'Accept: text/html' http://localhost:3000/\n```\n"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc