NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
Installations
npm install @student-coin/nestjs-form-data
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.7.0
NPM Version
8.15.0
Score
64.1
Supply Chain
94.7
Quality
73.8
Maintenance
25
Vulnerability
99.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
Download Statistics
Total Downloads
2,402
Last Day
1
Last Week
2
Last Month
10
Last Year
498
GitHub Statistics
MIT License
19 Commits
1 Branches
Updated on Jun 07, 2023
Bundle Size
731.31 kB
Minified
230.99 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.5.1
Package Id
@student-coin/nestjs-form-data@1.5.1
Unpacked Size
64.98 kB
Size
16.13 kB
File Count
95
NPM Version
8.15.0
Node Version
18.7.0
Total Downloads
Cumulative downloads
Total Downloads
2,402
Last Day
0%
1
Compared to previous day
Last Week
100%
2
Compared to previous week
Last Month
-61.5%
10
Compared to previous month
Last Year
160.7%
498
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
5
Description
nestjs-form-data is a NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
- Process files and strings, serialize form-data to object
- Process files in nested objects
- Integration with class-validator, validate files with validator decorator
nestjs-form-data serializes the form-data request into an object and places it in the body of the request.
The files in the request are transformed into objects.
Standard file storage types:
- Memory storage
- File system storage
Installation
1$ npm install nestjs-form-data
Add the module to your application
1@Module({ 2 imports: [ 3 NestjsFormDataModule, 4 ], 5}) 6export class AppModule { 7}
Usage
Apply @FormDataRequest()
decorator to your controller method
1@Controller() 2export class NestjsFormDataController { 3 4 5 @Post('load') 6 @FormDataRequest() 7 getHello(@Body() testDto: FormDataTestDto): void { 8 console.log(testDto); 9 } 10}
If you are using class-validator describe dto and specify validation rules
1export class FormDataTestDto { 2 3 @IsFile() 4 @MaxFileSize(1e6) 5 @HasMimeType(['image/jpeg', 'image/png']) 6 avatar: MemoryStoredFile; 7 8}
Configuration
Static configuration
You can set the global configuration when connecting the module using the NestjsFormDataModule.config
method:
1@Module({ 2 imports: [ 3 NestjsFormDataModule.config({ storage: MemoryStoredFile }), 4 ], 5 controllers: [], 6 providers: [], 7}) 8export class AppModule { 9}
Async configuration
Quite often you might want to asynchronously pass your module options instead of passing them beforehand.
In such case, use configAsync()
method, that provides a couple of various ways to deal with async data.
1. Use factory
1NestjsFormDataModule.configAsync({ 2 useFactory: () => ({ 3 storage: MemoryStoredFile 4 }) 5});
Our factory behaves like every other one (might be async and is able to inject dependencies through inject).
1NestjsFormDataModule.configAsync({ 2 imports: [ConfigModule], 3 useFactory: async (configService: ConfigService) => ({ 4 storage: MemoryStoredFile, 5 limits: { 6 files: configService.get<number>('files'), 7 } 8 }), 9 inject: [ConfigService], 10});
2. Use class
1NestjsFormDataModule.configAsync({
2 useClass: MyNestJsFormDataConfigService
3});
Above construction will instantiate MyNestJsFormDataConfigService
inside NestjsFormDataModule
and will leverage it
to create options object.
1export class MyNestJsFormDataConfigService implements NestjsFormDataConfigFactory { 2 configAsync(): Promise<FormDataInterceptorConfig> | FormDataInterceptorConfig { 3 return { 4 storage: FileSystemStoredFile, 5 fileSystemStoragePath: '/tmp/nestjs-fd', 6 }; 7 } 8}
3. Use existing
1NestjsFormDataModule.configAsync({ 2 imports: [MyNestJsFormDataConfigModule], 3 useExisting: MyNestJsFormDataConfigService 4});
It works the same as useClass with one critical difference - NestjsFormDataModule
will lookup imported modules to
reuse already created MyNestJsFormDataConfigService
, instead of instantiating it on its own.
Method level configuration
Or pass the config object while using the decorator on the method
1@Controller() 2export class NestjsFormDataController { 3 4 5 @Post('load') 6 @FormDataRequest({storage: MemoryStoredFile}) 7 getHello(@Body() testDto: FormDataTestDto): void { 8 console.log(testDto); 9 } 10}
Configuration fields
storage
- The type of storage logic for the uploaded file (Default MemoryStoredFile)fileSystemStoragePath
- The path to the directory for storing temporary files, used only forstorage: FileSystemStoredFile
(Default: /tmp/nestjs-tmp-storage)autoDeleteFile
- Automatically delete files after the request ends (Default true)limits
- busboy limits configuration. Constraints in this declaration are handled at the serialization stage, so using these parameters is preferable for performance.
File storage types
Memory storage
MemoryStoredFile
The file is loaded into RAM, files with this storage type are very fast but not suitable for processing large files.
File system storage
FileSystemStoredFile
The file is loaded into a temporary directory (see configuration) and is available during the processing of the request. The file is automatically deleted after the request finishes
Custom storage types
You can define a custom type of file storage, for this, inherit your class from StoredFile
, see examples in the storage directory
Validation
By default, several validators are available with which you can check the file
@IsFile()
- Checks if the value is an uploaded file
@IsFiles()
- Checks an array of files
@MaxFileSize()
- Maximum allowed file size
@MinFileSize()
- Minimum allowed file size
@HasMimeType()
- Check the mime type of the file
If you need to validate an array of files for size or otherwise, use each: true
property from ValidationOptions
Examples
FileSystemStoredFile storage configuration
Controller
1import { FileSystemStoredFile, FormDataRequest } from 'nestjs-form-data'; 2 3@Controller() 4export class NestjsFormDataController { 5 6 7 @Post('load') 8 @FormDataRequest({storage: FileSystemStoredFile}) 9 getHello(@Body() testDto: FormDataTestDto): void { 10 console.log(testDto); 11 } 12}
DTO
1import { FileSystemStoredFile, HasMimeType, IsFile, MaxFileSize } from 'nestjs-form-data'; 2 3 4export class FormDataTestDto { 5 6 @IsFile() 7 @MaxFileSize(1e6) 8 @HasMimeType(['image/jpeg', 'image/png']) 9 avatar: FileSystemStoredFile; 10 11}
Validate the array of file
1import { FileSystemStoredFile, HasMimeType, IsFiles, MaxFileSize } from 'nestjs-form-data'; 2 3export class FormDataTestDto { 4 5 @IsFiles() 6 @MaxFileSize(1e6, { each: true }) 7 @HasMimeType(['image/jpeg', 'image/png'], { each: true }) 8 avatars: FileSystemStoredFile[]; 9 10}
License

No vulnerabilities found.

No security vulnerabilities found.