@adonisjs/bodyparser

  • Version 10.0.2
  • Published
  • 140 kB
  • 11 dependencies
  • MIT license

Install

npm i @adonisjs/bodyparser
yarn add @adonisjs/bodyparser
pnpm add @adonisjs/bodyparser

Overview

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

Index

Functions

function defineConfig

defineConfig: (config: BodyParserOptionalConfig) => BodyParserConfig;
  • Define config for the bodyparser middleware. Your defined config will be merged with the default config

Classes

class Multipart

class Multipart {}
  • Multipart class offers a low level API to interact 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.

constructor

constructor(
ctx: HttpContext,
config?: Partial<{
limit: string | number;
fieldsLimit: string | number;
maxFields: number;
convertEmptyStringsToNull: boolean;
}>
);

    property state

    state: 'idle' | 'processing' | 'error' | 'success';
    • The current state of the multipart form handler

    method abort

    abort: (error: any) => void;
    • Abort request by emitting error

    method onFile

    onFile: (
    name: string,
    options: Partial<FileValidationOptions & { deferValidations: boolean }>,
    handler: PartHandlerType
    ) => this;
    • Attach handler for a given file. To handle all files, you can attach a wildcard handler.

      Example 1

      multipart.onFile('package', {}, async (stream) => {
      })
      multipart.onFile('*', {}, async (stream) => {
      })

    method process

    process: (
    config?: Partial<{ limit: string | number; maxFields: number }>
    ) => Promise<void>;
    • Process the request by going all the file and field streams.

    class MultipartFile

    class MultipartFile extends Macroable {}
    • The file holds the meta/data for an uploaded file, along with an errors occurred during the upload process.

    constructor

    constructor(
    data: { fieldName: string; clientName: string; headers: any },
    validationOptions: Partial<FileValidationOptions>
    );

      property allowedExtensions

      allowedExtensions: string[];
      • Extensions allowed

      property clientName

      clientName: string;
      • Client name is the file name on the user client

      property errors

      errors: FileUploadError[];
      • Upload errors

      property extname

      extname?: string;
      • The extname for the file.

      property fieldName

      fieldName: string;
      • Field name is the name of the field

      property fileName

      fileName?: string;
      • File name is only set after the move operation. It is the relative path of the moved file

      property filePath

      filePath?: string;
      • File path is only set after the move operation

      property hasErrors

      readonly hasErrors: boolean;
      • Opposite of [[this.isValid]]

      property headers

      headers: Record<string, any>;
      • The headers sent as part of the multipart request

      property isMultipartFile

      isMultipartFile: boolean;
      • A boolean to know if file is an instance of this class or not

      property isValid

      readonly isValid: boolean;
      • A boolean to know if file has one or more errors

      property meta

      meta: any;
      • The file meta data

      property size

      size: number;
      • File size in bytes

      property sizeLimit

      sizeLimit: string | number;
      • The maximum file size limit

      property state

      state: 'idle' | 'streaming' | 'consumed' | 'moved';
      • The state of the file

      property subtype

      subtype?: string;

        property tmpPath

        tmpPath?: string;
        • Tmp path, only exists when file is uploaded using the classic mode.

        property type

        type?: string;
        • Type and subtype are extracted from the content-type header or from the file magic number

        property validated

        readonly validated: boolean;
        • Whether or not the validations have been executed

        method markAsMoved

        markAsMoved: (fileName: string, filePath: string) => void;
        • Mark file as moved

        method move

        move: (
        location: string,
        options?: { name?: string; overwrite?: boolean }
        ) => Promise<void>;
        • Moves the file to a given location. Multiple calls to the move method are allowed, incase you want to move a file to multiple locations.

        method toJSON

        toJSON: () => FileJSON;
        • Returns file JSON representation

        method validate

        validate: () => void;
        • Validate the file

        Package Files (4)

        Dependencies (11)

        Dev Dependencies (37)

        Peer Dependencies (1)

        Badge

        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@adonisjs/bodyparser.

        • Markdown
          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@adonisjs/bodyparser)
        • HTML
          <a href="https://www.jsdocs.io/package/@adonisjs/bodyparser"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>