UNPKG

@adonisjs/bodyparser

Version:

BodyParser middleware for AdonisJS http server to read and parse request body

178 lines (177 loc) 4.91 kB
import Macroable from '@poppinss/macroable'; import { SizeValidator } from './validators/size.ts'; import { ExtensionValidator } from './validators/extensions.ts'; import type { FileJSON, FileUploadError, FileValidationOptions } from '../types.ts'; declare const STORE_IN_FLASH: unique symbol; /** * The file holds the meta/data for an uploaded file, along with * any errors that occurred during the upload process. */ export declare class MultipartFile extends Macroable { [STORE_IN_FLASH]: boolean; /** * File validators for size and extension validation */ sizeValidator: SizeValidator; extensionValidator: ExtensionValidator; /** * A boolean to know if file is an instance of this class * or not */ isMultipartFile: true; /** * Field name is the name of the field */ fieldName: string; /** * Client name is the file name on the user client */ clientName: string; /** * The headers sent as part of the multipart request */ headers: Record<string, any>; /** * File size in bytes */ size: number; /** * The extension for the file */ extname?: string; /** * Upload errors that occurred during processing */ errors: FileUploadError[]; /** * Type and subtype are extracted from the `content-type` * header or from the file magic number */ type?: string; subtype?: string; /** * File path is only set after the move operation */ filePath?: string; /** * File name is only set after the move operation. It is the relative * path of the moved file */ fileName?: string; /** * Temporary path, only exists when file is uploaded using the * classic mode */ tmpPath?: string; /** * The file metadata */ meta: any; /** * The current state of the file */ state: 'idle' | 'streaming' | 'consumed' | 'moved'; /** * Whether or not the validations have been executed */ get validated(): boolean; /** * A boolean to know if file has one or more errors */ get isValid(): boolean; /** * Opposite of [[this.isValid]] */ get hasErrors(): boolean; /** * The maximum file size limit */ get sizeLimit(): number | string | undefined; set sizeLimit(limit: number | string | undefined); /** * Extensions allowed */ get allowedExtensions(): string[] | undefined; set allowedExtensions(extensions: string[] | undefined); /** * Creates a new MultipartFile instance * * @param data - Object containing field name, client name, and headers * @param validationOptions - Validation options for the file */ constructor(data: { fieldName: string; clientName: string; headers: any; }, validationOptions: Partial<FileValidationOptions>); /** * Runs all configured validators (size and extension) on the file. * Validation results are stored in the errors array. * * @example * ```ts * file.sizeLimit = '2mb' * file.allowedExtensions = ['jpg', 'png'] * file.validate() * * if (!file.isValid) { * console.log(file.errors) * } * ``` */ validate(): void; /** * Mark file as moved to its final destination * * @param fileName - The name of the moved file * @param filePath - The full path where the file was moved */ markAsMoved(fileName: string, filePath: string): void; /** * Moves the file from its temporary location to a permanent destination. * Can be called multiple times to copy the file to multiple locations. * * @param location - The destination directory path * @param options - Move options including custom filename and overwrite flag * * @example * ```ts * const avatar = request.file('avatar') * * if (avatar) { * await avatar.move(app.publicPath('uploads'), { * name: `${Date.now()}.${avatar.extname}`, * overwrite: true * }) * } * ``` */ move(location: string, options?: { name?: string; overwrite?: boolean; }): Promise<void>; /** * Serializes the file to a JSON-compatible object containing all metadata, * validation state, and file paths. * * @example * ```ts * const file = request.file('avatar') * console.log(file?.toJSON()) * // { * // fieldName: 'avatar', * // clientName: 'profile.jpg', * // size: 45056, * // extname: 'jpg', * // type: 'image', * // subtype: 'jpeg', * // state: 'consumed', * // isValid: true, * // validated: true, * // errors: [] * // } * ``` */ toJSON(): FileJSON; } export {};