This commit is contained in:
Mrhongxing committed 2026-11-12 02:48:46 +08:00
1 parent d1ee8edbbd
commit 7733a44362
429 files changed
+75840 -94

No files matched your search

+361
View File
@@ -0,0 +1,361 @@
'use strict';
import utils from '../utils.js';
import buildURL from '../helpers/buildURL.js';
import InterceptorManager from './InterceptorManager.js';
import dispatchRequest from './dispatchRequest.js';
import mergeConfig from './mergeConfig.js';
import buildFullPath from './buildFullPath.js';
import methodList from './methodList.js';
import validator from '../helpers/validator.js';
import AxiosHeaders from './AxiosHeaders.js';
import transitionalDefaults from '../defaults/transitional.js';
import { getCauseStackIndex } from './AxiosError.js';
const validators = validator.validators;
// Recognize header maps without consuming objects intended as literal values.
// AxiosHeaders from other builds share a brand and header-container interface,
// but not constructor identity. A tag and toJSON alone can describe a literal.
const isHeadersBucket = (value) => {
if (value == null || utils.isPlainObject(value) || value instanceof AxiosHeaders) {
return true;
}
try {
return (
utils.isObject(value) &&
utils.getSafeProp(value, Symbol.toStringTag) === 'AxiosHeaders' &&
[
'toJSON',
'set',
'get',
'has',
'delete',
'clear',
'normalize',
'concat',
'toString',
Symbol.iterator,
].every((method) => utils.isFunction(utils.getSafeProp(value, method)))
);
} catch (e) {
// A failed optional brand probe must not prevent literal header serialization.
return false;
}
};
/**
* Create a new instance of Axios
*
* @param {Object} instanceConfig The default config for the instance
*
* @return {Axios} A new instance of Axios
*/
class Axios {
constructor(instanceConfig) {
this.defaults = instanceConfig || {};
this.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager(),
};
}
/**
* Dispatch a request
*
* @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
* @param {?Object} config
*
* @returns {Promise} The Promise to be fulfilled
*/
async request(configOrUrl, config) {
try {
return await this._request(configOrUrl, config);
} catch (err) {
if (err instanceof Error) {
try {
let dummy = {};
Error.captureStackTrace ? Error.captureStackTrace(dummy) : (dummy = new Error());
const dummyStack = dummy.stack;
let stack = '';
// slice off the Error: ... line
if (typeof dummyStack === 'string') {
const firstNewlineIndex = dummyStack.indexOf('\n');
stack = firstNewlineIndex === -1 ? '' : dummyStack.slice(firstNewlineIndex + 1);
}
if (!err.stack) {
err.stack = stack;
} else if (stack) {
// Match a common suffix after the 2 top frames when available,
// otherwise compare the full reconstructed stack.
const firstNewlineIndex = stack.indexOf('\n');
const secondNewlineIndex =
firstNewlineIndex === -1 ? -1 : stack.indexOf('\n', firstNewlineIndex + 1);
const duplicateCheckSuffix =
secondNewlineIndex === -1 ? stack : stack.slice(secondNewlineIndex + 1);
// `AxiosError.from()` may have already inlined the wrapped error's
// stack under a `Caused by:` marker. Keep the reconstructed caller
// frames ahead of that marker so stack-only reporters do not read
// them as part of the low-level cause.
const currentStack = String(err.stack);
const causeIndex = getCauseStackIndex(err, currentStack);
const stackHead = causeIndex === -1 ? currentStack : currentStack.slice(0, causeIndex);
const causeSection = causeIndex === -1 ? '' : currentStack.slice(causeIndex);
if (!stackHead.endsWith(duplicateCheckSuffix)) {
err.stack = stackHead + '\n' + stack + causeSection;
}
}
} catch (e) {
// Ignore failures from custom stack hooks or un-writable stack properties.
}
}
throw err;
}
}
_request(configOrUrl, config) {
/*eslint no-param-reassign:0*/
// Allow for axios('example/url'[, config]) a la fetch API
if (typeof configOrUrl === 'string') {
config = config || {};
config.url = configOrUrl;
} else {
config = configOrUrl || {};
}
config = mergeConfig(this.defaults, config);
const { transitional, paramsSerializer, headers } = config;
if (transitional !== undefined) {
validator.assertOptions(
transitional,
{
silentJSONParsing: validators.transitional(validators.boolean),
forcedJSONParsing: validators.transitional(validators.boolean),
clarifyTimeoutError: validators.transitional(validators.boolean),
legacyInterceptorReqResOrdering: validators.transitional(validators.boolean),
advertiseZstdAcceptEncoding: validators.transitional(validators.boolean),
validateStatusUndefinedResolves: validators.transitional(validators.boolean),
encodeBasicAuthUsername: validators.transitional(validators.boolean),
},
false
);
}
if (paramsSerializer != null) {
if (utils.isFunction(paramsSerializer)) {
config.paramsSerializer = {
serialize: paramsSerializer,
};
} else {
validator.assertOptions(
paramsSerializer,
{
encode: validators.function,
serialize: validators.function,
},
true
);
}
}
// Set config.allowAbsoluteUrls
if (config.allowAbsoluteUrls !== undefined) {
// do nothing
} else if (
utils.hasOwnProp(this.defaults, 'allowAbsoluteUrls') &&
this.defaults.allowAbsoluteUrls !== undefined
) {
config.allowAbsoluteUrls = this.defaults.allowAbsoluteUrls;
} else {
config.allowAbsoluteUrls = true;
}
validator.assertOptions(
config,
{
baseUrl: validators.spelling('baseURL'),
withXsrfToken: validators.spelling('withXSRFToken'),
},
true
);
// Set config.method
config.method = (
utils.getSafeProp(config, 'method') ||
utils.getSafeProp(this.defaults, 'method') ||
'get'
).toLowerCase();
// Flatten header defaults without consuming literal method-named headers.
const commonHeaders = utils.getSafeProp(headers, 'common');
const methodHeaders = utils.getSafeProp(headers, config.method);
const contextHeaders =
headers &&
utils.merge(
isHeadersBucket(commonHeaders) ? commonHeaders : undefined,
isHeadersBucket(methodHeaders) ? methodHeaders : undefined
);
headers &&
utils.forEach(methodList.concat('common'), (method) => {
if (isHeadersBucket(utils.getSafeProp(headers, method))) {
delete headers[method];
}
});
config.headers = AxiosHeaders.concat(contextHeaders, headers);
// filter out skipped interceptors
const requestInterceptorChain = [];
let synchronousRequestInterceptors = true;
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
return;
}
synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
const transitional = config.transitional || transitionalDefaults;
const legacyInterceptorReqResOrdering =
transitional && transitional.legacyInterceptorReqResOrdering;
if (legacyInterceptorReqResOrdering) {
requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
} else {
requestInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
}
});
const responseInterceptorChain = [];
this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
});
let promise;
let i = 0;
let len;
if (!synchronousRequestInterceptors) {
const chain = [dispatchRequest.bind(this), undefined];
chain.unshift(...requestInterceptorChain);
chain.push(...responseInterceptorChain);
len = chain.length;
promise = Promise.resolve(config);
while (i < len) {
promise = promise.then(chain[i++], chain[i++]);
}
return promise;
}
len = requestInterceptorChain.length;
let newConfig = config;
while (i < len) {
const onFulfilled = requestInterceptorChain[i++];
const onRejected = requestInterceptorChain[i++];
try {
newConfig = onFulfilled ? onFulfilled(newConfig) : newConfig;
} catch (error) {
if (!onRejected) {
promise = Promise.reject(error);
break;
}
try {
const rejectedResult = onRejected.call(this, error);
if (utils.isThenable(rejectedResult)) {
promise = Promise.resolve(rejectedResult).then(() =>
dispatchRequest.call(this, newConfig)
);
}
} catch (rejectedError) {
promise = Promise.reject(rejectedError);
}
break;
}
}
if (!promise) {
try {
promise = dispatchRequest.call(this, newConfig);
} catch (error) {
promise = Promise.reject(error);
}
}
i = 0;
len = responseInterceptorChain.length;
while (i < len) {
promise = promise.then(responseInterceptorChain[i++], responseInterceptorChain[i++]);
}
return promise;
}
getUri(config) {
config = mergeConfig(this.defaults, config);
const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls, config);
return buildURL(fullPath, config.params, config.paramsSerializer);
}
}
// Provide aliases for supported request methods
utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
/*eslint func-names:0*/
Axios.prototype[method] = async function (url, config) {
return this.request(
mergeConfig(config || {}, {
method,
url,
data: config && utils.hasOwnProp(config, 'data') ? config.data : undefined,
})
);
};
});
utils.forEach(['post', 'put', 'patch', 'query'], function forEachMethodWithData(method) {
function generateHTTPMethod(isForm) {
return async function httpMethod(url, data, config) {
return this.request(
mergeConfig(config || {}, {
method,
headers: isForm
? {
'Content-Type': 'multipart/form-data',
}
: {},
url,
data,
})
);
};
}
Axios.prototype[method] = generateHTTPMethod();
// QUERY is a safe/idempotent read method; multipart form bodies don't fit
// its semantics, so no queryForm shorthand is generated.
if (method !== 'query') {
Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
}
});
export default Axios;
+278
View File
@@ -0,0 +1,278 @@
'use strict';
import utils from '../utils.js';
import AxiosHeaders from './AxiosHeaders.js';
export const REDACTED = '[REDACTED ****]';
function hasOwnOrPrototypeToJSON(source) {
if (utils.hasOwnProp(source, 'toJSON')) {
return true;
}
let prototype = Object.getPrototypeOf(source);
while (prototype && prototype !== Object.prototype) {
if (utils.hasOwnProp(prototype, 'toJSON')) {
return true;
}
prototype = Object.getPrototypeOf(prototype);
}
return false;
}
// Build a plain-object snapshot of `config` and replace the value of any key
// (case-insensitive) listed in `redactKeys` with REDACTED. Walks through arrays
// and AxiosHeaders, and short-circuits on circular references.
function redactConfig(config, redactKeys) {
const lowerKeys = new Set(redactKeys.map((k) => String(k).toLowerCase()));
const seen = [];
const visit = (source) => {
if (source === null || typeof source !== 'object') return source;
if (utils.isBuffer(source)) return source;
if (seen.indexOf(source) !== -1) return undefined;
if (source instanceof AxiosHeaders) {
source = source.toJSON();
}
seen.push(source);
let result;
if (utils.isArray(source)) {
result = [];
source.forEach((v, i) => {
const reducedValue = visit(v);
if (!utils.isUndefined(reducedValue)) {
result[i] = reducedValue;
}
});
} else {
if (!utils.isPlainObject(source) && hasOwnOrPrototypeToJSON(source)) {
seen.pop();
return source;
}
result = Object.create(null);
for (const [key, value] of Object.entries(source)) {
const reducedValue = lowerKeys.has(key.toLowerCase()) ? REDACTED : visit(value);
if (!utils.isUndefined(reducedValue)) {
result[key] = reducedValue;
}
}
}
seen.pop();
return result;
};
return visit(config);
}
function stringifySafely(value) {
try {
return String(value);
} catch (err) {
return '';
}
}
function aggregateErrorMessage(error) {
const message = error.errors
.map((entry) => {
try {
return entry && entry.message ? stringifySafely(entry.message) : stringifySafely(entry);
} catch (err) {
return '';
}
})
.filter(Boolean)
.join('; ');
return message || error.name || 'AggregateError';
}
// Shared with `Axios.prototype.request`, which splices the reconstructed
// caller frames in ahead of this marker.
export const CAUSED_BY_SEPARATOR = '\nCaused by: ';
const $causeStack = Symbol('causeStack');
// Match the section we appended, not marker-like text in an error message or
// a replacement stack supplied by the caller. Do not re-read the cause's stack.
export function getCauseStackIndex(error, stack) {
const causeStack = utils.hasOwnProp(error, $causeStack) && error[$causeStack];
return causeStack && stack.endsWith(causeStack) ? stack.length - causeStack.length : -1;
}
/**
* Append the stack of a wrapped error to `target.stack`.
*
* `cause` already exposes the original error for tooling that walks the standard
* cause chain, but many error reporters read `stack` and nothing else. Inlining
* the wrapped stack there means those reporters surface the underlying failure
* instead of the axios frames that normalized it (see #6670).
*
* Every read is defensive: a custom `Error.prepareStackTrace` hook, a runtime
* without stack support, or a replaced `stack` property can leave the value
* missing, non-string, or read-only, and normalizing an error must not throw
* because of that.
*
* @param {Error} target The AxiosError being built.
* @param {Error} source The error being wrapped.
*
* @returns {void}
*/
function appendCauseStack(target, source) {
try {
const sourceStack = source && source.stack;
if (typeof sourceStack !== 'string' || !sourceStack) {
return;
}
const targetStack = target.stack;
if (typeof targetStack !== 'string' || !targetStack) {
return;
}
// Each call to `from()` creates a fresh target. Re-wrapping includes the
// previous stack verbatim, adding one cause section per wrap.
const causeStack = CAUSED_BY_SEPARATOR + sourceStack;
target.stack = targetStack + causeStack;
Object.defineProperty(target, $causeStack, { __proto__: null, value: causeStack });
} catch (e) {
// Ignore failures from custom stack hooks or un-writable stack properties.
}
}
class AxiosError extends Error {
static from(error, code, config, request, response, customProps) {
// `AggregateError` (thrown by Node on dual-stack/Happy-Eyeballs connection
// failures) has an empty `message`; its detail lives in `errors[]`. Without
// this, the wrapped error surfaces with a blank message (see #6721).
let message = error.message;
if (!message && utils.isArray(error.errors) && error.errors.length) {
message = aggregateErrorMessage(error);
}
const axiosError = new AxiosError(message, code || error.code, config, request, response);
// Match native `Error` `cause` semantics: non-enumerable. The wrapped
// error often carries circular internals (sockets, requests, agents), so
// an enumerable `cause` makes structured loggers (pino/winston) and any
// own-property walk throw "Converting circular structure to JSON".
// Regression from #6982; see #7205. `__proto__: null` mirrors the
// `message` descriptor below (prototype-pollution-safe descriptor).
Object.defineProperty(axiosError, 'cause', {
__proto__: null,
value: error,
writable: true,
enumerable: false,
configurable: true,
});
axiosError.name = error.name;
// Inline the wrapped stack for reporters that read only `stack`; the `cause`
// property above already covers cause-aware tooling.
appendCauseStack(axiosError, error);
// Preserve status from the original error if not already set from response
if (error.status != null && axiosError.status == null) {
axiosError.status = error.status;
}
customProps && Object.assign(axiosError, customProps);
return axiosError;
}
/**
* Create an Error with the specified message, config, error code, request and response.
*
* @param {string} message The error message.
* @param {string} [code] The error code (for example, 'ECONNABORTED').
* @param {Object} [config] The config.
* @param {Object} [request] The request.
* @param {Object} [response] The response.
*
* @returns {Error} The created error.
*/
constructor(message, code, config, request, response) {
super(message);
// Make message enumerable to maintain backward compatibility
// The native Error constructor sets message as non-enumerable,
// but axios < v1.13.3 had it as enumerable
Object.defineProperty(this, 'message', {
// Null-proto descriptor so a polluted Object.prototype.get cannot turn
// this data descriptor into an accessor descriptor on the way in.
__proto__: null,
value: message,
enumerable: true,
writable: true,
configurable: true,
});
this.name = 'AxiosError';
this.isAxiosError = true;
code && (this.code = code);
config && (this.config = config);
request && (this.request = request);
if (response) {
this.response = response;
this.status = response.status;
}
}
toJSON() {
// Opt-in redaction: when the request config carries a `redact` array, the
// value of any matching key (case-insensitive, at any depth) is replaced
// with REDACTED in the serialized snapshot. Undefined or empty leaves the
// existing serialization behavior unchanged.
const config = this.config;
const redactKeys = config && utils.hasOwnProp(config, 'redact') ? config.redact : undefined;
const serializedConfig =
utils.isArray(redactKeys) && redactKeys.length > 0
? redactConfig(config, redactKeys)
: utils.toJSONObject(config);
return {
// Standard
message: this.message,
name: this.name,
// Microsoft
description: this.description,
number: this.number,
// Mozilla
fileName: this.fileName,
lineNumber: this.lineNumber,
columnNumber: this.columnNumber,
stack: this.stack,
// Axios
config: serializedConfig,
code: this.code,
status: this.status,
};
}
}
// This can be changed to static properties as soon as the parser options in .eslint.cjs are updated.
AxiosError.ERR_BAD_OPTION_VALUE = 'ERR_BAD_OPTION_VALUE';
AxiosError.ERR_BAD_OPTION = 'ERR_BAD_OPTION';
AxiosError.ECONNABORTED = 'ECONNABORTED';
AxiosError.ETIMEDOUT = 'ETIMEDOUT';
AxiosError.ECONNREFUSED = 'ECONNREFUSED';
AxiosError.ERR_NETWORK = 'ERR_NETWORK';
AxiosError.ERR_FR_TOO_MANY_REDIRECTS = 'ERR_FR_TOO_MANY_REDIRECTS';
AxiosError.ERR_DEPRECATED = 'ERR_DEPRECATED';
AxiosError.ERR_BAD_RESPONSE = 'ERR_BAD_RESPONSE';
AxiosError.ERR_BAD_REQUEST = 'ERR_BAD_REQUEST';
AxiosError.ERR_CANCELED = 'ERR_CANCELED';
AxiosError.ERR_NOT_SUPPORT = 'ERR_NOT_SUPPORT';
AxiosError.ERR_INVALID_URL = 'ERR_INVALID_URL';
AxiosError.ERR_FORM_DATA_DEPTH_EXCEEDED = 'ERR_FORM_DATA_DEPTH_EXCEEDED';
export default AxiosError;
+487
View File
@@ -0,0 +1,487 @@
'use strict';
import utils from '../utils.js';
import parseHeaders from '../helpers/parseHeaders.js';
import { sanitizeHeaderValue } from '../helpers/sanitizeHeaderValue.js';
const $internals = Symbol('internals');
function normalizeHeader(header) {
return header && String(header).trim().toLowerCase();
}
function normalizeValue(value) {
if (value === false || value == null) {
return value;
}
return utils.isArray(value) ? value.map(normalizeValue) : sanitizeHeaderValue(String(value));
}
function parseTokens(str) {
const tokens = Object.create(null);
const tokensRE = /([^\s,;=]+)\s*(?:=\s*([^,;]+))?/g;
let match;
while ((match = tokensRE.exec(str))) {
tokens[match[1]] = match[2];
}
return tokens;
}
const parameterNameRE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
function trimOWS(value) {
let start = 0;
let end = value.length;
while (start < end) {
const code = value.charCodeAt(start);
if (code !== 0x09 && code !== 0x20) {
break;
}
start += 1;
}
while (end > start) {
const code = value.charCodeAt(end - 1);
if (code !== 0x09 && code !== 0x20) {
break;
}
end -= 1;
}
return start === 0 && end === value.length ? value : value.slice(start, end);
}
function decodeQuotedString(value) {
const last = value.length - 1;
if (last < 1 || value.charCodeAt(0) !== 0x22 || value.charCodeAt(last) !== 0x22) {
return value;
}
let decoded = '';
for (let i = 1; i < last; i++) {
const code = value.charCodeAt(i);
if (code === 0x22) {
return value;
}
if (code === 0x5c) {
i += 1;
if (i >= last) {
return value;
}
}
decoded += value[i];
}
return decoded;
}
function parseParameters(value) {
const parameters = Object.create(null);
const str = String(value);
let start = 0;
let quoted = false;
let escaped = false;
function parseParameter(end) {
const part = trimOWS(str.slice(start, end));
const equals = part.indexOf('=');
if (equals < 1) {
return;
}
const name = trimOWS(part.slice(0, equals));
if (!parameterNameRE.test(name)) {
return;
}
const normalizedName = name.toLowerCase();
if (
normalizedName === '__proto__' ||
normalizedName === 'constructor' ||
normalizedName === 'prototype'
) {
return;
}
const parameterValue = trimOWS(part.slice(equals + 1));
parameters[normalizedName] = decodeQuotedString(parameterValue);
}
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
if (quoted) {
if (escaped) {
escaped = false;
} else if (code === 0x5c) {
escaped = true;
} else if (code === 0x22) {
quoted = false;
}
} else if (code === 0x22) {
quoted = true;
} else if (code === 0x2c || code === 0x3b) {
parseParameter(i);
start = i + 1;
}
}
parseParameter(str.length);
return parameters;
}
const isValidHeaderName = (str) => /^[-_a-zA-Z0-9^`|~,!#$%&'*+.]+$/.test(str.trim());
// `__proto__` is a valid field name, and the one name the uppercase mapping at
// the bottom of this file cannot reach: it is inherited from Object.prototype
// instead of being declared on AxiosHeaders.prototype, so `self.__proto__ = v`
// runs the inherited setter. That silently discards the header, and an array
// value replaces the instance prototype, taking every method with it.
// Store it under the same kind of case variant that mapping uses for `set` and
// `constructor`. Header lookup is caseless, so it reads back under either
// spelling, and no own `__proto__` slot is ever created for a later
// `Object.assign` to copy onto a plain object through the same setter.
const PROTO_HEADER_SLOT = '__Proto__';
const headerSlot = (name) => (name === '__proto__' ? PROTO_HEADER_SLOT : name);
function matchHeaderValue(context, value, header, filter, isHeaderNameFilter) {
if (utils.isFunction(filter)) {
return filter.call(context, value, header);
}
if (isHeaderNameFilter) {
value = header;
}
if (!utils.isString(value)) return;
if (utils.isString(filter)) {
return value.indexOf(filter) !== -1;
}
if (utils.isRegExp(filter)) {
return filter.test(value);
}
}
function formatHeader(header) {
return header
.trim()
.toLowerCase()
.replace(/([a-z\d])(\w*)/g, (w, char, str) => {
return char.toUpperCase() + str;
});
}
function buildAccessors(obj, header) {
const accessorName = utils.toCamelCase(' ' + header);
['get', 'set', 'has'].forEach((methodName) => {
Object.defineProperty(obj, methodName + accessorName, {
// Null-proto descriptor so a polluted Object.prototype.get cannot turn
// this data descriptor into an accessor descriptor on the way in.
__proto__: null,
value: function (arg1, arg2, arg3) {
return this[methodName].call(this, header, arg1, arg2, arg3);
},
configurable: true,
});
});
}
class AxiosHeaders {
constructor(headers) {
headers && this.set(headers);
}
set(header, valueOrRewrite, rewrite) {
const self = this;
function setHeader(_value, _header, _rewrite) {
const lHeader = normalizeHeader(_header);
if (!lHeader) {
return;
}
const key = utils.findKey(self, lHeader);
if (
!key ||
self[key] === undefined ||
_rewrite === true ||
(_rewrite === undefined && self[key] !== false)
) {
self[headerSlot(key || _header)] = normalizeValue(_value);
}
}
const setHeaders = (headers, _rewrite) =>
utils.forEach(headers, (_value, _header) => setHeader(_value, _header, _rewrite));
if (utils.isPlainObject(header) || header instanceof this.constructor) {
setHeaders(header, valueOrRewrite);
} else if (utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
setHeaders(parseHeaders(header), valueOrRewrite);
} else if (utils.isObject(header) && utils.isSafeIterable(header)) {
let obj = Object.create(null),
dest,
key;
for (const entry of header) {
if (!utils.isArray(entry)) {
throw new TypeError('Object iterator must return a key-value pair');
}
key = entry[0];
if (utils.hasOwnProp(obj, key)) {
dest = obj[key];
obj[key] = utils.isArray(dest) ? [...dest, entry[1]] : [dest, entry[1]];
} else {
obj[key] = entry[1];
}
}
setHeaders(obj, valueOrRewrite);
} else {
header != null && setHeader(valueOrRewrite, header, rewrite);
}
return this;
}
get(header, parser) {
header = normalizeHeader(header);
if (header) {
const key = utils.findKey(this, header);
if (key) {
const value = this[key];
if (!parser) {
return value;
}
if (parser === true) {
return parseTokens(value);
}
if (utils.isFunction(parser)) {
return parser.call(this, value, key);
}
if (utils.isRegExp(parser)) {
return parser.exec(value);
}
throw new TypeError('parser must be boolean|regexp|function');
}
}
}
has(header, matcher) {
header = normalizeHeader(header);
if (header) {
const key = utils.findKey(this, header);
return !!(
key &&
this[key] !== undefined &&
(!matcher || matchHeaderValue(this, this[key], key, matcher))
);
}
return false;
}
delete(header, matcher) {
const self = this;
let deleted = false;
function deleteHeader(_header) {
_header = normalizeHeader(_header);
if (_header) {
const key = utils.findKey(self, _header);
if (key && (!matcher || matchHeaderValue(self, self[key], key, matcher))) {
delete self[key];
deleted = true;
}
}
}
if (utils.isArray(header)) {
header.forEach(deleteHeader);
} else {
deleteHeader(header);
}
return deleted;
}
clear(matcher) {
const keys = Object.keys(this);
let i = keys.length;
let deleted = false;
while (i--) {
const key = keys[i];
if (!matcher || matchHeaderValue(this, this[key], key, matcher, true)) {
delete this[key];
deleted = true;
}
}
return deleted;
}
normalize(format) {
const self = this;
const headers = Object.create(null);
utils.forEach(this, (value, header) => {
const key = utils.findKey(headers, header);
if (key) {
self[key] = normalizeValue(value);
delete self[header];
return;
}
const normalized = headerSlot(format ? formatHeader(header) : String(header).trim());
if (normalized !== header) {
delete self[header];
}
self[normalized] = normalizeValue(value);
headers[normalized] = true;
});
return this;
}
concat(...targets) {
return this.constructor.concat(this, ...targets);
}
toJSON(asStrings) {
const obj = Object.create(null);
utils.forEach(this, (value, header) => {
value != null &&
value !== false &&
(obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value);
});
return obj;
}
[Symbol.iterator]() {
return Object.entries(this.toJSON())[Symbol.iterator]();
}
toString() {
return Object.entries(this.toJSON())
.map(([header, value]) => header + ': ' + value)
.join('\n');
}
getSetCookie() {
const value = this.get('set-cookie');
return utils.isArray(value) ? value : value == null || value === false ? [] : [value];
}
get [Symbol.toStringTag]() {
return 'AxiosHeaders';
}
static from(thing) {
return thing instanceof this ? thing : new this(thing);
}
static parseParameters(value) {
return parseParameters(value);
}
static concat(first, ...targets) {
const computed = new this(first);
targets.forEach((target) => computed.set(target));
return computed;
}
static accessor(header) {
const internals =
(this[$internals] =
this[$internals] =
{
accessors: {},
});
const accessors = internals.accessors;
const prototype = this.prototype;
function defineAccessor(_header) {
const lHeader = normalizeHeader(_header);
if (!accessors[lHeader]) {
buildAccessors(prototype, _header);
accessors[lHeader] = true;
}
}
utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
return this;
}
}
AxiosHeaders.accessor([
'Content-Type',
'Content-Length',
'Accept',
'Accept-Encoding',
'User-Agent',
'Authorization',
]);
// reserved names hotfix
utils.reduceDescriptors(AxiosHeaders.prototype, ({ value }, key) => {
let mapped = key[0].toUpperCase() + key.slice(1); // map `set` => `Set`
return {
get: () => value,
set(headerValue) {
this[mapped] = headerValue;
},
};
});
utils.freezeMethods(AxiosHeaders);
export default AxiosHeaders;
+182
View File
@@ -0,0 +1,182 @@
'use strict';
import utils from '../utils.js';
const $internals = Symbol('internals');
// `handlers` is public and may be replaced with a nullish value by user code;
// `clear()` has always tolerated that. Treat it as an empty stack rather than
// dereferencing it.
function countHandlers(handlers) {
return handlers ? handlers.length : 0;
}
function trimHandlers(handlers) {
if (!handlers) {
return;
}
while (handlers.length && handlers[handlers.length - 1] === null) {
handlers.pop();
}
}
// Preserve the array-key coercion used before IDs were stored in a Map, without
// treating null, empty strings, or noncanonical numeric strings as ID zero.
function normalizeHandlerId(id) {
if (typeof id === 'string' && id !== '' && String(Number(id)) === id) {
return Number(id);
}
return id;
}
function syncHandlerEntries(manager, internals) {
const handlers = manager.handlers;
const length = countHandlers(handlers);
if (handlers !== internals.handlersRef) {
internals.handlersRef = handlers;
internals.handlerEntries.clear();
} else if (length !== internals.handlersLength) {
if (!length) {
internals.handlerEntries.clear();
} else {
internals.handlerEntries.forEach(function removeStaleEntry(entry, id) {
if (handlers[entry.index] !== entry.handler) {
internals.handlerEntries.delete(id);
}
});
}
}
internals.handlersLength = length;
}
class InterceptorManager {
constructor() {
this.handlers = [];
this[$internals] = {
handlersRef: this.handlers,
handlersLength: this.handlers.length,
handlerEntries: new Map(),
iterationDepth: 0,
nextId: 0,
};
}
/**
* Add a new interceptor to the stack
*
* @param {Function} fulfilled The function to handle `then` for a `Promise`
* @param {Function} rejected The function to handle `reject` for a `Promise`
* @param {Object} options The options for the interceptor, synchronous and runWhen
*
* @return {Number} An ID used to remove interceptor later
*/
use(fulfilled, rejected, options) {
const handler = {
fulfilled,
rejected,
synchronous: options ? options.synchronous : false,
runWhen: options ? options.runWhen : null,
};
const internals = this[$internals];
if (this.handlers == null) {
this.handlers = [];
}
syncHandlerEntries(this, internals);
const id = internals.nextId++;
this.handlers.push(handler);
internals.handlerEntries.set(id, {
handler,
index: this.handlers.length - 1,
});
internals.handlersLength = this.handlers.length;
return id;
}
/**
* Remove an interceptor from the stack
*
* @param {Number} id The ID that was returned by `use`
*
* @returns {void}
*/
eject(id) {
const internals = this[$internals];
syncHandlerEntries(this, internals);
const handlerId = normalizeHandlerId(id);
const entry = internals.handlerEntries.get(handlerId);
if (entry) {
internals.handlerEntries.delete(handlerId);
// Ignore IDs invalidated by clear or direct replacement of handlers.
if (this.handlers[entry.index] !== entry.handler) {
return;
}
this.handlers[entry.index] = null;
// Do not reuse an index while forEach is walking its length snapshot.
if (!internals.iterationDepth) {
trimHandlers(this.handlers);
internals.handlersLength = this.handlers.length;
}
}
}
/**
* Clear all interceptors from the stack
*
* @returns {void}
*/
clear() {
if (this.handlers) {
this.handlers = [];
syncHandlerEntries(this, this[$internals]);
}
}
/**
* Iterate over all the registered interceptors
*
* This method is particularly useful for skipping over any
* interceptors that may have become `null` calling `eject`.
*
* @param {Function} fn The function to call for each interceptor
*
* @returns {void}
*/
forEach(fn) {
const internals = this[$internals];
syncHandlerEntries(this, internals);
internals.iterationDepth++;
try {
utils.forEach(this.handlers, function forEachHandler(h) {
if (h !== null) {
fn(h);
}
});
} finally {
if (!--internals.iterationDepth) {
syncHandlerEntries(this, internals);
trimHandlers(this.handlers);
internals.handlersLength = countHandlers(this.handlers);
}
}
}
}
export default InterceptorManager;
+8
View File
@@ -0,0 +1,8 @@
# axios // core
The modules found in `core/` should be modules that are specific to the domain logic of axios. These modules would most likely not make sense to be consumed outside of the axios module, as their logic is too specific. Some examples of core modules are:
- Dispatching requests
- Requests sent via `adapters/` (see lib/adapters/README.md)
- Managing interceptors
- Handling config
+49
View File
@@ -0,0 +1,49 @@
'use strict';
import utils from '../utils.js';
import AxiosError from './AxiosError.js';
export default function assertParamsAcyclic(params) {
const pending = [{ value: params }];
const active = new WeakSet();
const visited = new WeakSet();
while (pending.length) {
const { value, done } = pending.pop();
if (!utils.isPlainObject(value) && !utils.isArray(value)) continue;
if (done) {
active.delete(value);
continue;
}
if (active.has(value)) {
throw new AxiosError(
'Circular reference detected in params',
AxiosError.ERR_BAD_OPTION_VALUE
);
}
if (visited.has(value)) continue;
visited.add(value);
active.add(value);
pending.push({ value, done: true });
const keys = Object.keys(value);
if (Object.getOwnPropertySymbols && Object.getOwnPropertyDescriptor) {
Object.getOwnPropertySymbols(value).forEach((key) => {
const descriptor = Object.getOwnPropertyDescriptor(value, key);
if (descriptor && descriptor.enumerable) keys.push(key);
});
}
// Shallow array elements retain fields filtered during plain-object merges.
// Inspect reserved data properties without evaluating their accessors.
keys.forEach((key) => {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
const descriptor = Object.getOwnPropertyDescriptor(value, key);
if (descriptor && utils.hasOwnProp(descriptor, 'value')) {
pending.push({ value: descriptor.value });
}
} else {
pending.push({ value: value[key] });
}
});
}
}
+76
View File
@@ -0,0 +1,76 @@
'use strict';
import AxiosError, { REDACTED } from './AxiosError.js';
import isAbsoluteURL from '../helpers/isAbsoluteURL.js';
import combineURLs from '../helpers/combineURLs.js';
import normalizeURLForProtocolCheck from '../helpers/normalizeURLForProtocolCheck.js';
const malformedHttpProtocol = /^https?:(?!\/\/)/i;
// Redact the parts of a URL that can carry secrets before it is embedded in an
// error message. AxiosError.toJSON() serializes `message` verbatim and errors
// are commonly logged, while the opt-in `config.redact` model only cleans
// config keys — it cannot reach the message. Redact only the genuinely
// sensitive substrings — userinfo (credentials), query parameter values and
// fragment contents — with the same REDACTED marker the config redaction uses,
// while keeping the scheme, host, path and parameter names so the offending
// request stays accurately identifiable.
function redactFragment(fragment) {
if (!fragment) {
return fragment;
}
return fragment.replace(/(^|&)([^=&]*=)?[^&]+/g, (match, separator, parameterName = '') => {
return `${separator}${parameterName}${REDACTED}`;
});
}
function redactSensitiveURLParts(url) {
const redactedURL = url.replace(/^(https?:\/{0,2})[^/?#]*@/i, `$1${REDACTED}@`);
const fragmentIndex = redactedURL.indexOf('#');
const urlWithoutFragment =
fragmentIndex === -1 ? redactedURL : redactedURL.slice(0, fragmentIndex);
const redactedURLWithoutFragment = urlWithoutFragment.replace(
/([?&][^=&#]*=)[^&#]*/g,
`$1${REDACTED}`
);
if (fragmentIndex === -1) {
return redactedURLWithoutFragment;
}
return `${redactedURLWithoutFragment}#${redactFragment(redactedURL.slice(fragmentIndex + 1))}`;
}
function assertValidHttpProtocolURL(url, config) {
if (typeof url === 'string') {
const normalizedURL = normalizeURLForProtocolCheck(url);
if (malformedHttpProtocol.test(normalizedURL)) {
throw new AxiosError(
`Invalid URL ${JSON.stringify(redactSensitiveURLParts(normalizedURL))}: missing "//" after protocol`,
AxiosError.ERR_INVALID_URL,
config
);
}
}
}
/**
* Creates a new URL by combining the baseURL with the requestedURL,
* only when the requestedURL is not already an absolute URL.
* If the requestURL is absolute, this function returns the requestedURL untouched.
*
* @param {string} baseURL The base URL
* @param {string} requestedURL Absolute or relative URL to combine
*
* @returns {string} The combined full path
*/
export default function buildFullPath(baseURL, requestedURL, allowAbsoluteUrls, config) {
assertValidHttpProtocolURL(requestedURL, config);
let isRelativeUrl = !isAbsoluteURL(requestedURL);
if (baseURL && (isRelativeUrl || allowAbsoluteUrls === false)) {
assertValidHttpProtocolURL(baseURL, config);
return combineURLs(baseURL, requestedURL);
}
return requestedURL;
}
+37
View File
@@ -0,0 +1,37 @@
import utils from '../utils.js';
import platform from '../platform/index.js';
import AxiosError from './AxiosError.js';
import formDataToBlob, { FormDataError } from '../helpers/formDataToBlob.js';
export default function createMultipartBody(form, Blob, limit, limitError, boundary) {
// Report missing Blob support before trying to select a random source.
if (!utils.isFunction(Blob)) {
throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT);
}
if (boundary === undefined) {
if (utils.isFunction(platform.generateString)) {
boundary = 'axios-' + platform.generateString(36, '0123456789abcdef');
} else {
const crypto = utils.global.crypto;
if (!crypto || !utils.isFunction(crypto.getRandomValues)) {
throw new AxiosError('Random byte generation is not supported', AxiosError.ERR_NOT_SUPPORT);
}
const random = new Uint8Array(18);
crypto.getRandomValues(random);
boundary =
'axios-' + Array.from(random, (value) => ('0' + value.toString(16)).slice(-2)).join('');
}
}
try {
return formDataToBlob(form, Blob, limit, limitError, boundary);
} catch (error) {
if (error instanceof FormDataError) {
const code =
error.reason === 'boundary' ? AxiosError.ERR_BAD_OPTION_VALUE : AxiosError.ERR_BAD_REQUEST;
throw new AxiosError(error.message, code);
}
throw error;
}
}
+132
View File
@@ -0,0 +1,132 @@
'use strict';
import transformData from './transformData.js';
import isCancel from '../cancel/isCancel.js';
import defaults from '../defaults/index.js';
import CanceledError from '../cancel/CanceledError.js';
import AxiosHeaders from '../core/AxiosHeaders.js';
import adapters from '../adapters/adapters.js';
import utils from '../utils.js';
/**
* Throws a `CanceledError` if cancellation has been requested.
*
* @param {Object} config The config that is to be used for the request
*
* @returns {void}
*/
function throwIfCancellationRequested(config) {
if (config.cancelToken) {
config.cancelToken.throwIfRequested();
}
if (config.signal && config.signal.aborted) {
throw new CanceledError(null, config);
}
}
/**
* Resolve the clock used to time adapter execution.
*
* Prefers the monotonic `performance.now()` so wall-clock adjustments do not
* skew the measurement, and so frameworks that forbid reading the current
* time during prerendering (e.g. Next.js Cache Components) accept it. Falls
* back to `Date.now()` in runtimes without the High Resolution Time API.
*
* @returns {() => number} Millisecond clock
*/
function resolveClock() {
return typeof performance !== 'undefined' && utils.isFunction(performance.now)
? () => performance.now()
: Date.now;
}
/**
* Whole milliseconds elapsed since `start`, floored at zero.
*
* Rounded so the value stays an integer as with `Date.now()`. The fallback
* wall clock can move backwards mid-request, so the result is clamped.
*
* @param {() => number} now Clock returned by `resolveClock()`
* @param {number} start Timestamp read from `now`
*
* @returns {number} Non-negative elapsed milliseconds
*/
function elapsedSince(now, start) {
return Math.max(0, Math.round(now() - start));
}
/**
* Dispatch a request to the server using the configured adapter.
*
* @param {object} config The config that is to be used for the request
*
* @returns {Promise} The Promise to be fulfilled
*/
export default function dispatchRequest(_config) {
// Interceptors may replace the merged config with an ordinary object. Flatten
// it at the dispatch boundary so shared prototype members cannot become
// request behavior, while preserving intentional template/class members.
const config = utils.toSafeFlatObject(_config);
throwIfCancellationRequested(config);
config.headers = AxiosHeaders.from(utils.getSafeProp(config, 'headers'));
// Transform request data
config.data = transformData.call(config, config.transformRequest);
if (['post', 'put', 'patch'].indexOf(config.method) !== -1) {
config.headers.setContentType('application/x-www-form-urlencoded', false);
}
const adapter = adapters.getAdapter(config.adapter || defaults.adapter, config);
const now = resolveClock();
const start = now();
return adapter(config).then(
function onAdapterResolution(response) {
throwIfCancellationRequested(config);
response.duration = elapsedSince(now, start);
// Expose the current response on config so that transformResponse can
// attach it to any AxiosError it throws (e.g. on JSON parse failure).
// We clean it up afterwards to avoid polluting the config object.
config.response = response;
try {
response.data = transformData.call(config, config.transformResponse, response);
} finally {
delete config.response;
}
response.headers = AxiosHeaders.from(response.headers);
return response;
},
function onAdapterRejection(reason) {
if (!isCancel(reason)) {
throwIfCancellationRequested(config);
// Transform response data
if (reason && reason.response) {
reason.response.duration = elapsedSince(now, start);
config.response = reason.response;
try {
reason.response.data = transformData.call(
config,
config.transformResponse,
reason.response
);
} finally {
delete config.response;
}
reason.response.headers = AxiosHeaders.from(reason.response.headers);
}
}
return Promise.reject(reason);
}
);
}
+237
View File
@@ -0,0 +1,237 @@
'use strict';
import utils from '../utils.js';
import AxiosHeaders from './AxiosHeaders.js';
import assertParamsAcyclic from './assertParamsAcyclic.js';
const headersToObject = (thing) => (thing instanceof AxiosHeaders ? { ...thing } : thing);
const ownEnumerableKeys = (thing) => {
if (Object.getOwnPropertySymbols && Object.getOwnPropertyDescriptor) {
return Object.keys(thing).concat(
Object.getOwnPropertySymbols(thing).filter((symbol) => {
const descriptor = Object.getOwnPropertyDescriptor(thing, symbol);
return descriptor && descriptor.enumerable;
})
);
}
return Object.keys(thing);
};
// Copy params without recursive merges so the serializer remains responsible
// for its depth policy. Only visit defaults that contribute to the result.
function mergeParams(a, b) {
const pending = [];
const copies = new WeakMap();
const empty = {};
function copy(target, source) {
if (!utils.isPlainObject(source)) {
return utils.isArray(source) ? source.slice() : source;
}
const base = utils.isPlainObject(target) ? target : empty;
let bases = copies.get(source);
if (!bases) {
bases = new WeakMap();
copies.set(source, bases);
}
if (bases.has(base)) return bases.get(base);
const result = {};
bases.set(base, result);
pending.push({ base, source, result });
return result;
}
const result = utils.isUndefined(b) ? copy(undefined, a) : copy(a, b);
while (pending.length) {
const { base, source, result: target } = pending.pop();
const baseKeys = new Set(ownEnumerableKeys(base));
const sourceKeys = new Set(ownEnumerableKeys(source));
const keys = new Set([...baseKeys, ...sourceKeys]);
keys.forEach((key) => {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') return;
const hasSource = sourceKeys.has(key);
const value = hasSource ? source[key] : undefined;
const previous =
(!hasSource || utils.isPlainObject(value)) && baseKeys.has(key) ? base[key] : undefined;
target[key] = hasSource ? copy(previous, value) : copy(undefined, previous);
});
}
assertParamsAcyclic(result);
return result;
}
function getMergedValue(target, source, prop, caseless) {
if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
return utils.merge.call({ caseless }, target, source);
} else if (utils.isPlainObject(source)) {
return utils.merge({}, source);
} else if (utils.isArray(source)) {
return source.slice();
}
return source;
}
function mergeDeepProperties(a, b, prop, caseless) {
if (!utils.isUndefined(b)) {
return getMergedValue(a, b, prop, caseless);
} else if (!utils.isUndefined(a)) {
return getMergedValue(undefined, a, prop, caseless);
}
}
// eslint-disable-next-line consistent-return
function valueFromConfig2(a, b) {
if (!utils.isUndefined(b)) {
return getMergedValue(undefined, b);
}
}
// eslint-disable-next-line consistent-return
function defaultToConfig2(a, b) {
if (!utils.isUndefined(b)) {
return getMergedValue(undefined, b);
} else if (!utils.isUndefined(a)) {
return getMergedValue(undefined, a);
}
}
function getMergedTransitionalOption(config1, config2, prop) {
const transitional2 = utils.hasOwnProp(config2, 'transitional')
? config2.transitional
: undefined;
if (!utils.isUndefined(transitional2)) {
if (utils.isPlainObject(transitional2)) {
if (utils.hasOwnProp(transitional2, prop)) {
return transitional2[prop];
}
} else {
return undefined;
}
}
const transitional1 = utils.hasOwnProp(config1, 'transitional')
? config1.transitional
: undefined;
if (utils.isPlainObject(transitional1) && utils.hasOwnProp(transitional1, prop)) {
return transitional1[prop];
}
return undefined;
}
// eslint-disable-next-line consistent-return
function mergeDirectKeys(a, b, prop, config1, config2) {
if (utils.hasOwnProp(config2, prop)) {
return getMergedValue(a, b);
} else if (utils.hasOwnProp(config1, prop)) {
return getMergedValue(undefined, a);
}
}
function mergeHeaders(a, b, prop) {
return mergeDeepProperties(headersToObject(a), headersToObject(b), prop, true);
}
// Null-proto, frozen descriptor so a polluted Object.prototype.get cannot turn
// this data descriptor into an accessor descriptor on the way in.
// defineProperty only reads it, so one shared instance serves every call.
const hasOwnPropertyDescriptor = Object.freeze({
__proto__: null,
value: Object.prototype.hasOwnProperty,
enumerable: false,
writable: true,
configurable: true,
});
// The strategy table and the strategies above are stateless, so they are
// built once at module load instead of on every call: mergeConfig runs at
// most twice per request (once from a method alias, once from `_request`).
const mergeMap = {
url: valueFromConfig2,
method: valueFromConfig2,
data: valueFromConfig2,
baseURL: defaultToConfig2,
transformRequest: defaultToConfig2,
transformResponse: defaultToConfig2,
params: mergeParams,
paramsSerializer: defaultToConfig2,
timeout: defaultToConfig2,
timeoutErrorMessage: defaultToConfig2,
withCredentials: defaultToConfig2,
withXSRFToken: defaultToConfig2,
adapter: defaultToConfig2,
responseType: defaultToConfig2,
xsrfCookieName: defaultToConfig2,
xsrfHeaderName: defaultToConfig2,
onUploadProgress: defaultToConfig2,
onDownloadProgress: defaultToConfig2,
decompress: defaultToConfig2,
maxContentLength: defaultToConfig2,
maxBodyLength: defaultToConfig2,
beforeRedirect: defaultToConfig2,
transport: defaultToConfig2,
httpAgent: defaultToConfig2,
httpsAgent: defaultToConfig2,
cancelToken: defaultToConfig2,
socketPath: defaultToConfig2,
allowedSocketPaths: defaultToConfig2,
responseEncoding: defaultToConfig2,
validateStatus: mergeDirectKeys,
headers: mergeHeaders,
};
/**
* Config-specific merge-function which creates a new config-object
* by merging two configuration objects together.
*
* @param {Object} config1
* @param {Object} config2
*
* @returns {Object} New object resulting from merging config2 to config1
*/
export default function mergeConfig(config1, config2) {
// eslint-disable-next-line no-param-reassign
config1 = config1 || {};
config2 = config2 || {};
// Use a null-prototype object so that downstream reads such as `config.auth`
// or `config.baseURL` cannot inherit polluted values from Object.prototype.
// `hasOwnProperty` is restored as a non-enumerable own slot to preserve
// ergonomics for user code that relies on it.
const config = Object.create(null);
Object.defineProperty(config, 'hasOwnProperty', hasOwnPropertyDescriptor);
const keys = ownEnumerableKeys({ ...config1, ...config2 });
for (let i = 0, len = keys.length; i < len; i++) {
const prop = keys[i];
if (prop === '__proto__' || prop === 'constructor' || prop === 'prototype') continue;
const merge = utils.hasOwnProp(mergeMap, prop) ? mergeMap[prop] : mergeDeepProperties;
const a = utils.hasOwnProp(config1, prop) ? config1[prop] : undefined;
const b = utils.hasOwnProp(config2, prop) ? config2[prop] : undefined;
const configValue =
merge === mergeDirectKeys ? merge(a, b, prop, config1, config2) : merge(a, b, prop);
(utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
}
if (
utils.hasOwnProp(config2, 'validateStatus') &&
utils.isUndefined(config2.validateStatus) &&
getMergedTransitionalOption(config1, config2, 'validateStatusUndefinedResolves') === false
) {
if (utils.hasOwnProp(config1, 'validateStatus')) {
config.validateStatus = getMergedValue(undefined, config1.validateStatus);
} else {
delete config.validateStatus;
}
}
return config;
}
+17
View File
@@ -0,0 +1,17 @@
'use strict';
const methodList = Object.freeze([
'get',
'delete',
'head',
'options',
'post',
'put',
'patch',
'purge',
'link',
'unlink',
'query',
]);
export default methodList;
+27
View File
@@ -0,0 +1,27 @@
'use strict';
const FORM_DATA_CONTENT_HEADERS = ['content-type', 'content-length'];
/**
* Apply the headers generated by a FormData implementation to the request headers,
* honoring the `formDataHeaderPolicy` option: with 'content-only', copy only the
* content-* headers; otherwise merge all of them.
*
* @param {AxiosHeaders} headers - the request headers to mutate
* @param {Object | null | undefined} formHeaders - headers produced by the FormData implementation
* @param {String} [policy] - the resolved `formDataHeaderPolicy` config value
*
* @returns {void}
*/
export default function setFormDataHeaders(headers, formHeaders, policy) {
if (policy !== 'content-only') {
headers.set(formHeaders);
return;
}
Object.entries(formHeaders || {}).forEach(([key, val]) => {
if (FORM_DATA_CONTENT_HEADERS.includes(key.toLowerCase())) {
headers.set(key, val);
}
});
}
+27
View File
@@ -0,0 +1,27 @@
'use strict';
import AxiosError from './AxiosError.js';
/**
* Resolve or reject a Promise based on response status.
*
* @param {Function} resolve A function that resolves the promise.
* @param {Function} reject A function that rejects the promise.
* @param {object} response The response.
*
* @returns {object} The response.
*/
export default function settle(resolve, reject, response) {
const validateStatus = response.config.validateStatus;
if (!response.status || !validateStatus || validateStatus(response.status)) {
resolve(response);
} else {
reject(new AxiosError(
'Request failed with status code ' + response.status,
response.status >= 400 && response.status < 500 ? AxiosError.ERR_BAD_REQUEST : AxiosError.ERR_BAD_RESPONSE,
response.config,
response.request,
response
));
}
}
+28
View File
@@ -0,0 +1,28 @@
'use strict';
import utils from '../utils.js';
import defaults from '../defaults/index.js';
import AxiosHeaders from '../core/AxiosHeaders.js';
/**
* Transform the data for a request or a response
*
* @param {Array|Function} fns A single function or Array of functions
* @param {?Object} response The response object
*
* @returns {*} The resulting transformed data
*/
export default function transformData(fns, response) {
const config = this || defaults;
const context = response || config;
const headers = AxiosHeaders.from(context.headers);
let data = context.data;
utils.forEach(fns, function transform(fn) {
data = fn.call(config, data, headers.normalize(), response ? response.status : undefined);
});
headers.normalize();
return data;
}