accepts
Advanced tools
Comparing version 1.3.3 to 1.3.4
@@ -0,1 +1,7 @@ | ||
1.3.4 / 2017-08-22 | ||
================== | ||
* deps: mime-types@~2.1.16 | ||
- deps: mime-db@~1.29.0 | ||
1.3.3 / 2016-05-02 | ||
@@ -2,0 +8,0 @@ ================== |
29
index.js
@@ -32,5 +32,6 @@ /*! | ||
function Accepts(req) { | ||
if (!(this instanceof Accepts)) | ||
function Accepts (req) { | ||
if (!(this instanceof Accepts)) { | ||
return new Accepts(req) | ||
} | ||
@@ -99,8 +100,14 @@ this.headers = req.headers | ||
if (!this.headers.accept) return types[0]; | ||
var mimes = types.map(extToMime); | ||
var accepts = this.negotiator.mediaTypes(mimes.filter(validMime)); | ||
var first = accepts[0]; | ||
if (!first) return false; | ||
return types[mimes.indexOf(first)]; | ||
// no accept header, return first given type | ||
if (!this.headers.accept) { | ||
return types[0] | ||
} | ||
var mimes = types.map(extToMime) | ||
var accepts = this.negotiator.mediaTypes(mimes.filter(validMime)) | ||
var first = accepts[0] | ||
return first | ||
? types[mimes.indexOf(first)] | ||
: false | ||
} | ||
@@ -217,3 +224,3 @@ | ||
function extToMime(type) { | ||
function extToMime (type) { | ||
return type.indexOf('/') === -1 | ||
@@ -232,4 +239,4 @@ ? mime.lookup(type) | ||
function validMime(type) { | ||
return typeof type === 'string'; | ||
function validMime (type) { | ||
return typeof type === 'string' | ||
} |
{ | ||
"name": "accepts", | ||
"description": "Higher-level content negotiation", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"contributors": [ | ||
@@ -12,7 +12,14 @@ "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"dependencies": { | ||
"mime-types": "~2.1.11", | ||
"mime-types": "~2.1.16", | ||
"negotiator": "0.6.1" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0.4.3", | ||
"eslint": "3.19.0", | ||
"eslint-config-standard": "10.2.1", | ||
"eslint-plugin-import": "2.7.0", | ||
"eslint-plugin-markdown": "1.0.0-beta.6", | ||
"eslint-plugin-node": "5.1.1", | ||
"eslint-plugin-promise": "3.5.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "~1.21.5" | ||
@@ -29,2 +36,3 @@ }, | ||
"scripts": { | ||
"lint": "eslint --plugin markdown --ext js,md .", | ||
"test": "mocha --reporter spec --check-leaks --bail test/", | ||
@@ -31,0 +39,0 @@ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", |
@@ -9,7 +9,9 @@ # accepts | ||
Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator). Extracted from [koa](https://www.npmjs.com/package/koa) for general use. | ||
Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator). | ||
Extracted from [koa](https://www.npmjs.com/package/koa) for general use. | ||
In addition to negotiator, it allows: | ||
- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])` as well as `('text/html', 'application/json')`. | ||
- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])` | ||
as well as `('text/html', 'application/json')`. | ||
- Allows type shorthands such as `json`. | ||
@@ -21,4 +23,8 @@ - Returns `false` when no types match | ||
This is a [Node.js](https://nodejs.org/en/) module available through the | ||
[npm registry](https://www.npmjs.com/). Installation is done using the | ||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): | ||
```sh | ||
npm install accepts | ||
$ npm install accepts | ||
``` | ||
@@ -28,2 +34,4 @@ | ||
<!-- eslint-disable no-unused-vars --> | ||
```js | ||
@@ -94,7 +102,7 @@ var accepts = require('accepts') | ||
function app(req, res) { | ||
function app (req, res) { | ||
var accept = accepts(req) | ||
// the order of this list is significant; should be server preferred order | ||
switch(accept.type(['json', 'html'])) { | ||
switch (accept.type(['json', 'html'])) { | ||
case 'json': | ||
@@ -133,3 +141,3 @@ res.setHeader('Content-Type', 'application/json') | ||
[node-version-image]: https://img.shields.io/node/v/accepts.svg | ||
[node-version-url]: http://nodejs.org/download/ | ||
[node-version-url]: https://nodejs.org/en/download/ | ||
[travis-image]: https://img.shields.io/travis/jshttp/accepts/master.svg | ||
@@ -136,0 +144,0 @@ [travis-url]: https://travis-ci.org/jshttp/accepts |
16458
204
144
9
Updatedmime-types@~2.1.16