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)
37 this.headers = req.headers
38 this.negotiator = new Negotiator(req)
42 * Check if the given `type(s)` is acceptable, returning
43 * the best match when true, otherwise `undefined`, in which
44 * case you should respond with 406 "Not Acceptable".
46 * The `type` value may be a single mime type string
47 * such as "application/json", the extension name
48 * such as "json" or an array `["json", "html", "text/plain"]`. When a list
49 * or array is given the _best_ match, if any is returned.
53 * // Accept: text/html
57 * // Accept: text/*, application/json
60 * this.types('text/html');
62 * this.types('json', 'text');
64 * this.types('application/json');
65 * // => "application/json"
67 * // Accept: text/*, application/json
68 * this.types('image/png');
72 * // Accept: text/*;q=.5, application/json
73 * this.types(['html', 'json']);
74 * this.types('html', 'json');
77 * @param {String|Array} types...
78 * @return {String|Array|Boolean}
82 Accepts.prototype.type =
83 Accepts.prototype.types = function (types_) {
86 // support flattened arguments
87 if (types && !Array.isArray(types)) {
88 types = new Array(arguments.length)
89 for (var i = 0; i < types.length; i++) {
90 types[i] = arguments[i]
94 // no types, return all requested types
95 if (!types || types.length === 0) {
96 return this.negotiator.mediaTypes()
99 // no accept header, return first given type
100 if (!this.headers.accept) {
104 var mimes = types.map(extToMime)
105 var accepts = this.negotiator.mediaTypes(mimes.filter(validMime))
106 var first = accepts[0]
109 ? types[mimes.indexOf(first)]
114 * Return accepted encodings or best fit based on `encodings`.
116 * Given `Accept-Encoding: gzip, deflate`
117 * an array sorted by quality is returned:
119 * ['gzip', 'deflate']
121 * @param {String|Array} encodings...
122 * @return {String|Array}
126 Accepts.prototype.encoding =
127 Accepts.prototype.encodings = function (encodings_) {
128 var encodings = encodings_
130 // support flattened arguments
131 if (encodings && !Array.isArray(encodings)) {
132 encodings = new Array(arguments.length)
133 for (var i = 0; i < encodings.length; i++) {
134 encodings[i] = arguments[i]
138 // no encodings, return all requested encodings
139 if (!encodings || encodings.length === 0) {
140 return this.negotiator.encodings()
143 return this.negotiator.encodings(encodings)[0] || false
147 * Return accepted charsets or best fit based on `charsets`.
149 * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
150 * an array sorted by quality is returned:
152 * ['utf-8', 'utf-7', 'iso-8859-1']
154 * @param {String|Array} charsets...
155 * @return {String|Array}
159 Accepts.prototype.charset =
160 Accepts.prototype.charsets = function (charsets_) {
161 var charsets = charsets_
163 // support flattened arguments
164 if (charsets && !Array.isArray(charsets)) {
165 charsets = new Array(arguments.length)
166 for (var i = 0; i < charsets.length; i++) {
167 charsets[i] = arguments[i]
171 // no charsets, return all requested charsets
172 if (!charsets || charsets.length === 0) {
173 return this.negotiator.charsets()
176 return this.negotiator.charsets(charsets)[0] || false
180 * Return accepted languages or best fit based on `langs`.
182 * Given `Accept-Language: en;q=0.8, es, pt`
183 * an array sorted by quality is returned:
187 * @param {String|Array} langs...
188 * @return {Array|String}
192 Accepts.prototype.lang =
193 Accepts.prototype.langs =
194 Accepts.prototype.language =
195 Accepts.prototype.languages = function (languages_) {
196 var languages = languages_
198 // support flattened arguments
199 if (languages && !Array.isArray(languages)) {
200 languages = new Array(arguments.length)
201 for (var i = 0; i < languages.length; i++) {
202 languages[i] = arguments[i]
206 // no languages, return all requested languages
207 if (!languages || languages.length === 0) {
208 return this.negotiator.languages()
211 return this.negotiator.languages(languages)[0] || false
215 * Convert extnames to mime.
217 * @param {String} type
222 function extToMime (type) {
223 return type.indexOf('/') === -1
229 * Check if mime is valid.
231 * @param {String} type
236 function validMime (type) {
237 return typeof type === 'string'