CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
resolveConfig.js62 linesDownload Raw Back to helpers
1import platform from "../platform/index.js";2import utils from "../utils.js";3import isURLSameOrigin from "./isURLSameOrigin.js";4import cookies from "./cookies.js";5import buildFullPath from "../core/buildFullPath.js";6import mergeConfig from "../core/mergeConfig.js";7import AxiosHeaders from "../core/AxiosHeaders.js";8import buildURL from "./buildURL.js";9 10export 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, newConfig.allowAbsoluteUrls), config.params, config.paramsSerializer);18 19  // HTTP basic authentication20  if (auth) {21    headers.set('Authorization', 'Basic ' +22      btoa((auth.username || '') + ':' + (auth.password ? unescape(encodeURIComponent(auth.password)) : ''))23    );24  }25 26  if (utils.isFormData(data)) {27    if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {28      headers.setContentType(undefined); // browser handles it29    } else if (utils.isFunction(data.getHeaders)) {30      // Node.js FormData (like form-data package)31      const formHeaders = data.getHeaders();32      // Only set safe headers to avoid overwriting security headers33      const allowedHeaders = ['content-type', 'content-length'];34      Object.entries(formHeaders).forEach(([key, val]) => {35        if (allowedHeaders.includes(key.toLowerCase())) {36          headers.set(key, val);37        }38      });39    }40  }  41 42  // Add xsrf header43  // This is only done if running in a standard browser environment.44  // Specifically not if we're in a web worker, or react-native.45 46  if (platform.hasStandardBrowserEnv) {47    withXSRFToken && utils.isFunction(withXSRFToken) && (withXSRFToken = withXSRFToken(newConfig));48 49    if (withXSRFToken || (withXSRFToken !== false && isURLSameOrigin(newConfig.url))) {50      // Add xsrf header51      const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName);52 53      if (xsrfValue) {54        headers.set(xsrfHeaderName, xsrfValue);55      }56    }57  }58 59  return newConfig;60}61 62