/
multerOptions.ts
62 lines (59 loc) · 1.72 KB
/
multerOptions.ts
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
import {getDecoratorType, Store} from "@tsed/core";
import * as Multer from "multer";
import {MultipartFileMiddleware} from "../middlewares/MultipartFileMiddleware";
/**
* Define multer option for all MultipartFile
*
* ```typescript
* import {Controller, Post} from "@tsed/common";
* import {MulterOptions, MultipartFile} from "@tsed/multipartfiles";
* import {Multer} from "@types/multer";
*
* type MulterFile = Express.Multer.File;
*
* @Controller('/')
* class MyCtrl {
* @Post('/file')
* private uploadFile(@MultipartFile("file1") file: MulterFile) {
*
* }
*
* @Post('/file')
* @MulterOptions({dest: "/other-dir"})
* private uploadFile(@MultipartFile("file1") file: MulterFile) {
*
* }
*
* @Post('/file2')
* @MulterOptions({dest: "/other-dir"})
* private uploadFile(@MultipartFile("file1") file: MulterFile, @MultipartFile("file2") file2: MulterFile) {
*
* }
*
* @Post('/files')
* private uploadFile(@MultipartFile("file1") files: MulterFile[]) {
*
* }
* }
* ```
*
* > See the tutorial on the [multer configuration](/tutorials/multer.md).
* @param {multer.Options} options
* @returns {(target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor}
* @decorator
* @multer
*/
export function MulterOptions(options: Multer.Options) {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const type = getDecoratorType([target, propertyKey, descriptor], true);
switch (type) {
default:
throw new Error("MulterOptions is only supported on method");
case "method":
Store.fromMethod(target, propertyKey).merge(MultipartFileMiddleware, {
options
});
return descriptor;
}
};
}