UNPKG

fastify-file-interceptor

Version:

This library for Nestjs using FastifyAdapter it rely on library fastify-multer and express multer

58 lines (52 loc) 1.47 kB
import { CallHandler, ExecutionContext, Inject, mixin, NestInterceptor, Optional, Type, } from "@nestjs/common"; import { Observable } from "rxjs"; import FastifyMulter from "fastify-multer"; import { Options, Multer, Field } from "multer"; import { MULTER_MODULE_OPTIONS } from "../constant/multer-module-option"; import { transformException } from "../utils/multer-utils"; type MulterInstance = any; export function FileFieldsFastifyInterceptor( fields: ReadonlyArray<Field>, localOptions?: Options ): Type<NestInterceptor> { class MixinInterceptor implements NestInterceptor { protected multer: MulterInstance; constructor( @Optional() @Inject(MULTER_MODULE_OPTIONS) options: Multer ) { this.multer = (FastifyMulter as any)({ ...options, ...localOptions }); } async intercept( context: ExecutionContext, next: CallHandler ): Promise<Observable<any>> { const ctx = context.switchToHttp(); await new Promise<void>((resolve, reject) => this.multer.fields(fields)( ctx.getRequest(), ctx.getResponse(), (err: any) => { if (err) { const error = transformException(err); return reject(error); } resolve(); } ) ); return next.handle(); } } const Interceptor = mixin(MixinInterceptor); return Interceptor as Type<NestInterceptor>; }