-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
169 lines (155 loc) · 4.33 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*!
* Based on https://github.com/jshttp/accepts/blob/master/index.js
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2015 Douglas Christopher Wilson
* Copyright(c) 2020 Henry Zhuang
* MIT Licensed
*/
import { Negotiator, lookup } from "./deps.ts";
/**
* Create a new Accepts object for the given headers.
*
* @param {Headers} headers
* @public
*/
export class Accepts {
private headers: Headers;
private negotiator: Negotiator;
constructor(headers: Headers) {
this.headers = headers;
this.negotiator = new Negotiator(headers);
}
/**
* Check if the given `type(s)` is acceptable, returning
* the best match when true, otherwise `undefined`, 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.
*
* Examples:
*
* // Accept: text/html
* this.types(['html']);
* // => ["html"]
*
* // Accept: text/*, application/json
* this.types(['html']);
* // => ["html"]
* this.types(['text/html']);
* // => ["text/html"]
* this.types(['json', 'text']);
* // => ["json"]
* this.types(['application/json']);
* // => ["application/json"]
*
* // Accept: text/*, application/json
* this.types(['image/png']);
* this.types(['png']);
* // => []
*
* // Accept: text/*;q=.5, application/json
* this.types(['html', 'json']);
* // => ["json"]
*
* @param {Array} types...
* @return {Array|String|False}
* @public
*/
types(types?: string[]): string[] | string | false {
// no types, return all requested types
if (!types || types.length === 0) {
return this.negotiator.mediaTypes();
}
// no accept header, return first given type
if (!this.headers.get("accept")) {
return types[0];
}
const mimes = types
.map(extToMime);
const accepts = this.negotiator.mediaTypes(
(mimes.filter((t) => t && validMime(t))) as string[],
);
const first = accepts[0];
return first ? types[mimes.indexOf(first)] : false;
}
/**
* Return accepted encodings or best fit based on `encodings`.
*
* Given `Accept-Encoding: gzip, deflate`
* an array sorted by quality is returned:
*
* ['gzip', 'deflate']
*
* @param {Array} encodings...
* @return {Array|String|False}
* @public
*/
encodings(encodings?: string[]): string[] | string | false {
// no encodings, return all requested encodings
if (!encodings || encodings.length === 0) {
return this.negotiator.encodings();
}
return this.negotiator.encodings(encodings)[0] || false;
}
/**
* 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']
*
* @param {Array} charsets...
* @return {Array|String|False}
* @public
*/
charsets(charsets?: string[]): string[] | string | false {
// no charsets, return all requested charsets
if (!charsets || charsets.length === 0) {
return this.negotiator.charsets();
}
return this.negotiator.charsets(charsets)[0] || false;
}
/**
* 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']
*
* @param {Array} langs...
* @return {Array|String|False}
* @public
*/
languages(languages?: string[]): string[] | string | false {
// no languages, return all requested languages
if (!languages || languages.length === 0) {
return this.negotiator.languages();
}
return this.negotiator.languages(languages)[0] || false;
}
}
/**
* Convert extnames to mime.
*
* @param {String} type
* @return {String|undefined}
* @private
*/
function extToMime(type: string): string | undefined {
return type.indexOf("/") === -1 ? lookup(type) : type;
}
/**
* Check if mime is valid.
*
* @param {String} type
* @return {String}
* @private
*/
function validMime(type?: string): boolean {
return typeof type === "string";
}