lib/request/accepts.js

1.
const accepts_ = require("accepts");
2.

			
3.
/**
4.
 * This is the accepts class. With this class you can check if the client accepts certain content 
5.
 * types, encodings, charsets and/or languages.
6.
 * @class request.accepts
7.
 * @nofunction
8.
 */
9.
module.exports = class accepts {
10.
	constructor(request) {
11.
		this._accepts = accepts_(request);
12.
	}
13.

			
14.
	/**
15.
	 * With this function you can get or check what types are accepted. If the type parameter is 
16.
	 * undefined it will return all the accepted types. You can also insert an array of types and 
17.
	 * the first item that is accepted will be returned.
18.
	 * @example
19.
	 * // with as content-type "application/javascript"
20.
	 * req.charset(["css", "js", "html"]);
21.
	 * // -> will return "application/javascript"
22.
	 * @summary Checks or returns what types are accepted.
23.
	 * @param {String[]} (type) The array of types to check.
24.
	 * @returns {String[]|String} Returns all accept types or the type that matched.
25.
	 * @function type
26.
	 */
27.
	type(type) {
28.
		if(type === undefined) return this._accepts.types();
29.

			
30.
		return this._accepts.type(type);
31.
	}
32.

			
33.
	/**
34.
	 * With this function you can get or check what encodings are accepted. If the type parameter 
35.
	 * is undefined it will return all the accepted encodings. You can also insert an array of 
36.
	 * encodings and the first item that is accepted will be returned.
37.
	 * @example
38.
	 * // with as content-type "deflate"
39.
	 * req.charset(["utf-8", "deflate", "gzip"]);
40.
	 * // -> will return "deflate"
41.
	 * @summary Checks or returns what encodings are accepted.
42.
	 * @param {String[]} (type) The array of encodings to check.
43.
	 * @returns {String[]|String} Returns all accept encodings or the type that matched.
44.
	 * @function encoding
45.
	 */
46.
	encoding(encoding) {
47.
		if(encoding === undefined) return this._accepts.encodings();
48.

			
49.
		return this._accepts.encoding(encoding);
50.
	}
51.

			
52.
	/**
53.
	 * With this function you can get or check what charsets are accepted. If the type parameter is
54.
	 *  undefined it will return all the accepted charsets. You can also insert an array of 
55.
	 * charsets and the first item that is accepted will be returned.
56.
	 * @example
57.
	 * // with as content-type "utf-8"
58.
	 * req.charset(["ascii", "utf-8", "utf-16"]);
59.
	 * // -> will return "utf-8"
60.
	 * @summary Checks or returns what charsets are accepted.
61.
	 * @param {String[]} (type) The array of charsets to check.
62.
	 * @returns {String[]|String} Returns all accept charsets or the type that matched.
63.
	 * @function charset
64.
	 */
65.
	charset(charset) {
66.
		if(charset === undefined) return this._accepts.charsets();
67.

			
68.
		return this._accepts.charset(charset);
69.
	}
70.

			
71.
	/**
72.
	 * With this function you can get or check what languages are accepted. If the type parameter 
73.
	 * is undefined it will return all the accepted languages. You can also insert an array of 
74.
	 * languages and the first item that is accepted will be returned.
75.
	 * @example
76.
	 * // with as content-type "en"
77.
	 * req.charset(["nl", "en", "de"]);
78.
	 * // -> will return "en"
79.
	 * @summary Checks or returns what languages are accepted.
80.
	 * @param {String[]} (type) The array of languages to check.
81.
	 * @returns {String[]|String} Returns all accept languages or the type that matched.
82.
	 * @function language
83.
	 */
84.
	language(language) {
85.
		if(language === undefined) return this._accepts.languages();
86.

			
87.
		return this._accepts.language(language);
88.
	}
89.
}
90.