UNPKG

1.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.FileInterceptor = void 0;
4const tslib_1 = require("tslib");
5const common_1 = require("@nestjs/common");
6const multer = require("multer");
7const files_constants_1 = require("../files.constants");
8const multer_utils_1 = require("../multer/multer.utils");
9/**
10 * @param fieldName
11 * @param localOptions
12 *
13 * @publicApi
14 */
15function FileInterceptor(fieldName, localOptions) {
16 let MixinInterceptor = class MixinInterceptor {
17 constructor(options = {}) {
18 this.multer = multer({
19 ...options,
20 ...localOptions,
21 });
22 }
23 async intercept(context, next) {
24 const ctx = context.switchToHttp();
25 await new Promise((resolve, reject) => this.multer.single(fieldName)(ctx.getRequest(), ctx.getResponse(), (err) => {
26 if (err) {
27 const error = (0, multer_utils_1.transformException)(err);
28 return reject(error);
29 }
30 resolve();
31 }));
32 return next.handle();
33 }
34 };
35 MixinInterceptor = tslib_1.__decorate([
36 tslib_1.__param(0, (0, common_1.Optional)()),
37 tslib_1.__param(0, (0, common_1.Inject)(files_constants_1.MULTER_MODULE_OPTIONS)),
38 tslib_1.__metadata("design:paramtypes", [Object])
39 ], MixinInterceptor);
40 const Interceptor = (0, common_1.mixin)(MixinInterceptor);
41 return Interceptor;
42}
43exports.FileInterceptor = FileInterceptor;