Gathering detailed insights and metrics for @student-coin/nestjs-form-data
Gathering detailed insights and metrics for @student-coin/nestjs-form-data
NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
npm install @student-coin/nestjs-form-data
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
2,401
Last Day
1
Last Week
2
Last Month
10
Last Year
497
MIT License
19 Commits
1 Branches
Updated on Jun 07, 2023
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
-65.5%
10
Compared to previous month
Last Year
153.6%
497
Compared to previous year
5
nestjs-form-data is a NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
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:
1$ npm install nestjs-form-data
Add the module to your application
1@Module({ 2 imports: [ 3 NestjsFormDataModule, 4 ], 5}) 6export class AppModule { 7}
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}
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}
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.
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});
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}
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.
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}
storage
- The type of storage logic for the uploaded file (Default MemoryStoredFile)fileSystemStoragePath
- The path to the directory for storing temporary files, used only for storage: 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.MemoryStoredFile
The file is loaded into RAM, files with this storage type are very fast but not suitable for processing large files.
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
You can define a custom type of file storage, for this, inherit your class from StoredFile
, see examples in the storage directory
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
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}
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}
No vulnerabilities found.
No security vulnerabilities found.