UNPKG

12.8 kBTypeScriptView Raw
1import { Request, RequestHandler } from "express";
2import { Readable } from "stream";
3
4declare global {
5 namespace Express {
6 namespace Multer {
7 /** Object containing file metadata and access information. */
8 interface File {
9 /** Name of the form field associated with this file. */
10 fieldname: string;
11 /** Name of the file on the uploader's computer. */
12 originalname: string;
13 /**
14 * Value of the `Content-Transfer-Encoding` header for this file.
15 * @deprecated since July 2015
16 * @see RFC 7578, Section 4.7
17 */
18 encoding: string;
19 /** Value of the `Content-Type` header for this file. */
20 mimetype: string;
21 /** Size of the file in bytes. */
22 size: number;
23 /**
24 * A readable stream of this file. Only available to the `_handleFile`
25 * callback for custom `StorageEngine`s.
26 */
27 stream: Readable;
28 /** `DiskStorage` only: Directory to which this file has been uploaded. */
29 destination: string;
30 /** `DiskStorage` only: Name of this file within `destination`. */
31 filename: string;
32 /** `DiskStorage` only: Full path to the uploaded file. */
33 path: string;
34 /** `MemoryStorage` only: A Buffer containing the entire file. */
35 buffer: Buffer;
36 }
37 }
38
39 interface Request {
40 /** `Multer.File` object populated by `single()` middleware. */
41 file?: Multer.File | undefined;
42 /**
43 * Array or dictionary of `Multer.File` object populated by `array()`,
44 * `fields()`, and `any()` middleware.
45 */
46 files?:
47 | {
48 [fieldname: string]: Multer.File[];
49 }
50 | Multer.File[]
51 | undefined;
52 }
53 }
54}
55
56/**
57 * Returns a Multer instance that provides several methods for generating
58 * middleware that process files uploaded in `multipart/form-data` format.
59 *
60 * The `StorageEngine` specified in `storage` will be used to store files. If
61 * `storage` is not set and `dest` is, files will be stored in `dest` on the
62 * local file system with random names. If neither are set, files will be stored
63 * in memory.
64 *
65 * In addition to files, all generated middleware process all text fields in
66 * the request. For each non-file field, the `Request.body` object will be
67 * populated with an entry mapping the field name to its string value, or array
68 * of string values if multiple fields share the same name.
69 */
70declare function multer(options?: multer.Options): multer.Multer;
71
72declare namespace multer {
73 /**
74 * @see {@link https://github.com/expressjs/multer#api}
75 */
76 interface Multer {
77 /**
78 * Returns middleware that processes a single file associated with the
79 * given form field.
80 *
81 * The `Request` object will be populated with a `file` object containing
82 * information about the processed file.
83 *
84 * @param fieldName Name of the multipart form field to process.
85 */
86 single(fieldName: string): RequestHandler;
87 /**
88 * Returns middleware that processes multiple files sharing the same field
89 * name.
90 *
91 * The `Request` object will be populated with a `files` array containing
92 * an information object for each processed file.
93 *
94 * @param fieldName Shared name of the multipart form fields to process.
95 * @param maxCount Optional. Maximum number of files to process. (default: Infinity)
96 * @throws `MulterError('LIMIT_UNEXPECTED_FILE')` if more than `maxCount` files are associated with `fieldName`
97 */
98 array(fieldName: string, maxCount?: number): RequestHandler;
99 /**
100 * Returns middleware that processes multiple files associated with the
101 * given form fields.
102 *
103 * The `Request` object will be populated with a `files` object which
104 * maps each field name to an array of the associated file information
105 * objects.
106 *
107 * @param fields Array of `Field` objects describing multipart form fields to process.
108 * @throws `MulterError('LIMIT_UNEXPECTED_FILE')` if more than `maxCount` files are associated with `fieldName` for any field.
109 */
110 fields(fields: readonly Field[]): RequestHandler;
111 /**
112 * Returns middleware that processes all files contained in the multipart
113 * request.
114 *
115 * The `Request` object will be populated with a `files` array containing
116 * an information object for each processed file.
117 */
118 any(): RequestHandler;
119 /**
120 * Returns middleware that accepts only non-file multipart form fields.
121 *
122 * @throws `MulterError('LIMIT_UNEXPECTED_FILE')` if any file is encountered.
123 */
124 none(): RequestHandler;
125 }
126
127 /**
128 * Returns a `StorageEngine` implementation configured to store files on
129 * the local file system.
130 *
131 * A string or function may be specified to determine the destination
132 * directory, and a function to determine filenames. If no options are set,
133 * files will be stored in the system's temporary directory with random 32
134 * character filenames.
135 */
136 function diskStorage(options: DiskStorageOptions): StorageEngine;
137
138 /**
139 * Returns a `StorageEngine` implementation configured to store files in
140 * memory as `Buffer` objects.
141 */
142 function memoryStorage(): StorageEngine;
143
144 type ErrorCode =
145 | "LIMIT_PART_COUNT"
146 | "LIMIT_FILE_SIZE"
147 | "LIMIT_FILE_COUNT"
148 | "LIMIT_FIELD_KEY"
149 | "LIMIT_FIELD_VALUE"
150 | "LIMIT_FIELD_COUNT"
151 | "LIMIT_UNEXPECTED_FILE";
152
153 class MulterError extends Error {
154 constructor(code: ErrorCode, field?: string);
155 /** Name of the MulterError constructor. */
156 name: string;
157 /** Identifying error code. */
158 code: ErrorCode;
159 /** Descriptive error message. */
160 message: string;
161 /** Name of the multipart form field associated with this error. */
162 field?: string | undefined;
163 }
164
165 /**
166 * a function to control which files should be uploaded and which should be skipped
167 * pass a boolean to indicate if the file should be accepted
168 * pass an error if something goes wrong
169 */
170 interface FileFilterCallback {
171 (error: Error): void;
172 (error: null, acceptFile: boolean): void;
173 }
174
175 /** Options for initializing a Multer instance. */
176 interface Options {
177 /**
178 * A `StorageEngine` responsible for processing files uploaded via Multer.
179 * Takes precedence over `dest`.
180 */
181 storage?: StorageEngine | undefined;
182 /**
183 * The destination directory for uploaded files. If `storage` is not set
184 * and `dest` is, Multer will create a `DiskStorage` instance configured
185 * to store files at `dest` with random filenames.
186 *
187 * Ignored if `storage` is set.
188 */
189 dest?: string | undefined;
190 /**
191 * An object specifying various limits on incoming data. This object is
192 * passed to Busboy directly, and the details of properties can be found
193 * at https://github.com/mscdex/busboy#busboy-methods.
194 */
195 limits?: {
196 /** Maximum size of each form field name in bytes. (Default: 100) */
197 fieldNameSize?: number | undefined;
198 /** Maximum size of each form field value in bytes. (Default: 1048576) */
199 fieldSize?: number | undefined;
200 /** Maximum number of non-file form fields. (Default: Infinity) */
201 fields?: number | undefined;
202 /** Maximum size of each file in bytes. (Default: Infinity) */
203 fileSize?: number | undefined;
204 /** Maximum number of file fields. (Default: Infinity) */
205 files?: number | undefined;
206 /** Maximum number of parts (non-file fields + files). (Default: Infinity) */
207 parts?: number | undefined;
208 /** Maximum number of headers. (Default: 2000) */
209 headerPairs?: number | undefined;
210 } | undefined;
211 /** Preserve the full path of the original filename rather than the basename. (Default: false) */
212 preservePath?: boolean | undefined;
213 /**
214 * Optional function to control which files are uploaded. This is called
215 * for every file that is processed.
216 *
217 * @param req The Express `Request` object.
218 * @param file Object containing information about the processed file.
219 * @param callback a function to control which files should be uploaded and which should be skipped.
220 */
221 fileFilter?(
222 req: Request,
223 file: Express.Multer.File,
224 callback: FileFilterCallback,
225 ): void;
226 }
227
228 /**
229 * Implementations of this interface are responsible for storing files
230 * encountered by Multer and returning information on how to access them
231 * once stored. Implementations must also provide a method for removing
232 * files in the event that an error occurs.
233 */
234 interface StorageEngine {
235 /**
236 * Store the file described by `file`, then invoke the callback with
237 * information about the stored file.
238 *
239 * File contents are available as a stream via `file.stream`. Information
240 * passed to the callback will be merged with `file` for subsequent
241 * middleware.
242 *
243 * @param req The Express `Request` object.
244 * @param file Object with `stream`, `fieldname`, `originalname`, `encoding`, and `mimetype` defined.
245 * @param callback Callback to specify file information.
246 */
247 _handleFile(
248 req: Request,
249 file: Express.Multer.File,
250 callback: (error?: any, info?: Partial<Express.Multer.File>) => void,
251 ): void;
252 /**
253 * Remove the file described by `file`, then invoke the callback with.
254 *
255 * `file` contains all the properties available to `_handleFile`, as
256 * well as those returned by `_handleFile`.
257 *
258 * @param req The Express `Request` object.
259 * @param file Object containing information about the processed file.
260 * @param callback Callback to indicate completion.
261 */
262 _removeFile(
263 req: Request,
264 file: Express.Multer.File,
265 callback: (error: Error | null) => void,
266 ): void;
267 }
268
269 interface DiskStorageOptions {
270 /**
271 * A string or function that determines the destination path for uploaded
272 * files. If a string is passed and the directory does not exist, Multer
273 * attempts to create it recursively. If neither a string or a function
274 * is passed, the destination defaults to `os.tmpdir()`.
275 *
276 * @param req The Express `Request` object.
277 * @param file Object containing information about the processed file.
278 * @param callback Callback to determine the destination path.
279 */
280 destination?:
281 | string
282 | ((
283 req: Request,
284 file: Express.Multer.File,
285 callback: (error: Error | null, destination: string) => void,
286 ) => void)
287 | undefined;
288 /**
289 * A function that determines the name of the uploaded file. If nothing
290 * is passed, Multer will generate a 32 character pseudorandom hex string
291 * with no extension.
292 *
293 * @param req The Express `Request` object.
294 * @param file Object containing information about the processed file.
295 * @param callback Callback to determine the name of the uploaded file.
296 */
297 filename?(
298 req: Request,
299 file: Express.Multer.File,
300 callback: (error: Error | null, filename: string) => void,
301 ): void;
302 }
303
304 /**
305 * An object describing a field name and the maximum number of files with
306 * that field name to accept.
307 */
308 interface Field {
309 /** The field name. */
310 name: string;
311 /** Optional maximum number of files per field to accept. (Default: Infinity) */
312 maxCount?: number | undefined;
313 }
314}
315
316export = multer;