vue3/node_modules/@tinyhttp/accepts/dist/index.js

111 行
3.3 KiB
JavaScript

import Negotiator from "negotiator";
import mime from "mime";
const extToMime = (type) => type.indexOf("/") == -1 ? mime.getType(type) : type;
const validMime = (type) => typeof type == "string";
class Accepts {
constructor(req) {
this.headers = req.headers;
this.negotiator = new Negotiator(req);
}
/**
* Check if the given `type(s)` is acceptable, returning the best match when true, otherwise `false`, in which case you should respond with 406 "Not Acceptable".
*
* The `type` value may be a single mime type string such as "application/json", the extension name such as "json" or an array `["json", "html", "text/plain"]`. When a list or array is given the _best_ match, if any is returned. When no types are given as arguments, returns all types accepted by the client in the preference order.
*/
types(types, ...args) {
let mimeTypes = [];
if (types && !Array.isArray(types)) {
mimeTypes = [types, ...args];
} else if (types) {
mimeTypes = [...types, ...args];
}
if (!mimeTypes || mimeTypes.length == 0) {
return this.negotiator.mediaTypes();
}
if (!this.headers["accept"]) {
return mimeTypes[0];
}
const mimes = mimeTypes.map(extToMime);
const accepts = this.negotiator.mediaTypes(mimes.filter(validMime));
const [first] = accepts;
return first ? mimeTypes[mimes.indexOf(first)] : false;
}
get type() {
return this.types;
}
/**
* Return accepted encodings or best fit based on `encodings`.
*
* Given `Accept-Encoding: gzip, deflate`
* an array sorted by quality is returned:
*
* ['gzip', 'deflate']
*/
encodings(encodings, ...args) {
let _encodings = encodings;
if (_encodings && !Array.isArray(_encodings)) {
_encodings = [_encodings, ...args];
}
if (!_encodings || _encodings.length == 0) {
return this.negotiator.encodings();
}
return this.negotiator.encodings(_encodings)[0] || false;
}
get encoding() {
return this.encodings;
}
/**
* Return accepted charsets or best fit based on `charsets`.
*
* Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
* an array sorted by quality is returned:
*
* ['utf-8', 'utf-7', 'iso-8859-1']
*/
charsets(charsets, ...args) {
let _charsets = charsets;
if (_charsets && !Array.isArray(_charsets)) {
_charsets = [_charsets, ...args];
}
if (!_charsets || _charsets.length == 0) {
return this.negotiator.charsets();
}
return this.negotiator.charsets(_charsets)[0] || false;
}
get charset() {
return this.charsets;
}
/**
* Return accepted languages or best fit based on `langs`.
*
* Given `Accept-Language: en;q=0.8, es, pt`
* an array sorted by quality is returned:
*
* ['es', 'pt', 'en']
*
*/
languages(languages, ...args) {
let _languages = languages;
if (_languages && !Array.isArray(_languages)) {
_languages = [_languages, ...args];
}
if (!_languages || _languages.length == 0) {
return this.negotiator.languages();
}
return this.negotiator.languages(_languages)[0] || false;
}
get lang() {
return this.languages;
}
get langs() {
return this.languages;
}
get language() {
return this.languages;
}
}
export {
Accepts
};
//# sourceMappingURL=index.js.map