MCPcopy
hub / github.com/axios/axios / toFormData

Function toFormData

lib/helpers/toFormData.js:93–283  ·  view source on GitHub ↗

* It converts an object into a FormData object * * @param {Object<any, any>} obj - The object to convert to form data. * @param {string} formData - The FormData object to append to. * @param {Object<string, any>} options * * @returns

(obj, formData, options)

Source from the content-addressed store, hash-verified

91 * @returns
92 */
93function toFormData(obj, formData, options) {
94 if (!utils.isObject(obj)) {
95 throw new TypeError('target must be an object');
96 }
97
98 // eslint-disable-next-line no-param-reassign
99 formData = formData || new (PlatformFormData || FormData)();
100
101 // eslint-disable-next-line no-param-reassign
102 options = utils.toFlatObject(
103 options,
104 {
105 metaTokens: true,
106 dots: false,
107 indexes: false,
108 },
109 false,
110 function defined(option, source) {
111 // eslint-disable-next-line no-eq-null,eqeqeq
112 return !utils.isUndefined(source[option]);
113 }
114 );
115
116 const metaTokens = options.metaTokens;
117 // eslint-disable-next-line no-use-before-define
118 const visitor = options.visitor || defaultVisitor;
119 const dots = options.dots;
120 const indexes = options.indexes;
121 const _Blob = options.Blob || (typeof Blob !== 'undefined' && Blob);
122 const maxDepth = options.maxDepth === undefined ? DEFAULT_FORM_DATA_MAX_DEPTH : options.maxDepth;
123 const useBlob = _Blob && utils.isSpecCompliantForm(formData);
124 const stack = [];
125
126 if (!utils.isFunction(visitor)) {
127 throw new TypeError('visitor must be a function');
128 }
129
130 function convertValue(value) {
131 if (value === null) return '';
132
133 if (utils.isDate(value)) {
134 return value.toISOString();
135 }
136
137 if (utils.isBoolean(value)) {
138 return value.toString();
139 }
140
141 if (!useBlob && utils.isBlob(value)) {
142 throw new AxiosError('Blob is not supported. Use a Buffer instead.');
143 }
144
145 if (utils.isArrayBuffer(value) || utils.isTypedArray(value)) {
146 if (useBlob && typeof _Blob === 'function') {
147 return new _Blob([value]);
148 }
149 if (typeof Buffer !== 'undefined') {
150 return Buffer.from(value);

Callers 7

index.jsFile · 0.85
AxiosURLSearchParamsFunction · 0.85
toURLEncodedFormFunction · 0.85
esm-added-types.tsFile · 0.85
esm-index.tsFile · 0.85
toFormData.test.jsFile · 0.85

Calls 1

buildFunction · 0.70

Tested by

no test coverage detected