import { CallHandler, ExecutionContext, mixin, NestInterceptor, Type, } from '@nestjs/common'; import { finalize } from 'rxjs'; import { Observable } from 'rxjs/internal/Observable'; import { FileProcessResult } from '../multipart/handlers/base-handler'; import { transformUploadOptions, UploadOptions } from '../multipart/options'; import { getMultipartRequest } from '../multipart/request'; export type HandlerFunction = ( req: ReturnType, options: UploadOptions, ) => Promise; export function createInterceptor( rawOptions: UploadOptions, handlerFn: HandlerFunction, resultProcessor: ( req: ReturnType, result: T, ) => void, ): Type { class MixinInterceptor implements NestInterceptor { private readonly options: UploadOptions; constructor() { this.options = transformUploadOptions(rawOptions); } async intercept( context: ExecutionContext, next: CallHandler, ): Promise> { const ctx = context.switchToHttp(); const req = getMultipartRequest(ctx); if (!req.header('content-type')?.startsWith('multipart/form-data')) { return next.handle(); } const result = await handlerFn(req, this.options); resultProcessor(req, result); return next.handle().pipe(finalize(result.remove)); } } return mixin(MixinInterceptor); }