Comparing version 1.2.0 to 1.2.1
@@ -0,1 +1,7 @@ | ||
1.2.1 / 2014-12-30 | ||
================== | ||
* deps: mime-types@~2.0.5 | ||
- deps: mime-db@~1.3.1 | ||
1.2.0 / 2014-12-19 | ||
@@ -2,0 +8,0 @@ ================== |
{ | ||
"name": "accepts", | ||
"description": "Higher-level content negotiation", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -9,8 +9,8 @@ "license": "MIT", | ||
"dependencies": { | ||
"mime-types": "~2.0.4", | ||
"mime-types": "~2.0.5", | ||
"negotiator": "0.5.0" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "~0.3.4", | ||
"mocha": "~2.0.1" | ||
"istanbul": "0.3.5", | ||
"mocha": "~2.1.0" | ||
}, | ||
@@ -17,0 +17,0 @@ "files": [ |
119
README.md
@@ -9,5 +9,5 @@ # accepts | ||
Higher level content negotation based on [negotiator](https://github.com/federomero/negotiator). Extracted from [koa](https://github.com/koajs/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 negotatior, it allows: | ||
In addition to negotiator, it allows: | ||
@@ -19,65 +19,106 @@ - Allows types as an array or arguments list, ie `(['text/html', 'application/json'])` as well as `('text/html', 'application/json')`. | ||
## Installation | ||
```sh | ||
npm install accepts | ||
``` | ||
## API | ||
### var accept = new Accepts(req) | ||
```js | ||
var accepts = require('accepts') | ||
http.createServer(function (req, res) { | ||
var accept = accepts(req) | ||
}) | ||
``` | ||
### accept\[property\]\(\) | ||
### accepts(req) | ||
Returns all the explicitly accepted content property as an array in descending priority. | ||
Create a new `Accepts` object for the given `req`. | ||
- `accept.types()` | ||
- `accept.encodings()` | ||
- `accept.charsets()` | ||
- `accept.languages()` | ||
#### .charset(charsets) | ||
They are also aliased in singular form such as `accept.type()`. `accept.languages()` is also aliased as `accept.langs()`, etc. | ||
Return the first accepted charset. If nothing in `charsets` is accepted, | ||
then `false` is returned. | ||
Note: you should almost never do this in a real app as it defeats the purpose of content negotiation. | ||
#### .charsets() | ||
Example: | ||
Return the charsets that the request accepts, in the order of the client's | ||
preference (most preferred first). | ||
```js | ||
// in Google Chrome | ||
var encodings = accept.encodings() // -> ['sdch', 'gzip', 'deflate'] | ||
``` | ||
#### .encoding(encodings) | ||
Since you probably don't support `sdch`, you should just supply the encodings you support: | ||
Return the first accepted encoding. If nothing in `encodings` is accepted, | ||
then `false` is returned. | ||
```js | ||
var encoding = accept.encodings('gzip', 'deflate') // -> 'gzip', probably | ||
``` | ||
#### .encodings() | ||
### accept\[property\]\(values, ...\) | ||
Return the encodings that the request accepts, in the order of the client's | ||
preference (most preferred first). | ||
You can either have `values` be an array or have an argument list of values. | ||
#### .language(languages) | ||
If the client does not accept any `values`, `false` will be returned. | ||
If the client accepts any `values`, the preferred `value` will be return. | ||
Return the first accepted language. If nothing in `languages` is accepted, | ||
then `false` is returned. | ||
For `accept.types()`, shorthand mime types are allowed. | ||
#### .languages() | ||
Example: | ||
Return the languages that the request accepts, in the order of the client's | ||
preference (most preferred first). | ||
#### .type(types) | ||
Return the first accepted type (and it is returned as the same text as what | ||
appears in the `types` array). If nothing in `types` is accepted, then `false` | ||
is returned. | ||
The `types` array can contain full MIME types or file extensions. Any value | ||
that is not a full MIME types is passed to `require('mime-types').lookup`. | ||
#### .types() | ||
Return the types that the request accepts, in the order of the client's | ||
preference (most preferred first). | ||
## Examples | ||
### Simple type negotiation | ||
This simple example shows how to use `accepts` to return a different typed | ||
respond body based on what the client wants to accept. The server lists it's | ||
preferences in order and will get back the best match between the client and | ||
server. | ||
```js | ||
// req.headers.accept = 'application/json' | ||
var accepts = require('accepts') | ||
var http = require('http') | ||
accept.types('json') // -> 'json' | ||
accept.types('html', 'json') // -> 'json' | ||
accept.types('html') // -> false | ||
function app(req, res) { | ||
var accept = accepts(req) | ||
// req.headers.accept = '' | ||
// which is equivalent to `*` | ||
// the order of this list is significant; should be server preferred order | ||
switch(accept.type(['json', 'html'])) { | ||
case 'json': | ||
req.setHeader('Content-Type', 'application/json') | ||
req.write('{"hello":"world!"}') | ||
break | ||
case 'html': | ||
req.setHeader('Content-Type', 'text/html') | ||
req.write('<b>hello, world!</b>') | ||
break | ||
default: | ||
// the fallback is text/plain, so no need to specify it above | ||
req.setHeader('Content-Type', 'text/plain') | ||
req.write('hello, world!') | ||
break | ||
} | ||
accept.types() // -> [], no explicit types | ||
accept.types('text/html', 'text/json') // -> 'text/html', since it was first | ||
req.end() | ||
} | ||
http.createServer(app).listen(3000) | ||
``` | ||
You can test this out with the cURL program: | ||
```sh | ||
curl -I -H'Accept: text/html' http://localhost:3000/ | ||
``` | ||
## License | ||
@@ -84,0 +125,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11545
136
Updatedmime-types@~2.0.5