-
Targeted File Access:
The getFile(field: string)
function retrieves a specific file from the multipart request based on its field name.
import { getFile } from "@minimajs/multipart";
async function handleFile(fieldName: string) {
const uploadedFile = await getFile(fieldName);
const fileBuffer = await uploadedFile.buffer();
}
-
Iterating Over Multiple Files:
The getFiles()
function provides an asynchronous iterator that allows you to efficiently process each uploaded file sequentially.
import { getFiles } from "@minimajs/multipart";
async function handleMultipleFiles() {
for await (const file of getFiles()) {
console.log(`Received file: ${file.name}`);
}
}
-
Retrieving Form Field Values:
The getFields()
function returns a promise that resolves to a Record<string, unknown>
, providing access to all submitted form fields and their corresponding values.
async function handleFormFields() {
const formData = await getFields();
const username = formData.username as string;
const email = formData.email as string;
}
-
Direct Body Access:
The getBody()
function returns an asynchronous iterator that yields an array containing the field name and its corresponding value (which can be a string or a File
object). This offers maximum flexibility for handling all parts of the request body.
async function handleRequestBody() {
for await (const [fieldName, fieldValue] of getBody()) {
if (typeof fieldValue === "string") {
console.log(`Field: ${fieldName}, Value: ${fieldValue}`);
} else {
console.log(`File uploaded: ${fieldValue.name}`);
}
}
}