mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Improved FormData support; (#4448)
* Fixed isFormData predicate;
Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment through `form-data` package;
* Added the `form-data` package as a dependency for the server build;
Added tests for FormData payload;
* Added FormData automatic serialization section;
Refactored cancellation section;
* Reworked toFormData helper;
Expose toFormData helper as a static method;
Refactored transform request;
Added kindOf, kindOfTest, endsWith, isTypedArray util;
Refactored utils.js to use kindOf for tests;
* Fixed isFormData predicate; (#4413)
Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment using `form-data` package;
(cherry picked from commit 73e3bdb883)
* Added shortcut methods `postForm`, `putForm`, `patchForm` to submit a Form;
Added ability to submit FileList object as a FormData;
Updated README.md;
* Updated README.md;
This commit is contained in:
+65
-47
@@ -1,55 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
function combinedKey(parentKey, elKey) {
|
||||
return parentKey + '.' + elKey;
|
||||
}
|
||||
|
||||
function buildFormData(formData, data, parentKey) {
|
||||
if (Array.isArray(data)) {
|
||||
data.forEach(function buildArray(el, i) {
|
||||
buildFormData(formData, el, combinedKey(parentKey, i));
|
||||
});
|
||||
} else if (
|
||||
typeof data === 'object' &&
|
||||
!(data instanceof File || data === null)
|
||||
) {
|
||||
Object.keys(data).forEach(function buildObject(key) {
|
||||
buildFormData(
|
||||
formData,
|
||||
data[key],
|
||||
parentKey ? combinedKey(parentKey, key) : key
|
||||
);
|
||||
});
|
||||
} else {
|
||||
if (data === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
var value =
|
||||
typeof data === 'boolean' || typeof data === 'number'
|
||||
? data.toString()
|
||||
: data;
|
||||
formData.append(parentKey, value);
|
||||
}
|
||||
}
|
||||
var utils = require('../utils');
|
||||
|
||||
/**
|
||||
* convert a data object to FormData
|
||||
*
|
||||
* type FormDataPrimitive = string | Blob | number | boolean
|
||||
* interface FormDataNest {
|
||||
* [x: string]: FormVal
|
||||
* }
|
||||
*
|
||||
* type FormVal = FormDataNest | FormDataPrimitive
|
||||
*
|
||||
* @param {FormVal} data
|
||||
*/
|
||||
* Convert a data object to FormData
|
||||
* @param {Object} obj
|
||||
* @param {?Object} [formData]
|
||||
* @returns {Object}
|
||||
**/
|
||||
|
||||
module.exports = function getFormData(data) {
|
||||
var formData = new FormData();
|
||||
function toFormData(obj, formData) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
formData = formData || new FormData();
|
||||
|
||||
buildFormData(formData, data);
|
||||
var stack = [];
|
||||
|
||||
function convertValue(value) {
|
||||
if (value === null) return '';
|
||||
|
||||
if (utils.isDate(value)) {
|
||||
return value.toISOString();
|
||||
}
|
||||
|
||||
if (utils.isArrayBuffer(value) || utils.isTypedArray(value)) {
|
||||
return typeof Blob === 'function' ? new Blob([value]) : Buffer.from(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function build(data, parentKey) {
|
||||
if (utils.isPlainObject(data) || utils.isArray(data)) {
|
||||
if (stack.indexOf(data) !== -1) {
|
||||
throw Error('Circular reference detected in ' + parentKey);
|
||||
}
|
||||
|
||||
stack.push(data);
|
||||
|
||||
utils.forEach(data, function each(value, key) {
|
||||
if (utils.isUndefined(value)) return;
|
||||
|
||||
var fullKey = parentKey ? parentKey + '.' + key : key;
|
||||
var arr;
|
||||
|
||||
if (value && !parentKey && typeof value === 'object') {
|
||||
if (utils.endsWith(key, '{}')) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
value = JSON.stringify(value);
|
||||
} else if (utils.endsWith(key, '[]') && (arr = utils.toArray(value))) {
|
||||
// eslint-disable-next-line func-names
|
||||
arr.forEach(function(el) {
|
||||
!utils.isUndefined(el) && formData.append(fullKey, convertValue(el));
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
build(value, fullKey);
|
||||
});
|
||||
|
||||
stack.pop();
|
||||
} else {
|
||||
formData.append(parentKey, convertValue(data));
|
||||
}
|
||||
}
|
||||
|
||||
build(obj);
|
||||
|
||||
return formData;
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = toFormData;
|
||||
|
||||
Reference in New Issue
Block a user