[d565449] | 1 | import platform from "../platform/index.js";
|
---|
| 2 | import utils from "../utils.js";
|
---|
| 3 | import isURLSameOrigin from "./isURLSameOrigin.js";
|
---|
| 4 | import cookies from "./cookies.js";
|
---|
| 5 | import buildFullPath from "../core/buildFullPath.js";
|
---|
| 6 | import mergeConfig from "../core/mergeConfig.js";
|
---|
| 7 | import AxiosHeaders from "../core/AxiosHeaders.js";
|
---|
| 8 | import buildURL from "./buildURL.js";
|
---|
| 9 |
|
---|
| 10 | export default (config) => {
|
---|
| 11 | const newConfig = mergeConfig({}, config);
|
---|
| 12 |
|
---|
| 13 | let {data, withXSRFToken, xsrfHeaderName, xsrfCookieName, headers, auth} = newConfig;
|
---|
| 14 |
|
---|
| 15 | newConfig.headers = headers = AxiosHeaders.from(headers);
|
---|
| 16 |
|
---|
| 17 | newConfig.url = buildURL(buildFullPath(newConfig.baseURL, newConfig.url), config.params, config.paramsSerializer);
|
---|
| 18 |
|
---|
| 19 | // HTTP basic authentication
|
---|
| 20 | if (auth) {
|
---|
| 21 | headers.set('Authorization', 'Basic ' +
|
---|
| 22 | btoa((auth.username || '') + ':' + (auth.password ? unescape(encodeURIComponent(auth.password)) : ''))
|
---|
| 23 | );
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | let contentType;
|
---|
| 27 |
|
---|
| 28 | if (utils.isFormData(data)) {
|
---|
| 29 | if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
|
---|
| 30 | headers.setContentType(undefined); // Let the browser set it
|
---|
| 31 | } else if ((contentType = headers.getContentType()) !== false) {
|
---|
| 32 | // fix semicolon duplication issue for ReactNative FormData implementation
|
---|
| 33 | const [type, ...tokens] = contentType ? contentType.split(';').map(token => token.trim()).filter(Boolean) : [];
|
---|
| 34 | headers.setContentType([type || 'multipart/form-data', ...tokens].join('; '));
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | // Add xsrf header
|
---|
| 39 | // This is only done if running in a standard browser environment.
|
---|
| 40 | // Specifically not if we're in a web worker, or react-native.
|
---|
| 41 |
|
---|
| 42 | if (platform.hasStandardBrowserEnv) {
|
---|
| 43 | withXSRFToken && utils.isFunction(withXSRFToken) && (withXSRFToken = withXSRFToken(newConfig));
|
---|
| 44 |
|
---|
| 45 | if (withXSRFToken || (withXSRFToken !== false && isURLSameOrigin(newConfig.url))) {
|
---|
| 46 | // Add xsrf header
|
---|
| 47 | const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName);
|
---|
| 48 |
|
---|
| 49 | if (xsrfValue) {
|
---|
| 50 | headers.set(xsrfHeaderName, xsrfValue);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | return newConfig;
|
---|
| 56 | }
|
---|
| 57 |
|
---|