mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 21:10:23 +00:00
[koa__multer] Allows to access the properties file and files through the koa context. (#40034)
* [koa__multer] Allows to access the file nad files through the koa contet. * Extends the koa context and request by the fields file, files. * Removes unecessary import
This commit is contained in:
committed by
Nathan Shively-Sanders
parent
1cb82bdd9f
commit
6d1bed7dac
Vendored
+17
@@ -19,6 +19,11 @@
|
||||
* async function uploadFile(ctx: Koa.Context){
|
||||
* let multerReq = <multer.MulterIncomingMessage>ctx.req;
|
||||
* let files = multerReq.files;
|
||||
*
|
||||
* // You can also directly access the files property from the context object or request object:
|
||||
* files = ctx.files;
|
||||
* files = ctx.request.files;
|
||||
*
|
||||
* let baseFilePath: string = ctx.params.path || '';
|
||||
* //...
|
||||
* }
|
||||
@@ -32,6 +37,18 @@
|
||||
import * as Koa from 'koa';
|
||||
import { IncomingMessage } from 'http';
|
||||
|
||||
declare module 'koa' {
|
||||
interface DefaultContext {
|
||||
file: multer.File;
|
||||
files: multer.File[];
|
||||
}
|
||||
|
||||
interface Request {
|
||||
file: multer.File;
|
||||
files: multer.File[];
|
||||
}
|
||||
}
|
||||
|
||||
declare namespace multer {
|
||||
interface File {
|
||||
/** Field name specified in the form */
|
||||
|
||||
@@ -13,6 +13,12 @@ const app = new Koa();
|
||||
app.use(upload.single('avatar'));
|
||||
|
||||
app.use(upload.array('photos', 12));
|
||||
app.use(async (ctx, next) => {
|
||||
for (const file of ctx.request.files) {
|
||||
console.info(`Received file: ${file.filename}`);
|
||||
}
|
||||
await next();
|
||||
});
|
||||
|
||||
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]);
|
||||
app.use(cpUpload);
|
||||
|
||||
Reference in New Issue
Block a user