opusdev/vector-similarity-api
1
1'use strict';2 3import utils from '../utils.js';4import AxiosError from '../core/AxiosError.js';5// temporary hotfix to avoid circular references until AxiosURLSearchParams is refactored6import PlatformFormData from '../platform/node/classes/FormData.js';7 8/**9 * Determines if the given thing is a array or js object.10 *11 * @param {string} thing - The object or array to be visited.12 *13 * @returns {boolean}14 */15function isVisitable(thing) {16 return utils.isPlainObject(thing) || utils.isArray(thing);17}18 19/**20 * It removes the brackets from the end of a string21 *22 * @param {string} key - The key of the parameter.23 *24 * @returns {string} the key without the brackets.25 */26function removeBrackets(key) {27 return utils.endsWith(key, '[]') ? key.slice(0, -2) : key;28}29 30/**31 * It takes a path, a key, and a boolean, and returns a string32 *33 * @param {string} path - The path to the current key.34 * @param {string} key - The key of the current object being iterated over.35 * @param {string} dots - If true, the key will be rendered with dots instead of brackets.36 *37 * @returns {string} The path to the current key.38 */39function renderKey(path, key, dots) {40 if (!path) return key;41 return path.concat(key).map(function each(token, i) {42 // eslint-disable-next-line no-param-reassign43 token = removeBrackets(token);44 return !dots && i ? '[' + token + ']' : token;45 }).join(dots ? '.' : '');46}47 48/**49 * If the array is an array and none of its elements are visitable, then it's a flat array.50 *51 * @param {Array<any>} arr - The array to check52 *53 * @returns {boolean}54 */55function isFlatArray(arr) {56 return utils.isArray(arr) && !arr.some(isVisitable);57}58 59const predicates = utils.toFlatObject(utils, {}, null, function filter(prop) {60 return /^is[A-Z]/.test(prop);61});62 63/**64 * Convert a data object to FormData65 *66 * @param {Object} obj67 * @param {?Object} [formData]68 * @param {?Object} [options]69 * @param {Function} [options.visitor]70 * @param {Boolean} [options.metaTokens = true]71 * @param {Boolean} [options.dots = false]72 * @param {?Boolean} [options.indexes = false]73 *74 * @returns {Object}75 **/76 77/**78 * It converts an object into a FormData object79 *80 * @param {Object<any, any>} obj - The object to convert to form data.81 * @param {string} formData - The FormData object to append to.82 * @param {Object<string, any>} options83 *84 * @returns85 */86function toFormData(obj, formData, options) {87 if (!utils.isObject(obj)) {88 throw new TypeError('target must be an object');89 }90 91 // eslint-disable-next-line no-param-reassign92 formData = formData || new (PlatformFormData || FormData)();93 94 // eslint-disable-next-line no-param-reassign95 options = utils.toFlatObject(options, {96 metaTokens: true,97 dots: false,98 indexes: false99 }, false, function defined(option, source) {100 // eslint-disable-next-line no-eq-null,eqeqeq101 return !utils.isUndefined(source[option]);102 });103 104 const metaTokens = options.metaTokens;105 // eslint-disable-next-line no-use-before-define106 const visitor = options.visitor || defaultVisitor;107 const dots = options.dots;108 const indexes = options.indexes;109 const _Blob = options.Blob || typeof Blob !== 'undefined' && Blob;110 const useBlob = _Blob && utils.isSpecCompliantForm(formData);111 112 if (!utils.isFunction(visitor)) {113 throw new TypeError('visitor must be a function');114 }115 116 function convertValue(value) {117 if (value === null) return '';118 119 if (utils.isDate(value)) {120 return value.toISOString();121 }122 123 if (utils.isBoolean(value)) {124 return value.toString();125 }126 127 if (!useBlob && utils.isBlob(value)) {128 throw new AxiosError('Blob is not supported. Use a Buffer instead.');129 }130 131 if (utils.isArrayBuffer(value) || utils.isTypedArray(value)) {132 return useBlob && typeof Blob === 'function' ? new Blob([value]) : Buffer.from(value);133 }134 135 return value;136 }137 138 /**139 * Default visitor.140 *141 * @param {*} value142 * @param {String|Number} key143 * @param {Array<String|Number>} path144 * @this {FormData}145 *146 * @returns {boolean} return true to visit the each prop of the value recursively147 */148 function defaultVisitor(value, key, path) {149 let arr = value;150 151 if (value && !path && typeof value === 'object') {152 if (utils.endsWith(key, '{}')) {153 // eslint-disable-next-line no-param-reassign154 key = metaTokens ? key : key.slice(0, -2);155 // eslint-disable-next-line no-param-reassign156 value = JSON.stringify(value);157 } else if (158 (utils.isArray(value) && isFlatArray(value)) ||159 ((utils.isFileList(value) || utils.endsWith(key, '[]')) && (arr = utils.toArray(value))160 )) {161 // eslint-disable-next-line no-param-reassign162 key = removeBrackets(key);163 164 arr.forEach(function each(el, index) {165 !(utils.isUndefined(el) || el === null) && formData.append(166 // eslint-disable-next-line no-nested-ternary167 indexes === true ? renderKey([key], index, dots) : (indexes === null ? key : key + '[]'),168 convertValue(el)169 );170 });171 return false;172 }173 }174 175 if (isVisitable(value)) {176 return true;177 }178 179 formData.append(renderKey(path, key, dots), convertValue(value));180 181 return false;182 }183 184 const stack = [];185 186 const exposedHelpers = Object.assign(predicates, {187 defaultVisitor,188 convertValue,189 isVisitable190 });191 192 function build(value, path) {193 if (utils.isUndefined(value)) return;194 195 if (stack.indexOf(value) !== -1) {196 throw Error('Circular reference detected in ' + path.join('.'));197 }198 199 stack.push(value);200 201 utils.forEach(value, function each(el, key) {202 const result = !(utils.isUndefined(el) || el === null) && visitor.call(203 formData, el, utils.isString(key) ? key.trim() : key, path, exposedHelpers204 );205 206 if (result === true) {207 build(el, path ? path.concat(key) : [key]);208 }209 });210 211 stack.pop();212 }213 214 if (!utils.isObject(obj)) {215 throw new TypeError('data must be an object');216 }217 218 build(obj);219 220 return formData;221}222 223export default toFormData;224 