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