@brightkyefoo/form-data
Version:
Extension de FormData pour ajouter JSON stringifié si nécessaire
45 lines (44 loc) • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class BrightFormData extends FormData {
append(name, value) {
if (typeof value === 'string' || value instanceof Blob) {
super.append(name, value);
}
else {
super.append(name, JSON.stringify(value));
}
}
get(name) {
const notComputedValue = super.get(name);
if (!notComputedValue)
return null;
if (notComputedValue instanceof File)
return notComputedValue;
try {
return JSON.parse(notComputedValue);
}
catch (_) {
return notComputedValue;
}
}
set(name, value) {
if (typeof value === 'string' || value instanceof Blob) {
super.set(name, value);
}
else {
super.set(name, JSON.stringify(value));
}
}
/**
* This method help appending multiple files as an array to the key name specified
* @param name is the name of the key you want to append
* @param files is an array of files
*/
addFiles(name, files) {
for (let i = 0; i < files.length; i++) {
this.append(`${name}[]`, files[i]);
}
}
}
exports.default = BrightFormData;