CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
README.md141 linesDownload Raw Back to accepts
1# accepts2 3[![NPM Version][npm-version-image]][npm-url]4[![NPM Downloads][npm-downloads-image]][npm-url]5[![Node.js Version][node-version-image]][node-version-url]6[![Build Status][github-actions-ci-image]][github-actions-ci-url]7[![Test Coverage][coveralls-image]][coveralls-url]8 9Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator).10Extracted from [koa](https://www.npmjs.com/package/koa) for general use.11 12In addition to negotiator, it allows:13 14- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])`15  as well as `('text/html', 'application/json')`.16- Allows type shorthands such as `json`.17- Returns `false` when no types match18- Treats non-existent headers as `*`19 20## Installation21 22This is a [Node.js](https://nodejs.org/en/) module available through the23[npm registry](https://www.npmjs.com/). Installation is done using the24[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):25 26```sh27$ npm install accepts28```29 30## API31 32```js33var accepts = require('accepts')34```35 36### accepts(req)37 38Create a new `Accepts` object for the given `req`.39 40#### .charset(charsets)41 42Return the first accepted charset. If nothing in `charsets` is accepted,43then `false` is returned.44 45#### .charsets()46 47Return the charsets that the request accepts, in the order of the client's48preference (most preferred first).49 50#### .encoding(encodings)51 52Return the first accepted encoding. If nothing in `encodings` is accepted,53then `false` is returned.54 55#### .encodings()56 57Return the encodings that the request accepts, in the order of the client's58preference (most preferred first).59 60#### .language(languages)61 62Return the first accepted language. If nothing in `languages` is accepted,63then `false` is returned.64 65#### .languages()66 67Return the languages that the request accepts, in the order of the client's68preference (most preferred first).69 70#### .type(types)71 72Return the first accepted type (and it is returned as the same text as what73appears in the `types` array). If nothing in `types` is accepted, then `false`74is returned.75 76The `types` array can contain full MIME types or file extensions. Any value77that is not a full MIME types is passed to `require('mime-types').lookup`.78 79#### .types()80 81Return the types that the request accepts, in the order of the client's82preference (most preferred first).83 84## Examples85 86### Simple type negotiation87 88This simple example shows how to use `accepts` to return a different typed89respond body based on what the client wants to accept. The server lists it's90preferences in order and will get back the best match between the client and91server.92 93```js94var accepts = require('accepts')95var http = require('http')96 97function app (req, res) {98  var accept = accepts(req)99 100  // the order of this list is significant; should be server preferred order101  switch (accept.type(['json', 'html'])) {102    case 'json':103      res.setHeader('Content-Type', 'application/json')104      res.write('{"hello":"world!"}')105      break106    case 'html':107      res.setHeader('Content-Type', 'text/html')108      res.write('<b>hello, world!</b>')109      break110    default:111      // the fallback is text/plain, so no need to specify it above112      res.setHeader('Content-Type', 'text/plain')113      res.write('hello, world!')114      break115  }116 117  res.end()118}119 120http.createServer(app).listen(3000)121```122 123You can test this out with the cURL program:124```sh125curl -I -H'Accept: text/html' http://localhost:3000/126```127 128## License129 130[MIT](LICENSE)131 132[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/accepts/master133[coveralls-url]: https://coveralls.io/r/jshttp/accepts?branch=master134[github-actions-ci-image]: https://badgen.net/github/checks/jshttp/accepts/master?label=ci135[github-actions-ci-url]: https://github.com/jshttp/accepts/actions/workflows/ci.yml136[node-version-image]: https://badgen.net/npm/node/accepts137[node-version-url]: https://nodejs.org/en/download138[npm-downloads-image]: https://badgen.net/npm/dm/accepts139[npm-url]: https://npmjs.org/package/accepts140[npm-version-image]: https://badgen.net/npm/v/accepts141