HttpFormData
An extended HTTP form data object with a strictly-typed schema. Fully compatible with the built-in
FormData
class.
import { HttpFormData } from '@zimic/http';
const formData = new HttpFormData<{
files: File[];
description?: string;
}>();
formData.append('file', new File(['content'], 'file.txt', { type: 'text/plain' }));
formData.append('description', 'My file');
const files = formData.getAll('file');
console.log(files); // [File { name: 'file.txt', type: 'text/plain' }]
const description = formData.get('description');
console.log(description); // 'My file'
constructor()
Creates a new HttpFormData
instance.
new HttpFormData<Schema>();
Type arguments:
-
Schema:
HttpFormDataSchema.Loose
An object type whose keys are the form data fields and values are the expected types of those fields. This schema is used to enforce type safety when using the form data instance.
formData.set()
Sets a form data value. If the value already exists, it will be replaced.
formData.set(name, value);
formData.set(name, value, fileName);
Arguments:
-
name:
string
The name of the form data field to set.
-
value:
string | File
The value to set for the form data field.
-
fileName:
string | undefined
The name of the file to set when the second parameter is
Blob
orFile
.
Related:
formData.append()
Appends a value to a form data field, or adds the field if it does not exist.
formData.append(name, value);
formData.append(name, value, fileName);
Arguments:
-
name:
string
The name of the form data field to append to.
-
value:
string | File
The value to append for the form data field.
-
fileName:
string | undefined
The name of the file to append when the second parameter is
Blob
orFile
.
Related:
formData.get()
Retrieves the value of a given form data field. If the value of the key is an array in the schema, use
formData.getAll()
instead.
formData.get(name);
Arguments:
-
name:
string
The name of the form data field to retrieve.
Returns: string | File | null
The value of the form data field, or null
if it does not exist.
Related:
formData.getAll()
Retrieves all values of a given form data field. If the value of the key is not an array in the schema, use
formData.get()
instead.
formData.getAll(name);
Arguments:
-
name:
string
The name of the form data field to retrieve.
Returns: (string | File)[]
An array of values for the form data field, or an empty array if it does not exist.
Related:
formData.has()
Checks if a form data field with the given name exists.
formData.has(name);
Arguments:
-
name:
string
The name of the form data field to check.
Returns: boolean
true
if the form data field exists, false
otherwise.
Related:
formData.delete()
Deletes a form data field with the given name.
formData.delete(name);
Arguments:
-
name:
string
The name of the form data field to delete.
Related:
formData.forEach()
Executes a function for each form data (name
, value
) pair.
formData.forEach(callback);
formData.forEach(callback, thisArg);
Arguments:
-
callback:
(value: string | File, name: string, formData: HttpFormData) => void
A function that will be called for each form data field.
-
thisArg:
HttpFormData | undefined
A value to use as
this
when executingcallback
.
Related:
formData.keys()
formData.keys();
Returns: Iterator<string>
An iterator over all form data field names.
Related:
formData.values()
formData.values();
Returns: Iterator<string | File>
An iterator over all form data field values.
Related:
formData.entries()
formData.entries();
Returns: Iterator<[string, string | File]>
An iterator over all form data (name
, value
) pairs.
Related:
formData.equals()
Compares an HttpFormData
instance with another to check if they are equal. Equality is defined as having the same keys
and values, regardless of the order of the keys.
formData.equals(otherFormData);
Arguments:
-
otherFormData:
HttpFormData
Another
HttpFormData
instance to compare with.
Returns: boolean
true
if the two form data objects are equal, false
otherwise.
formData.contains()
Checks if an HttpFormData
instance contains all keys and values of another HttpFormData
instance.
formData.contains(otherFormData);
Arguments:
-
otherFormData:
HttpFormData
Another
HttpFormData
instance to check against.
Returns: boolean
true
if the current form data contains all keys and values of the other form data, false
otherwise.
formData.toObject()
Converts the HttpFormData
instance to a plain object. This method is useful for serialization and debugging purposes.
formData.toObject();
Returns: Record<string, string | File | (string | File)[]>
A plain object representation of the form data. If a key has multiple values, the object will contain an array of values for that key. If the key has only one value, the object will contain its value directly, without an array, regardless of how the value was initialized when creating the form data object.
const formData = new HttpFormData<{
title: string;
content: Blob[];
}>();
formData.set('title', 'My title');
formData.set('content', new Blob(['content 1'], { type: 'text/plain' }));
formData.set('content', new Blob(['content 2'], { type: 'text/plain' }));
const object = formData.toObject();
console.log(object); // { title: 'My title', content: [Blob, Blob] }