fastify-file-interceptor
Version:
This library for Nestjs using FastifyAdapter it rely on library fastify-multer and express multer
57 lines (52 loc) • 1.44 kB
text/typescript
import {
CallHandler,
ExecutionContext,
Inject,
mixin,
NestInterceptor,
Optional,
Type,
} from "@nestjs/common";
import { Observable } from "rxjs";
import FastifyMulter from "fastify-multer";
import { Options, Multer } from "multer";
import { MULTER_MODULE_OPTIONS } from "../constant/multer-module-option";
import { transformException } from "../utils/multer-utils";
type MulterInstance = any;
export function FileFastifyInterceptor(
fieldName: string,
localOptions?: Options
): Type<NestInterceptor> {
class MixinInterceptor implements NestInterceptor {
protected multer: MulterInstance;
constructor(
()
(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.single(fieldName)(
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>;
}