UNPKG

@proventuslabs/nestjs-multipart-form

Version:

A lightweight and efficient NestJS package for handling multipart form data and file uploads with RxJS streaming support and type safety.

33 lines (32 loc) 1.49 kB
import { type CallHandler, type ExecutionContext } from "@nestjs/common"; import { type Observable } from "rxjs"; import type { MultipartOptions } from "../core/types"; /** * Creates a NestJS HTTP interceptor that parses multipart/form-data requests using RxJS streams. * * This interceptor: * - Attaches `_files$` and `_fields$` observables to the request object for use with decorators * - Ensures proper cleanup of file streams after the request is handled * - Only applies to HTTP requests; other contexts are passed through unchanged * - Supports both global (module-level) and local (interceptor-level) configuration * * @param localOptions Optional configuration for the multipart parser that overrides global options * @returns A NestJS interceptor class that can be applied via `@UseInterceptors` * * @example * ‍@Post('upload') * ‍@UseInterceptors(MultipartInterceptor({ limits: { files: 5 } })) * upload( * ‍@MultipartFiles() files$: Observable<MultipartFileStream>, * ‍@MultipartFields() fields$: Observable<MultipartField> * ) { * // Process files and fields as RxJS streams * return merge(files$, fields$).pipe(toArray()); * } */ export declare function MultipartInterceptor(localOptions?: MultipartOptions): { new (globalOptions?: MultipartOptions | undefined): { readonly globalOptions?: MultipartOptions | undefined; intercept(ctx: ExecutionContext, next: CallHandler<unknown>): Observable<unknown>; }; };