2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

chore(helpers/toFormData): added docs for to form data functions

This commit is contained in:
Jay
2022-06-02 20:46:05 +02:00
parent 34b71f0a1c
commit ba3e593807
+50
View File
@@ -4,14 +4,37 @@ var utils = require('../utils');
var AxiosError = require('../core/AxiosError'); var AxiosError = require('../core/AxiosError');
var envFormData = require('../env/classes/FormData'); var envFormData = require('../env/classes/FormData');
/**
* Determines if the given thing is a array or js object.
*
* @param {string} thing - The object or array to be visited.
*
* @returns {boolean}
*/
function isVisitable(thing) { function isVisitable(thing) {
return utils.isPlainObject(thing) || utils.isArray(thing); return utils.isPlainObject(thing) || utils.isArray(thing);
} }
/**
* It removes the brackets from the end of a string
*
* @param {string} key - The key of the parameter.
*
* @returns {string} the key without the brackets.
*/
function removeBrackets(key) { function removeBrackets(key) {
return utils.endsWith(key, '[]') ? key.slice(0, -2) : key; return utils.endsWith(key, '[]') ? key.slice(0, -2) : key;
} }
/**
* It takes a path, a key, and a boolean, and returns a string
*
* @param {string} path - The path to the current key.
* @param {string} key - The key of the current object being iterated over.
* @param {string} dots - If true, the key will be rendered with dots instead of brackets.
*
* @returns {string} The path to the current key.
*/
function renderKey(path, key, dots) { function renderKey(path, key, dots) {
if (!path) return key; if (!path) return key;
return path.concat(key).map(function each(token, i) { return path.concat(key).map(function each(token, i) {
@@ -21,6 +44,13 @@ function renderKey(path, key, dots) {
}).join(dots ? '.' : ''); }).join(dots ? '.' : '');
} }
/**
* If the array is an array and none of its elements are visitable, then it's a flat array.
*
* @param {Array<any>} arr - The array to check
*
* @returns {boolean}
*/
function isFlatArray(arr) { function isFlatArray(arr) {
return utils.isArray(arr) && !arr.some(isVisitable); return utils.isArray(arr) && !arr.some(isVisitable);
} }
@@ -29,12 +59,20 @@ var predicates = utils.toFlatObject(utils, {}, null, function filter(prop) {
return /^is[A-Z]/.test(prop); return /^is[A-Z]/.test(prop);
}); });
/**
* If the thing is a FormData object, return true, otherwise return false.
*
* @param {unknown} thing - The thing to check.
*
* @returns {boolean}
*/
function isSpecCompliant(thing) { function isSpecCompliant(thing) {
return thing && utils.isFunction(thing.append) && thing[Symbol.toStringTag] === 'FormData' && thing[Symbol.iterator]; return thing && utils.isFunction(thing.append) && thing[Symbol.toStringTag] === 'FormData' && thing[Symbol.iterator];
} }
/** /**
* Convert a data object to FormData * Convert a data object to FormData
*
* @param {Object} obj * @param {Object} obj
* @param {?Object} [formData] * @param {?Object} [formData]
* @param {?Object} [options] * @param {?Object} [options]
@@ -42,9 +80,19 @@ function isSpecCompliant(thing) {
* @param {Boolean} [options.metaTokens = true] * @param {Boolean} [options.metaTokens = true]
* @param {Boolean} [options.dots = false] * @param {Boolean} [options.dots = false]
* @param {?Boolean} [options.indexes = false] * @param {?Boolean} [options.indexes = false]
*
* @returns {Object} * @returns {Object}
**/ **/
/**
* 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
*/
function toFormData(obj, formData, options) { function toFormData(obj, formData, options) {
if (!utils.isObject(obj)) { if (!utils.isObject(obj)) {
throw new TypeError('target must be an object'); throw new TypeError('target must be an object');
@@ -94,11 +142,13 @@ function toFormData(obj, formData, options) {
} }
/** /**
* Default visitor.
* *
* @param {*} value * @param {*} value
* @param {String|Number} key * @param {String|Number} key
* @param {Array<String|Number>} path * @param {Array<String|Number>} path
* @this {FormData} * @this {FormData}
*
* @returns {boolean} return true to visit the each prop of the value recursively * @returns {boolean} return true to visit the each prop of the value recursively
*/ */
function defaultVisitor(value, key, path) { function defaultVisitor(value, key, path) {