@k1eu/typed-formdata
Version:
A typed version of FormData
74 lines (73 loc) • 2.08 kB
JavaScript
export class TypedFormData {
constructor(initElement) {
this.formData = new FormData();
if (!initElement) {
return;
}
if (initElement instanceof FormData) {
this.formData = initElement;
return;
}
this.formData = new FormData(initElement);
}
get(key) {
return this.formData.get(key);
}
getAll(key) {
return this.formData.getAll(key);
}
/**
* Executes a provided function once for each key/value pair in the FormData object.
* @deprecated This method is deprecated and is not advised to be used. Use entries() or for...of loop instead.
* @param callbackfn A function that is called for each key/value pair in the FormData object.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
forEach(callbackfn,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
thisArg) {
this.formData.forEach(callbackfn, thisArg);
}
getFormData() {
return this.formData;
}
getObject() {
return Object.fromEntries(this.entries());
}
entries() {
return this.formData.entries();
}
typedEntries() {
return this.entries();
}
keys() {
return this.formData.keys();
}
values() {
return this.formData.values();
}
set(key, value, filename) {
if (typeof value === "string") {
this.formData.set(key, value);
}
else {
this.formData.set(key, value, filename);
}
}
append(key, value, filename) {
if (typeof value === "string") {
this.formData.append(key, value);
}
else {
this.formData.append(key, value, filename);
}
}
has(key) {
return this.formData.has(key);
}
delete(key) {
this.formData.delete(key);
}
*[Symbol.iterator]() {
yield* this.entries();
}
}