mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-01 09:01:45 +08:00
Updated multer typings
This commit is contained in:
@@ -4,5 +4,30 @@
|
||||
import express = require('express');
|
||||
import multer = require('multer');
|
||||
|
||||
var app: express.Express = express();
|
||||
app.use(multer());
|
||||
var upload = multer({ dest: 'uploads/' });
|
||||
|
||||
var app = express();
|
||||
|
||||
app.post('/profile', upload.single('avatar'), (req, res, next) => {
|
||||
});
|
||||
|
||||
app.post('/photos/upload', upload.array('photos', 12), (req, res, next) => {
|
||||
});
|
||||
|
||||
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
|
||||
app.post('/cool-profile', cpUpload, (req, res, next) => {
|
||||
});
|
||||
|
||||
var diskStorage = multer.diskStorage({
|
||||
destination(req, file, cb) {
|
||||
cb(null, '/tmp/my-uploads');
|
||||
},
|
||||
filename(req, file, cb) {
|
||||
cb(null, file.fieldname + '-' + Date.now());
|
||||
}
|
||||
})
|
||||
|
||||
var diskUpload = multer({ storage: diskStorage });
|
||||
|
||||
var memoryStorage = multer.memoryStorage();
|
||||
var memoryUpload = multer({ storage: memoryStorage });
|
||||
|
||||
85
multer/multer.d.ts
vendored
85
multer/multer.d.ts
vendored
@@ -1,16 +1,16 @@
|
||||
// Type definitions for multer
|
||||
// Project: https://github.com/expressjs/multer
|
||||
// Definitions by: jt000 <https://github.com/jt000>, vilicvane <https://vilic.github.io/>
|
||||
// Definitions by: jt000 <https://github.com/jt000>, vilicvane <https://vilic.github.io/>, David Broder-Rodgers <https://github.com/DavidBR-SW>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../express/express.d.ts" />
|
||||
|
||||
|
||||
declare module Express {
|
||||
export interface Request {
|
||||
file: Multer.File;
|
||||
files: {
|
||||
[fieldname: string]: Multer.File
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module Multer {
|
||||
@@ -40,13 +40,19 @@ declare module Express {
|
||||
declare module "multer" {
|
||||
import express = require('express');
|
||||
|
||||
function multer(options?: multer.Options): express.RequestHandler;
|
||||
|
||||
module multer {
|
||||
interface Field {
|
||||
/** The field name. */
|
||||
name: string;
|
||||
/** Optional maximum number of files per field to accept. */
|
||||
maxCount?: number;
|
||||
}
|
||||
|
||||
type Options = {
|
||||
interface Options {
|
||||
/** The destination directory for the uploaded files. */
|
||||
dest?: string;
|
||||
/** The storage engine to use for uploaded files. */
|
||||
storage?: StorageEngine;
|
||||
/** An object specifying the size limits of the following optional properties. This object is passed to busboy directly, and the details of properties can be found on https://github.com/mscdex/busboy#busboy-methods */
|
||||
limits?: {
|
||||
/** Max field name size (Default: 100 bytes) */
|
||||
@@ -64,36 +70,45 @@ declare module "multer" {
|
||||
/** For multipart forms, the max number of header key=> value pairs to parse Default: 2000(same as node's http). */
|
||||
headerPairs?: number;
|
||||
};
|
||||
/** A Boolean value to specify whether empty submitted values should be processed and applied to req.body; defaults to false; */
|
||||
includeEmptyFields?: boolean;
|
||||
/** If this Boolean value is true, the file.buffer property holds the data in-memory that Multer would have written to disk. The dest option is still populated and the path property contains the proposed path to save the file. Defaults to false. */
|
||||
inMemory?: boolean;
|
||||
/** Function to rename the uploaded files. Whatever the function returns will become the new name of the uploaded file (extension is not included). The fieldname and filename of the file will be available in this function, use them if you need to. */
|
||||
rename?: (fieldname: string, filename: string, req: Express.Request, res: Express.Response) => string;
|
||||
/** Function to rename the directory in which to place uploaded files. The dest parameter is the default value originally assigned or passed into multer. The req and res parameters are also passed into the function because they may contain information (eg session data) needed to create the path (eg get userid from the session). */
|
||||
changeDest?: (dest: string, req: Express.Request, res: Express.Response) => string;
|
||||
/** Event handler triggered when a file starts to be uploaded. A file object, with the following properties, is available to this function: fieldname, originalname, name, encoding, mimetype, path, and extension. */
|
||||
onFileUploadStart?: (file: Express.Multer.File, req: Express.Request, res: Express.Response) => void;
|
||||
/** Event handler triggered when a chunk of buffer is received. A buffer object along with a file object is available to the function. */
|
||||
onFileUploadData?: (file: Express.Multer.File, data: Buffer, req: Express.Request, res: Express.Response) => void;
|
||||
/** Event handler trigger when a file is completely uploaded. A file object is available to the function. */
|
||||
onFileUploadComplete?: (file: Express.Multer.File, req: Express.Request, res: Express.Response) => void;
|
||||
/** Event handler triggered when the form parsing starts. */
|
||||
onParseStart?: () => void;
|
||||
/** Event handler triggered when the form parsing completes. The request object and the next objects are are passed to the function. */
|
||||
onParseEnd?: (req: Express.Request, next: () => void) => void;
|
||||
/** Event handler for any errors encountering while processing the form. The error object and the next object is available to the function. If you are handling errors yourself, make sure to terminate the request or call the next() function, else the request will be left hanging. */
|
||||
onError?: () => void;
|
||||
/** Event handler triggered when a file size exceeds the specification in the limit object. No more files will be parsed after the limit is reached. */
|
||||
onFileSizeLimit?: (file: Express.Multer.File) => void;
|
||||
/** Event handler triggered when the number of files exceed the specification in the limit object. No more files will be parsed after the limit is reached. */
|
||||
onFilesLimit?: () => void;
|
||||
/** Event handler triggered when the number of fields exceed the specification in the limit object. No more fields will be parsed after the limit is reached. */
|
||||
onFieldsLimit?: () => void;
|
||||
/** Event handler triggered when the number of parts exceed the specification in the limit object. No more files or fields will be parsed after the limit is reached. */
|
||||
onPartsLimit?: () => void;
|
||||
};
|
||||
/** A function to control which files to upload and which to skip. */
|
||||
fileFilter?: (req: Express.Request, file: Express.Multer.File, callback: (error: Error, acceptFile: boolean) => void) => void;
|
||||
}
|
||||
|
||||
interface StorageEngine {
|
||||
_handleFile(req: express.Request, file: Express.Multer.File, callback: (error?: any, info?: Express.Multer.File) => void): void;
|
||||
_removeFile(req: express.Request, file: Express.Multer.File, callback: (error: Error) => void): void;
|
||||
}
|
||||
|
||||
interface DiskStorageOptions {
|
||||
/** A function used to determine within which folder the uploaded files should be stored. Defaults to the system's default temporary directory. */
|
||||
destination?: (req: Express.Request, file: Express.Multer.File, callback: (error: Error, destination: string) => void) => void;
|
||||
/** A function used to determine what the file should be named inside the folder. Defaults to a random name with no file extension. */
|
||||
filename?: (req: Express.Request, file: Express.Multer.File, callback: (error: Error, filename: string) => void) => void;
|
||||
}
|
||||
|
||||
interface Instance {
|
||||
/** Accept a single file with the name fieldname. The single file will be stored in req.file. */
|
||||
single(fieldame: string): express.RequestHandler;
|
||||
/** Accept an array of files, all with the name fieldname. Optionally error out if more than maxCount files are uploaded. The array of files will be stored in req.files. */
|
||||
array(fieldame: string, maxCount?: number): express.RequestHandler;
|
||||
/** Accept a mix of files, specified by fields. An object with arrays of files will be stored in req.files. */
|
||||
fields(fields: Field[]): express.RequestHandler;
|
||||
/** Accepts all files that comes over the wire. An array of files will be stored in req.files. */
|
||||
any(): express.RequestHandler;
|
||||
}
|
||||
}
|
||||
|
||||
interface Multer {
|
||||
|
||||
(options?: multer.Options): multer.Instance;
|
||||
|
||||
/* The disk storage engine gives you full control on storing files to disk. */
|
||||
diskStorage(options: multer.DiskStorageOptions): multer.StorageEngine;
|
||||
/* The memory storage engine stores the files in memory as Buffer objects. */
|
||||
memoryStorage(): multer.StorageEngine;
|
||||
}
|
||||
|
||||
var multer: Multer;
|
||||
|
||||
export = multer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user