@adonisjs/bodyparser
Version:
BodyParser middleware for AdonisJS http server to read and parse request body
81 lines (80 loc) • 2.75 kB
TypeScript
import type { HttpContext } from '@adonisjs/http-server';
import { type prepareMultipartConfig } from '../parsers/multipart.ts';
import type { FileValidationOptions, PartHandler as PartHandlerType } from '../types.ts';
/**
* Multipart class offers a low level API to interact with the incoming
* HTTP request data as a stream. This makes it super easy to
* write files to S3 without saving them to the disk first.
*/
export declare class Multipart {
#private;
/**
* The current state of the multipart form handler
*/
state: 'idle' | 'processing' | 'error' | 'success';
/**
* Creates a new Multipart instance for processing multipart form data
*
* @param ctx - The HTTP context
* @param config - Configuration options for multipart processing
* @param _featureFlags - Feature flags (unused)
*/
constructor(ctx: HttpContext, config?: ReturnType<typeof prepareMultipartConfig>, _featureFlags?: Record<string, never>);
/**
* Attach handler for a given file. To handle all files, you
* can attach a wildcard handler.
*
* @param name - The field name to handle, or '*' for wildcard
* @param options - Validation options and configuration
* @param handler - The handler function to process the file
*
* @example
* ```ts
* multipart.onFile('package', {}, async (stream) => {
* })
*
* multipart.onFile('*', {}, async (stream) => {
* })
* ```
*/
onFile(name: string, options: Partial<FileValidationOptions & {
deferValidations: boolean;
}>, handler: PartHandlerType): this;
/**
* Aborts the multipart stream processing by emitting an error event.
* This will stop all file processing and reject the process promise.
*
* @param error - The error that caused the abort
*
* @example
* ```ts
* multipart.onFile('*', {}, async (part) => {
* if (part.file.size > MAX_SIZE) {
* multipart.abort(new Error('File too large'))
* }
* })
* ```
*/
abort(error: any): void;
/**
* Processes the multipart request by parsing all file and field streams.
* Must be called after registering all file handlers with onFile.
*
* @param config - Optional configuration overrides for this specific request
*
* @example
* ```ts
* const multipart = ctx.request.multipart
*
* multipart.onFile('avatar', {}, async (part, reportChunk) => {
* await streamFile(part, '/tmp/avatar.jpg', reportChunk)
* })
*
* await multipart.process()
* ```
*/
process(config?: Partial<{
limit: string | number;
maxFields: number;
}>): Promise<void>;
}