opusdev/vector-similarity-api
1
1"use strict";2 3import utils from "../utils.js";4import AxiosHeaders from "./AxiosHeaders.js";5 6const headersToObject = (thing) =>7 thing instanceof AxiosHeaders ? { ...thing } : thing;8 9/**10 * Config-specific merge-function which creates a new config-object11 * by merging two configuration objects together.12 *13 * @param {Object} config114 * @param {Object} config215 *16 * @returns {Object} New object resulting from merging config2 to config117 */18export default function mergeConfig(config1, config2) {19 // eslint-disable-next-line no-param-reassign20 config2 = config2 || {};21 const config = {};22 23 function getMergedValue(target, source, prop, caseless) {24 if (utils.isPlainObject(target) && utils.isPlainObject(source)) {25 return utils.merge.call({ caseless }, target, source);26 } else if (utils.isPlainObject(source)) {27 return utils.merge({}, source);28 } else if (utils.isArray(source)) {29 return source.slice();30 }31 return source;32 }33 34 function mergeDeepProperties(a, b, prop, caseless) {35 if (!utils.isUndefined(b)) {36 return getMergedValue(a, b, prop, caseless);37 } else if (!utils.isUndefined(a)) {38 return getMergedValue(undefined, a, prop, caseless);39 }40 }41 42 // eslint-disable-next-line consistent-return43 function valueFromConfig2(a, b) {44 if (!utils.isUndefined(b)) {45 return getMergedValue(undefined, b);46 }47 }48 49 // eslint-disable-next-line consistent-return50 function defaultToConfig2(a, b) {51 if (!utils.isUndefined(b)) {52 return getMergedValue(undefined, b);53 } else if (!utils.isUndefined(a)) {54 return getMergedValue(undefined, a);55 }56 }57 58 // eslint-disable-next-line consistent-return59 function mergeDirectKeys(a, b, prop) {60 if (prop in config2) {61 return getMergedValue(a, b);62 } else if (prop in config1) {63 return getMergedValue(undefined, a);64 }65 }66 67 const mergeMap = {68 url: valueFromConfig2,69 method: valueFromConfig2,70 data: valueFromConfig2,71 baseURL: defaultToConfig2,72 transformRequest: defaultToConfig2,73 transformResponse: defaultToConfig2,74 paramsSerializer: defaultToConfig2,75 timeout: defaultToConfig2,76 timeoutMessage: defaultToConfig2,77 withCredentials: defaultToConfig2,78 withXSRFToken: defaultToConfig2,79 adapter: defaultToConfig2,80 responseType: defaultToConfig2,81 xsrfCookieName: defaultToConfig2,82 xsrfHeaderName: defaultToConfig2,83 onUploadProgress: defaultToConfig2,84 onDownloadProgress: defaultToConfig2,85 decompress: defaultToConfig2,86 maxContentLength: defaultToConfig2,87 maxBodyLength: defaultToConfig2,88 beforeRedirect: defaultToConfig2,89 transport: defaultToConfig2,90 httpAgent: defaultToConfig2,91 httpsAgent: defaultToConfig2,92 cancelToken: defaultToConfig2,93 socketPath: defaultToConfig2,94 responseEncoding: defaultToConfig2,95 validateStatus: mergeDirectKeys,96 headers: (a, b, prop) =>97 mergeDeepProperties(headersToObject(a), headersToObject(b), prop, true),98 };99 100 utils.forEach(101 Object.keys({ ...config1, ...config2 }),102 function computeConfigValue(prop) {103 if (104 prop === "__proto__" ||105 prop === "constructor" ||106 prop === "prototype"107 )108 return;109 const merge = utils.hasOwnProp(mergeMap, prop)110 ? mergeMap[prop]111 : mergeDeepProperties;112 const configValue = merge(config1[prop], config2[prop], prop);113 (utils.isUndefined(configValue) && merge !== mergeDirectKeys) ||114 (config[prop] = configValue);115 },116 );117 118 return config;119}120 