| 'use strict'; |
|
|
| import utils from '../utils.js'; |
| import AxiosError from '../core/AxiosError.js'; |
| |
| import PlatformFormData from '../platform/node/classes/FormData.js'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| function isVisitable(thing) { |
| return utils.isPlainObject(thing) || utils.isArray(thing); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function removeBrackets(key) { |
| return utils.endsWith(key, '[]') ? key.slice(0, -2) : key; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function renderKey(path, key, dots) { |
| if (!path) return key; |
| return path.concat(key).map(function each(token, i) { |
| |
| token = removeBrackets(token); |
| return !dots && i ? '[' + token + ']' : token; |
| }).join(dots ? '.' : ''); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function isFlatArray(arr) { |
| return utils.isArray(arr) && !arr.some(isVisitable); |
| } |
|
|
| const predicates = utils.toFlatObject(utils, {}, null, function filter(prop) { |
| return /^is[A-Z]/.test(prop); |
| }); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function toFormData(obj, formData, options) { |
| if (!utils.isObject(obj)) { |
| throw new TypeError('target must be an object'); |
| } |
|
|
| |
| formData = formData || new (PlatformFormData || FormData)(); |
|
|
| |
| options = utils.toFlatObject(options, { |
| metaTokens: true, |
| dots: false, |
| indexes: false |
| }, false, function defined(option, source) { |
| |
| return !utils.isUndefined(source[option]); |
| }); |
|
|
| const metaTokens = options.metaTokens; |
| |
| const visitor = options.visitor || defaultVisitor; |
| const dots = options.dots; |
| const indexes = options.indexes; |
| const _Blob = options.Blob || typeof Blob !== 'undefined' && Blob; |
| const useBlob = _Blob && utils.isSpecCompliantForm(formData); |
|
|
| if (!utils.isFunction(visitor)) { |
| throw new TypeError('visitor must be a function'); |
| } |
|
|
| function convertValue(value) { |
| if (value === null) return ''; |
|
|
| if (utils.isDate(value)) { |
| return value.toISOString(); |
| } |
|
|
| if (!useBlob && utils.isBlob(value)) { |
| throw new AxiosError('Blob is not supported. Use a Buffer instead.'); |
| } |
|
|
| if (utils.isArrayBuffer(value) || utils.isTypedArray(value)) { |
| return useBlob && typeof Blob === 'function' ? new Blob([value]) : Buffer.from(value); |
| } |
|
|
| return value; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function defaultVisitor(value, key, path) { |
| let arr = value; |
|
|
| if (value && !path && typeof value === 'object') { |
| if (utils.endsWith(key, '{}')) { |
| |
| key = metaTokens ? key : key.slice(0, -2); |
| |
| value = JSON.stringify(value); |
| } else if ( |
| (utils.isArray(value) && isFlatArray(value)) || |
| ((utils.isFileList(value) || utils.endsWith(key, '[]')) && (arr = utils.toArray(value)) |
| )) { |
| |
| key = removeBrackets(key); |
|
|
| arr.forEach(function each(el, index) { |
| !(utils.isUndefined(el) || el === null) && formData.append( |
| |
| indexes === true ? renderKey([key], index, dots) : (indexes === null ? key : key + '[]'), |
| convertValue(el) |
| ); |
| }); |
| return false; |
| } |
| } |
|
|
| if (isVisitable(value)) { |
| return true; |
| } |
|
|
| formData.append(renderKey(path, key, dots), convertValue(value)); |
|
|
| return false; |
| } |
|
|
| const stack = []; |
|
|
| const exposedHelpers = Object.assign(predicates, { |
| defaultVisitor, |
| convertValue, |
| isVisitable |
| }); |
|
|
| function build(value, path) { |
| if (utils.isUndefined(value)) return; |
|
|
| if (stack.indexOf(value) !== -1) { |
| throw Error('Circular reference detected in ' + path.join('.')); |
| } |
|
|
| stack.push(value); |
|
|
| utils.forEach(value, function each(el, key) { |
| const result = !(utils.isUndefined(el) || el === null) && visitor.call( |
| formData, el, utils.isString(key) ? key.trim() : key, path, exposedHelpers |
| ); |
|
|
| if (result === true) { |
| build(el, path ? path.concat(key) : [key]); |
| } |
| }); |
|
|
| stack.pop(); |
| } |
|
|
| if (!utils.isObject(obj)) { |
| throw new TypeError('data must be an object'); |
| } |
|
|
| build(obj); |
|
|
| return formData; |
| } |
|
|
| export default toFormData; |
|
|