UNPKG

@adonisjs/bodyparser

Version:

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

104 lines (103 loc) 3.07 kB
import type { Mode } from 'node:fs'; /** * We can detect file types for these files using the magic * number */ export declare const supportMagicFileTypes: ReadonlySet<string>; /** * Detects the file type, extension, and MIME type/subtype by analyzing * the file's magic number (binary signature). * * @param fileContents - Buffer containing the file contents to analyze * * @example * ```ts * const buffer = await fs.readFile('image.png') * const fileType = await getFileType(buffer) * // { ext: 'png', type: 'image', subtype: 'png' } * ``` */ export declare function getFileType(fileContents: Buffer): Promise<null | { ext: string; type?: string; subtype?: string; }>; /** * Computes the file extension and MIME type from the filename and headers * when magic number detection is not available or applicable. * * @param clientName - The original filename provided by the client * @param headers - Headers object containing the content-type * * @example * ```ts * const fileType = computeFileTypeFromName('document.pdf', { * 'content-type': 'application/pdf' * }) * // { ext: 'pdf', type: 'application', subtype: 'pdf' } * ``` */ export declare function computeFileTypeFromName(clientName: string, headers: { [key: string]: string; }): { ext: string; type?: string; subtype?: string; }; /** * Checks if a file or directory exists at the specified path. * * @param filePath - The path to check for existence * * @example * ```ts * if (await pathExists('/tmp/upload.jpg')) { * console.log('File exists') * } * ``` */ export declare function pathExists(filePath: string): Promise<boolean>; /** * Moves a file from source to destination, automatically handling cross-device * moves by falling back to copy+delete. Creates the destination directory if * it doesn't exist. * * @param sourcePath - The current path of the file * @param destinationPath - The target path for the file * @param options - Move options including overwrite flag and directory permissions * * @example * ```ts * await moveFile('/tmp/upload.jpg', '/app/public/images/photo.jpg', { * overwrite: true * }) * ``` */ export declare function moveFile(sourcePath: string, destinationPath: string, options?: { overwrite: boolean; directoryMode?: Mode; }): Promise<void>; /** * Collection of normalizer functions for processing form body values. * These functions can trim whitespace and/or convert empty strings to null. */ export declare const formBodyNormalizers: { /** * Trims leading and trailing whitespace from a string value. * * @param value - The string to trim */ trimWhitespaces(value: string): string; /** * Trims whitespace and converts empty strings to null. * * @param value - The string to process */ trimWhitespacesAndConvertToNull(value: string): string | null; /** * Converts empty strings to null without trimming. * * @param value - The string to process */ convertToNull(value: string): string | null; };