[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | import utils from '../utils.js';
|
---|
| 4 | import parseHeaders from '../helpers/parseHeaders.js';
|
---|
| 5 |
|
---|
| 6 | const $internals = Symbol('internals');
|
---|
| 7 |
|
---|
| 8 | function normalizeHeader(header) {
|
---|
| 9 | return header && String(header).trim().toLowerCase();
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | function 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 |
|
---|
| 20 | function 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 |
|
---|
| 32 | const isValidHeaderName = (str) => /^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(str.trim());
|
---|
| 33 |
|
---|
| 34 | function 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 |
|
---|
| 54 | function 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 |
|
---|
| 61 | function 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 |
|
---|
| 74 | class 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 {
|
---|
| 104 | header != null && setHeader(valueOrRewrite, header, rewrite);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | return this;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | get(header, parser) {
|
---|
| 111 | header = normalizeHeader(header);
|
---|
| 112 |
|
---|
| 113 | if (header) {
|
---|
| 114 | const key = utils.findKey(this, header);
|
---|
| 115 |
|
---|
| 116 | if (key) {
|
---|
| 117 | const value = this[key];
|
---|
| 118 |
|
---|
| 119 | if (!parser) {
|
---|
| 120 | return value;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | if (parser === true) {
|
---|
| 124 | return parseTokens(value);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | if (utils.isFunction(parser)) {
|
---|
| 128 | return parser.call(this, value, key);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | if (utils.isRegExp(parser)) {
|
---|
| 132 | return parser.exec(value);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | throw new TypeError('parser must be boolean|regexp|function');
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | has(header, matcher) {
|
---|
| 141 | header = normalizeHeader(header);
|
---|
| 142 |
|
---|
| 143 | if (header) {
|
---|
| 144 | const key = utils.findKey(this, header);
|
---|
| 145 |
|
---|
| 146 | return !!(key && this[key] !== undefined && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return false;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | delete(header, matcher) {
|
---|
| 153 | const self = this;
|
---|
| 154 | let deleted = false;
|
---|
| 155 |
|
---|
| 156 | function deleteHeader(_header) {
|
---|
| 157 | _header = normalizeHeader(_header);
|
---|
| 158 |
|
---|
| 159 | if (_header) {
|
---|
| 160 | const key = utils.findKey(self, _header);
|
---|
| 161 |
|
---|
| 162 | if (key && (!matcher || matchHeaderValue(self, self[key], key, matcher))) {
|
---|
| 163 | delete self[key];
|
---|
| 164 |
|
---|
| 165 | deleted = true;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | if (utils.isArray(header)) {
|
---|
| 171 | header.forEach(deleteHeader);
|
---|
| 172 | } else {
|
---|
| 173 | deleteHeader(header);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | return deleted;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | clear(matcher) {
|
---|
| 180 | const keys = Object.keys(this);
|
---|
| 181 | let i = keys.length;
|
---|
| 182 | let deleted = false;
|
---|
| 183 |
|
---|
| 184 | while (i--) {
|
---|
| 185 | const key = keys[i];
|
---|
| 186 | if(!matcher || matchHeaderValue(this, this[key], key, matcher, true)) {
|
---|
| 187 | delete this[key];
|
---|
| 188 | deleted = true;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | return deleted;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | normalize(format) {
|
---|
| 196 | const self = this;
|
---|
| 197 | const headers = {};
|
---|
| 198 |
|
---|
| 199 | utils.forEach(this, (value, header) => {
|
---|
| 200 | const key = utils.findKey(headers, header);
|
---|
| 201 |
|
---|
| 202 | if (key) {
|
---|
| 203 | self[key] = normalizeValue(value);
|
---|
| 204 | delete self[header];
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | const normalized = format ? formatHeader(header) : String(header).trim();
|
---|
| 209 |
|
---|
| 210 | if (normalized !== header) {
|
---|
| 211 | delete self[header];
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | self[normalized] = normalizeValue(value);
|
---|
| 215 |
|
---|
| 216 | headers[normalized] = true;
|
---|
| 217 | });
|
---|
| 218 |
|
---|
| 219 | return this;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | concat(...targets) {
|
---|
| 223 | return this.constructor.concat(this, ...targets);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | toJSON(asStrings) {
|
---|
| 227 | const obj = Object.create(null);
|
---|
| 228 |
|
---|
| 229 | utils.forEach(this, (value, header) => {
|
---|
| 230 | value != null && value !== false && (obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value);
|
---|
| 231 | });
|
---|
| 232 |
|
---|
| 233 | return obj;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | [Symbol.iterator]() {
|
---|
| 237 | return Object.entries(this.toJSON())[Symbol.iterator]();
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | toString() {
|
---|
| 241 | return Object.entries(this.toJSON()).map(([header, value]) => header + ': ' + value).join('\n');
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | get [Symbol.toStringTag]() {
|
---|
| 245 | return 'AxiosHeaders';
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | static from(thing) {
|
---|
| 249 | return thing instanceof this ? thing : new this(thing);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | static concat(first, ...targets) {
|
---|
| 253 | const computed = new this(first);
|
---|
| 254 |
|
---|
| 255 | targets.forEach((target) => computed.set(target));
|
---|
| 256 |
|
---|
| 257 | return computed;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | static accessor(header) {
|
---|
| 261 | const internals = this[$internals] = (this[$internals] = {
|
---|
| 262 | accessors: {}
|
---|
| 263 | });
|
---|
| 264 |
|
---|
| 265 | const accessors = internals.accessors;
|
---|
| 266 | const prototype = this.prototype;
|
---|
| 267 |
|
---|
| 268 | function defineAccessor(_header) {
|
---|
| 269 | const lHeader = normalizeHeader(_header);
|
---|
| 270 |
|
---|
| 271 | if (!accessors[lHeader]) {
|
---|
| 272 | buildAccessors(prototype, _header);
|
---|
| 273 | accessors[lHeader] = true;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
|
---|
| 278 |
|
---|
| 279 | return this;
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent', 'Authorization']);
|
---|
| 284 |
|
---|
| 285 | // reserved names hotfix
|
---|
| 286 | utils.reduceDescriptors(AxiosHeaders.prototype, ({value}, key) => {
|
---|
| 287 | let mapped = key[0].toUpperCase() + key.slice(1); // map `set` => `Set`
|
---|
| 288 | return {
|
---|
| 289 | get: () => value,
|
---|
| 290 | set(headerValue) {
|
---|
| 291 | this[mapped] = headerValue;
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | });
|
---|
| 295 |
|
---|
| 296 | utils.freezeMethods(AxiosHeaders);
|
---|
| 297 |
|
---|
| 298 | export default AxiosHeaders;
|
---|