| import platform from '../platform/index.js'; |
| import utils from '../utils.js'; |
| import isURLSameOrigin from './isURLSameOrigin.js'; |
| import cookies from './cookies.js'; |
| import buildFullPath from '../core/buildFullPath.js'; |
| import mergeConfig from '../core/mergeConfig.js'; |
| import AxiosHeaders from '../core/AxiosHeaders.js'; |
| import buildURL from './buildURL.js'; |
|
|
| export default (config) => { |
| const newConfig = mergeConfig({}, config); |
|
|
| |
| |
| const own = (key) => (utils.hasOwnProp(newConfig, key) ? newConfig[key] : undefined); |
|
|
| const data = own('data'); |
| let withXSRFToken = own('withXSRFToken'); |
| const xsrfHeaderName = own('xsrfHeaderName'); |
| const xsrfCookieName = own('xsrfCookieName'); |
| let headers = own('headers'); |
| const auth = own('auth'); |
| const baseURL = own('baseURL'); |
| const allowAbsoluteUrls = own('allowAbsoluteUrls'); |
| const url = own('url'); |
|
|
| newConfig.headers = headers = AxiosHeaders.from(headers); |
|
|
| newConfig.url = buildURL( |
| buildFullPath(baseURL, url, allowAbsoluteUrls), |
| config.params, |
| config.paramsSerializer |
| ); |
|
|
| |
| if (auth) { |
| headers.set( |
| 'Authorization', |
| 'Basic ' + |
| btoa( |
| (auth.username || '') + |
| ':' + |
| (auth.password ? unescape(encodeURIComponent(auth.password)) : '') |
| ) |
| ); |
| } |
|
|
| if (utils.isFormData(data)) { |
| if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) { |
| headers.setContentType(undefined); |
| } else if (utils.isFunction(data.getHeaders)) { |
| |
| const formHeaders = data.getHeaders(); |
| |
| const allowedHeaders = ['content-type', 'content-length']; |
| Object.entries(formHeaders).forEach(([key, val]) => { |
| if (allowedHeaders.includes(key.toLowerCase())) { |
| headers.set(key, val); |
| } |
| }); |
| } |
| } |
|
|
| |
| |
| |
|
|
| if (platform.hasStandardBrowserEnv) { |
| if (utils.isFunction(withXSRFToken)) { |
| withXSRFToken = withXSRFToken(newConfig); |
| } |
|
|
| |
| |
| |
| const shouldSendXSRF = |
| withXSRFToken === true || |
| (withXSRFToken == null && isURLSameOrigin(newConfig.url)); |
|
|
| if (shouldSendXSRF) { |
| const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName); |
|
|
| if (xsrfValue) { |
| headers.set(xsrfHeaderName, xsrfValue); |
| } |
| } |
| } |
|
|
| return newConfig; |
| }; |
|
|