@types/multer

  • Version 1.4.7
  • Published
  • 17.3 kB
  • 1 dependency
  • MIT license

Install

npm i @types/multer
yarn add @types/multer
pnpm add @types/multer

Overview

TypeScript definitions for multer

Index

Functions

function diskStorage

diskStorage: (options: DiskStorageOptions) => StorageEngine;
  • Returns a StorageEngine implementation configured to store files on the local file system.

    A string or function may be specified to determine the destination directory, and a function to determine filenames. If no options are set, files will be stored in the system's temporary directory with random 32 character filenames.

function memoryStorage

memoryStorage: () => StorageEngine;
  • Returns a StorageEngine implementation configured to store files in memory as Buffer objects.

function multer

multer: typeof multer;
  • Returns a Multer instance that provides several methods for generating middleware that process files uploaded in multipart/form-data format.

    The StorageEngine specified in storage will be used to store files. If storage is not set and dest is, files will be stored in dest on the local file system with random names. If neither are set, files will be stored in memory.

    In addition to files, all generated middleware process all text fields in the request. For each non-file field, the Request.body object will be populated with an entry mapping the field name to its string value, or array of string values if multiple fields share the same name.

Classes

class MulterError

class MulterError extends Error {}

    constructor

    constructor(code: ErrorCode, field?: string);

      property code

      code: ErrorCode;
      • Identifying error code.

      property field

      field?: string;
      • Name of the multipart form field associated with this error.

      property message

      message: string;
      • Descriptive error message.

      property name

      name: string;
      • Name of the MulterError constructor.

      Interfaces

      interface DiskStorageOptions

      interface DiskStorageOptions {}

        property destination

        destination?:
        | string
        | ((
        req: Request,
        file: Express.Multer.File,
        callback: (error: Error | null, destination: string) => void
        ) => void)
        | undefined;
        • A string or function that determines the destination path for uploaded files. If a string is passed and the directory does not exist, Multer attempts to create it recursively. If neither a string or a function is passed, the destination defaults to os.tmpdir().

          Parameter req

          The Express Request object.

          Parameter file

          Object containing information about the processed file.

          Parameter callback

          Callback to determine the destination path.

        method filename

        filename: (
        req: any,
        file: Express.Multer.File,
        callback: (error: Error | null, filename: string) => void
        ) => void;
        • A function that determines the name of the uploaded file. If nothing is passed, Multer will generate a 32 character pseudorandom hex string with no extension.

          Parameter req

          The Express Request object.

          Parameter file

          Object containing information about the processed file.

          Parameter callback

          Callback to determine the name of the uploaded file.

        interface Field

        interface Field {}
        • An object describing a field name and the maximum number of files with that field name to accept.

        property maxCount

        maxCount?: number | undefined;
        • Optional maximum number of files per field to accept. (Default: Infinity)

        property name

        name: string;
        • The field name.

        interface FileFilterCallback

        interface FileFilterCallback {}
        • a function to control which files should be uploaded and which should be skipped pass a boolean to indicate if the file should be accepted pass an error if something goes wrong

        call signature

        (error: Error): void;

          call signature

          (error: null, acceptFile: boolean): void;

            interface Multer

            interface Multer {}

            method any

            any: () => any;
            • Returns middleware that processes all files contained in the multipart request.

              The Request object will be populated with a files array containing an information object for each processed file.

            method array

            array: (fieldName: string, maxCount?: number) => any;
            • Returns middleware that processes multiple files sharing the same field name.

              The Request object will be populated with a files array containing an information object for each processed file.

              Parameter fieldName

              Shared name of the multipart form fields to process.

              Parameter maxCount

              Optional. Maximum number of files to process. (default: Infinity)

              Throws

              MulterError('LIMIT_UNEXPECTED_FILE') if more than maxCount files are associated with fieldName

            method fields

            fields: (fields: ReadonlyArray<Field>) => any;
            • Returns middleware that processes multiple files associated with the given form fields.

              The Request object will be populated with a files object which maps each field name to an array of the associated file information objects.

              Parameter fields

              Array of Field objects describing multipart form fields to process.

              Throws

              MulterError('LIMIT_UNEXPECTED_FILE') if more than maxCount files are associated with fieldName for any field.

            method none

            none: () => any;
            • Returns middleware that accepts only non-file multipart form fields.

              Throws

              MulterError('LIMIT_UNEXPECTED_FILE') if any file is encountered.

            method single

            single: (fieldName: string) => any;
            • Returns middleware that processes a single file associated with the given form field.

              The Request object will be populated with a file object containing information about the processed file.

              Parameter fieldName

              Name of the multipart form field to process.

            interface Options

            interface Options {}
            • Options for initializing a Multer instance.

            property dest

            dest?: string | undefined;
            • The destination directory for uploaded files. If storage is not set and dest is, Multer will create a DiskStorage instance configured to store files at dest with random filenames.

              Ignored if storage is set.

            property limits

            limits?:
            | {
            /** Maximum size of each form field name in bytes. (Default: 100) */
            fieldNameSize?: number | undefined;
            /** Maximum size of each form field value in bytes. (Default: 1048576) */
            fieldSize?: number | undefined;
            /** Maximum number of non-file form fields. (Default: Infinity) */
            fields?: number | undefined;
            /** Maximum size of each file in bytes. (Default: Infinity) */
            fileSize?: number | undefined;
            /** Maximum number of file fields. (Default: Infinity) */
            files?: number | undefined;
            /** Maximum number of parts (non-file fields + files). (Default: Infinity) */
            parts?: number | undefined;
            /** Maximum number of headers. (Default: 2000) */
            headerPairs?: number | undefined;
            }
            | undefined;
            • An object specifying various limits on incoming data. This object is passed to Busboy directly, and the details of properties can be found at https://github.com/mscdex/busboy#busboy-methods.

            property preservePath

            preservePath?: boolean | undefined;
            • Preserve the full path of the original filename rather than the basename. (Default: false)

            property storage

            storage?: StorageEngine | undefined;
            • A StorageEngine responsible for processing files uploaded via Multer. Takes precedence over dest.

            method fileFilter

            fileFilter: (
            req: any,
            file: Express.Multer.File,
            callback: FileFilterCallback
            ) => void;
            • Optional function to control which files are uploaded. This is called for every file that is processed.

              Parameter req

              The Express Request object.

              Parameter file

              Object containing information about the processed file.

              Parameter callback

              a function to control which files should be uploaded and which should be skipped.

            interface StorageEngine

            interface StorageEngine {}
            • Implementations of this interface are responsible for storing files encountered by Multer and returning information on how to access them once stored. Implementations must also provide a method for removing files in the event that an error occurs.

            Type Aliases

            type ErrorCode

            type ErrorCode =
            | 'LIMIT_PART_COUNT'
            | 'LIMIT_FILE_SIZE'
            | 'LIMIT_FILE_COUNT'
            | 'LIMIT_FIELD_KEY'
            | 'LIMIT_FIELD_VALUE'
            | 'LIMIT_FIELD_COUNT'
            | 'LIMIT_UNEXPECTED_FILE';

              Namespaces

              namespace global

              namespace global {}

                namespace global.Express

                namespace global.Express {}

                  interface Request

                  interface Request {}

                    property file

                    file?: Multer.File | undefined;
                    • Multer.File object populated by single() middleware.

                    property files

                    files?:
                    | {
                    [fieldname: string]: Multer.File[];
                    }
                    | Multer.File[]
                    | undefined;
                    • Array or dictionary of Multer.File object populated by array(), fields(), and any() middleware.

                    namespace global.Express.Multer

                    namespace global.Express.Multer {}

                      interface File

                      interface File {}
                      • Object containing file metadata and access information.

                      property buffer

                      buffer: Buffer;
                      • MemoryStorage only: A Buffer containing the entire file.

                      property destination

                      destination: string;
                      • DiskStorage only: Directory to which this file has been uploaded.

                      property encoding

                      encoding: string;
                      • Value of the Content-Transfer-Encoding header for this file.

                        See Also

                        • RFC 7578, Section 4.7

                        Deprecated

                        since July 2015

                      property fieldname

                      fieldname: string;
                      • Name of the form field associated with this file.

                      property filename

                      filename: string;
                      • DiskStorage only: Name of this file within destination.

                      property mimetype

                      mimetype: string;
                      • Value of the Content-Type header for this file.

                      property originalname

                      originalname: string;
                      • Name of the file on the uploader's computer.

                      property path

                      path: string;
                      • DiskStorage only: Full path to the uploaded file.

                      property size

                      size: number;
                      • Size of the file in bytes.

                      property stream

                      stream: Readable;
                      • A readable stream of this file. Only available to the _handleFile callback for custom StorageEngines.

                      Package Files (1)

                      Dependencies (1)

                      Dev Dependencies (0)

                      No dev dependencies.

                      Peer Dependencies (0)

                      No peer dependencies.

                      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/@types/multer.

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