resolveConfig.js 2.0 KB

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