3 * Copyright(c) 2014 Jonathan Ong
4 * Copyright(c) 2015 Douglas Christopher Wilson
11 * Module dependencies.
15 var Negotiator = require('negotiator')
16 var mime = require('mime-types')
23 module.exports = Accepts
26 * Create a new Accepts object for the given req.
32 function Accepts(req) {
33 if (!(this instanceof Accepts))
34 return new Accepts(req)
36 this.headers = req.headers
37 this.negotiator = new Negotiator(req)
41 * Check if the given `type(s)` is acceptable, returning
42 * the best match when true, otherwise `undefined`, in which
43 * case you should respond with 406 "Not Acceptable".
45 * The `type` value may be a single mime type string
46 * such as "application/json", the extension name
47 * such as "json" or an array `["json", "html", "text/plain"]`. When a list
48 * or array is given the _best_ match, if any is returned.
52 * // Accept: text/html
56 * // Accept: text/*, application/json
59 * this.types('text/html');
61 * this.types('json', 'text');
63 * this.types('application/json');
64 * // => "application/json"
66 * // Accept: text/*, application/json
67 * this.types('image/png');
71 * // Accept: text/*;q=.5, application/json
72 * this.types(['html', 'json']);
73 * this.types('html', 'json');
76 * @param {String|Array} types...
77 * @return {String|Array|Boolean}
81 Accepts.prototype.type =
82 Accepts.prototype.types = function (types_) {
85 // support flattened arguments
86 if (types && !Array.isArray(types)) {
87 types = new Array(arguments.length)
88 for (var i = 0; i < types.length; i++) {
89 types[i] = arguments[i]
93 // no types, return all requested types
94 if (!types || types.length === 0) {
95 return this.negotiator.mediaTypes()
98 if (!this.headers.accept) return types[0];
99 var mimes = types.map(extToMime);
100 var accepts = this.negotiator.mediaTypes(mimes.filter(validMime));
101 var first = accepts[0];
102 if (!first) return false;
103 return types[mimes.indexOf(first)];
107 * Return accepted encodings or best fit based on `encodings`.
109 * Given `Accept-Encoding: gzip, deflate`
110 * an array sorted by quality is returned:
112 * ['gzip', 'deflate']
114 * @param {String|Array} encodings...
115 * @return {String|Array}
119 Accepts.prototype.encoding =
120 Accepts.prototype.encodings = function (encodings_) {
121 var encodings = encodings_
123 // support flattened arguments
124 if (encodings && !Array.isArray(encodings)) {
125 encodings = new Array(arguments.length)
126 for (var i = 0; i < encodings.length; i++) {
127 encodings[i] = arguments[i]
131 // no encodings, return all requested encodings
132 if (!encodings || encodings.length === 0) {
133 return this.negotiator.encodings()
136 return this.negotiator.encodings(encodings)[0] || false
140 * Return accepted charsets or best fit based on `charsets`.
142 * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
143 * an array sorted by quality is returned:
145 * ['utf-8', 'utf-7', 'iso-8859-1']
147 * @param {String|Array} charsets...
148 * @return {String|Array}
152 Accepts.prototype.charset =
153 Accepts.prototype.charsets = function (charsets_) {
154 var charsets = charsets_
156 // support flattened arguments
157 if (charsets && !Array.isArray(charsets)) {
158 charsets = new Array(arguments.length)
159 for (var i = 0; i < charsets.length; i++) {
160 charsets[i] = arguments[i]
164 // no charsets, return all requested charsets
165 if (!charsets || charsets.length === 0) {
166 return this.negotiator.charsets()
169 return this.negotiator.charsets(charsets)[0] || false
173 * Return accepted languages or best fit based on `langs`.
175 * Given `Accept-Language: en;q=0.8, es, pt`
176 * an array sorted by quality is returned:
180 * @param {String|Array} langs...
181 * @return {Array|String}
185 Accepts.prototype.lang =
186 Accepts.prototype.langs =
187 Accepts.prototype.language =
188 Accepts.prototype.languages = function (languages_) {
189 var languages = languages_
191 // support flattened arguments
192 if (languages && !Array.isArray(languages)) {
193 languages = new Array(arguments.length)
194 for (var i = 0; i < languages.length; i++) {
195 languages[i] = arguments[i]
199 // no languages, return all requested languages
200 if (!languages || languages.length === 0) {
201 return this.negotiator.languages()
204 return this.negotiator.languages(languages)[0] || false
208 * Convert extnames to mime.
210 * @param {String} type
215 function extToMime(type) {
216 return type.indexOf('/') === -1
222 * Check if mime is valid.
224 * @param {String} type
229 function validMime(type) {
230 return typeof type === 'string';