source: imaps-frontend/node_modules/axios/lib/core/AxiosHeaders.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 6.9 KB
Line 
1'use strict';
2
3import utils from '../utils.js';
4import parseHeaders from '../helpers/parseHeaders.js';
5
6const $internals = Symbol('internals');
7
8function normalizeHeader(header) {
9 return header && String(header).trim().toLowerCase();
10}
11
12function normalizeValue(value) {
13 if (value === false || value == null) {
14 return value;
15 }
16
17 return utils.isArray(value) ? value.map(normalizeValue) : String(value);
18}
19
20function parseTokens(str) {
21 const tokens = Object.create(null);
22 const tokensRE = /([^\s,;=]+)\s*(?:=\s*([^,;]+))?/g;
23 let match;
24
25 while ((match = tokensRE.exec(str))) {
26 tokens[match[1]] = match[2];
27 }
28
29 return tokens;
30}
31
32const isValidHeaderName = (str) => /^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(str.trim());
33
34function matchHeaderValue(context, value, header, filter, isHeaderNameFilter) {
35 if (utils.isFunction(filter)) {
36 return filter.call(this, value, header);
37 }
38
39 if (isHeaderNameFilter) {
40 value = header;
41 }
42
43 if (!utils.isString(value)) return;
44
45 if (utils.isString(filter)) {
46 return value.indexOf(filter) !== -1;
47 }
48
49 if (utils.isRegExp(filter)) {
50 return filter.test(value);
51 }
52}
53
54function formatHeader(header) {
55 return header.trim()
56 .toLowerCase().replace(/([a-z\d])(\w*)/g, (w, char, str) => {
57 return char.toUpperCase() + str;
58 });
59}
60
61function buildAccessors(obj, header) {
62 const accessorName = utils.toCamelCase(' ' + header);
63
64 ['get', 'set', 'has'].forEach(methodName => {
65 Object.defineProperty(obj, methodName + accessorName, {
66 value: function(arg1, arg2, arg3) {
67 return this[methodName].call(this, header, arg1, arg2, arg3);
68 },
69 configurable: true
70 });
71 });
72}
73
74class AxiosHeaders {
75 constructor(headers) {
76 headers && this.set(headers);
77 }
78
79 set(header, valueOrRewrite, rewrite) {
80 const self = this;
81
82 function setHeader(_value, _header, _rewrite) {
83 const lHeader = normalizeHeader(_header);
84
85 if (!lHeader) {
86 throw new Error('header name must be a non-empty string');
87 }
88
89 const key = utils.findKey(self, lHeader);
90
91 if(!key || self[key] === undefined || _rewrite === true || (_rewrite === undefined && self[key] !== false)) {
92 self[key || _header] = normalizeValue(_value);
93 }
94 }
95
96 const setHeaders = (headers, _rewrite) =>
97 utils.forEach(headers, (_value, _header) => setHeader(_value, _header, _rewrite));
98
99 if (utils.isPlainObject(header) || header instanceof this.constructor) {
100 setHeaders(header, valueOrRewrite)
101 } else if(utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
102 setHeaders(parseHeaders(header), valueOrRewrite);
103 } else if (utils.isHeaders(header)) {
104 for (const [key, value] of header.entries()) {
105 setHeader(value, key, rewrite);
106 }
107 } else {
108 header != null && setHeader(valueOrRewrite, header, rewrite);
109 }
110
111 return this;
112 }
113
114 get(header, parser) {
115 header = normalizeHeader(header);
116
117 if (header) {
118 const key = utils.findKey(this, header);
119
120 if (key) {
121 const value = this[key];
122
123 if (!parser) {
124 return value;
125 }
126
127 if (parser === true) {
128 return parseTokens(value);
129 }
130
131 if (utils.isFunction(parser)) {
132 return parser.call(this, value, key);
133 }
134
135 if (utils.isRegExp(parser)) {
136 return parser.exec(value);
137 }
138
139 throw new TypeError('parser must be boolean|regexp|function');
140 }
141 }
142 }
143
144 has(header, matcher) {
145 header = normalizeHeader(header);
146
147 if (header) {
148 const key = utils.findKey(this, header);
149
150 return !!(key && this[key] !== undefined && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
151 }
152
153 return false;
154 }
155
156 delete(header, matcher) {
157 const self = this;
158 let deleted = false;
159
160 function deleteHeader(_header) {
161 _header = normalizeHeader(_header);
162
163 if (_header) {
164 const key = utils.findKey(self, _header);
165
166 if (key && (!matcher || matchHeaderValue(self, self[key], key, matcher))) {
167 delete self[key];
168
169 deleted = true;
170 }
171 }
172 }
173
174 if (utils.isArray(header)) {
175 header.forEach(deleteHeader);
176 } else {
177 deleteHeader(header);
178 }
179
180 return deleted;
181 }
182
183 clear(matcher) {
184 const keys = Object.keys(this);
185 let i = keys.length;
186 let deleted = false;
187
188 while (i--) {
189 const key = keys[i];
190 if(!matcher || matchHeaderValue(this, this[key], key, matcher, true)) {
191 delete this[key];
192 deleted = true;
193 }
194 }
195
196 return deleted;
197 }
198
199 normalize(format) {
200 const self = this;
201 const headers = {};
202
203 utils.forEach(this, (value, header) => {
204 const key = utils.findKey(headers, header);
205
206 if (key) {
207 self[key] = normalizeValue(value);
208 delete self[header];
209 return;
210 }
211
212 const normalized = format ? formatHeader(header) : String(header).trim();
213
214 if (normalized !== header) {
215 delete self[header];
216 }
217
218 self[normalized] = normalizeValue(value);
219
220 headers[normalized] = true;
221 });
222
223 return this;
224 }
225
226 concat(...targets) {
227 return this.constructor.concat(this, ...targets);
228 }
229
230 toJSON(asStrings) {
231 const obj = Object.create(null);
232
233 utils.forEach(this, (value, header) => {
234 value != null && value !== false && (obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value);
235 });
236
237 return obj;
238 }
239
240 [Symbol.iterator]() {
241 return Object.entries(this.toJSON())[Symbol.iterator]();
242 }
243
244 toString() {
245 return Object.entries(this.toJSON()).map(([header, value]) => header + ': ' + value).join('\n');
246 }
247
248 get [Symbol.toStringTag]() {
249 return 'AxiosHeaders';
250 }
251
252 static from(thing) {
253 return thing instanceof this ? thing : new this(thing);
254 }
255
256 static concat(first, ...targets) {
257 const computed = new this(first);
258
259 targets.forEach((target) => computed.set(target));
260
261 return computed;
262 }
263
264 static accessor(header) {
265 const internals = this[$internals] = (this[$internals] = {
266 accessors: {}
267 });
268
269 const accessors = internals.accessors;
270 const prototype = this.prototype;
271
272 function defineAccessor(_header) {
273 const lHeader = normalizeHeader(_header);
274
275 if (!accessors[lHeader]) {
276 buildAccessors(prototype, _header);
277 accessors[lHeader] = true;
278 }
279 }
280
281 utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
282
283 return this;
284 }
285}
286
287AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent', 'Authorization']);
288
289// reserved names hotfix
290utils.reduceDescriptors(AxiosHeaders.prototype, ({value}, key) => {
291 let mapped = key[0].toUpperCase() + key.slice(1); // map `set` => `Set`
292 return {
293 get: () => value,
294 set(headerValue) {
295 this[mapped] = headerValue;
296 }
297 }
298});
299
300utils.freezeMethods(AxiosHeaders);
301
302export default AxiosHeaders;
Note: See TracBrowser for help on using the repository browser.