Merge pull request #16924 from mxl/multer-files

multer - Request.files can be an array
This commit is contained in:
Daniel Rosenwasser
2017-06-12 00:14:31 -07:00
committed by GitHub
4 changed files with 36 additions and 37 deletions

View File

@@ -1,9 +1,12 @@
// Type definitions for multer
// Type definitions for multer 1.3
// Project: https://github.com/expressjs/multer
// Definitions by: jt000 <https://github.com/jt000>, vilicvane <https://vilic.github.io/>, David Broder-Rodgers <https://github.com/DavidBR-SW>
// Definitions by: jt000 <https://github.com/jt000>
// vilicvane <https://vilic.github.io/>
// David Broder-Rodgers <https://github.com/DavidBR-SW>
// Michael Ledin <https://github.com/mxl>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import express = require('express');
import * as express from 'express';
declare namespace multer {
interface Field {
@@ -18,7 +21,9 @@ declare namespace multer {
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 */
/** 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) */
fieldNameSize?: number;
@@ -39,7 +44,7 @@ declare namespace multer {
};
/** 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;
fileFilter?(req: Express.Request, file: Express.Multer.File, callback: (error: Error, acceptFile: boolean) => void): void;
}
interface StorageEngine {
@@ -51,27 +56,23 @@ declare namespace multer {
/** A function used to determine within which folder the uploaded files should be stored. Defaults to the system's default temporary directory. */
destination?: string | ((req: Express.Request, file: Express.Multer.File, callback: (error: Error | null, 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 | null, filename: string) => void) => void;
filename?(req: Express.Request, file: Express.Multer.File, callback: (error: Error | null, filename: string) => void): void;
}
interface Instance {
/** In case you need to handle a text-only multipart form, you can use any of the multer methods (.single(), .array(), fields()), req.body contains the text fields */
single(): express.RequestHandler;
/** Accept a single file with the name fieldname. The single file will be stored in req.file. */
single(fieldame: string): express.RequestHandler;
/** In case you need to handle a text-only multipart form, you can use any of the multer methods (.single(), .array(), fields()), req.body contains the text fields */
array(): 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 single file with the name fieldName. The single file will be stored in req.file. */
single(fieldName?: 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(fieldName: 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;
/** In case you need to handle a text-only multipart form, you can use any of the multer methods (.single(), .array(), fields()), req.body contains the text fields */
/** 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. */
@@ -80,21 +81,21 @@ interface Multer {
memoryStorage(): multer.StorageEngine;
}
declare var multer: Multer;
declare const multer: Multer;
export = multer;
declare global {
namespace Express {
export interface Request {
interface Request {
file: Multer.File;
files: {
[fieldname: string]: Multer.File[];
};
} | Multer.File[];
}
namespace Multer {
export interface File {
interface File {
/** Field name specified in the form */
fieldname: string;
/** Name of the file on the user's computer */

View File

@@ -1,33 +1,30 @@
import * as express from 'express';
import * as multer from 'multer';
const upload = multer({ dest: 'uploads/' });
const app = express();
import express = require('express');
import multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var app = express();
app.post('/profile', upload.single('avatar'), (req, res, next) => {
app.post('/profile', upload.single('avatar'), (req: express.Request, res: express.Response, next: express.NextFunction) => {
});
app.post('/photos/upload', upload.array('photos', 12), (req, res, next) => {
app.post('/photos/upload', upload.array('photos', 12), (req: express.Request, res: express.Response, next: express.NextFunction) => {
});
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, (req, res, next) => {
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]);
app.post('/cool-profile', cpUpload, (req: express.Request, res: express.Response, next: express.NextFunction) => {
});
var diskStorage = multer.diskStorage({
const 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 });
const diskUpload = multer({ storage: diskStorage });
var memoryStorage = multer.memoryStorage();
var memoryUpload = multer({ storage: memoryStorage });
const memoryStorage = multer.memoryStorage();
const memoryUpload = multer({ storage: memoryStorage });

View File

@@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
@@ -19,4 +19,4 @@
"index.d.ts",
"multer-tests.ts"
]
}
}

1
types/multer/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }