
Security News
Static vs. Runtime Reachability: Insights from Latio’s On the Record Podcast
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
nestjs-formdata-interceptor
Advanced tools
Supply Chain Security
Vulnerability
Quality
Maintenance
License
AI-detected possible typosquat
Supply chain riskThere is a package with a similar name that is downloaded much more often.
Did you mean |
---|
nestjs-form-data |
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
nestjs-formdata-interceptor
is a powerful library for NestJS that provides seamless interception and handling of multipart/form-data requests. This functionality is particularly beneficial for efficiently managing file uploads in your application.
To install nestjs-formdata-interceptor
using npm:
npm install nestjs-formdata-interceptor
To use nestjs-formdata-interceptor
, import it into the main directory of your NestJS application and configure it as shown below:
import { NestFactory } from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express";
import { AppModule } from "./app.module";
import {
FormdataInterceptor,
DefaultFileSaver,
} from "nestjs-formdata-interceptor";
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useGlobalInterceptors(
new FormdataInterceptor({
customFileName(context, originalFileName) {
return `${Date.now()}-${originalFileName}`;
},
fileSaver: new DefaultFileSaver({
prefixDirectory: "./public",
customDirectory(context, originalDirectory) {
return originalDirectory;
},
}),
})
);
await app.listen(3000);
}
bootstrap();
need to install @fastify/multipart package.
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import {
FastifyAdapter,
NestFastifyApplication,
} from "@nestjs/platform-fastify";
import fastifyMultipart from "@fastify/multipart";
import {
DefaultFileSaver,
FormdataInterceptor,
} from "nestjs-formdata-interceptor";
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
app.register(fastifyMultipart);
app.useGlobalInterceptors(
new FormdataInterceptor({
customFileName(context, originalFileName) {
return `${Date.now()}-${originalFileName}`;
},
fileSaver: new DefaultFileSaver({
prefixDirectory: "./public",
customDirectory(context, originalDirectory) {
return originalDirectory;
},
}),
})
);
await app.listen(3000);
}
bootstrap();
The customFileName
function allows you to generate custom file names for each uploaded file. In the example above, the file name is prefixed with the current timestamp followed by the original file name.
The DefaultFileSaver
is used to define the directory where the files will be saved.
prefixDirectory
specifies the root directory where all files will be saved.customDirectory
allows you to specify a custom sub-directory within the root directory. By default, it uses the original directory provided.If you need custom file-saving logic, implement the IFileSaver interface. Here's an example:
import { FileData, IFileSaver } from "nestjs-formdata-interceptor";
import { ExecutionContext } from "@nestjs/common";
export class CustomFileSaver implements IFileSaver {
public save(
fileData: FileData,
context: ExecutionContext
): string | Promise<string> {
// do your file save logic
// and return the string value of your file url
}
}
Then, use your custom file saver in the interceptor configuration:
import { NestFactory } from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express";
import { AppModule } from "./app.module";
import { FormdataInterceptor } from "nestjs-formdata-interceptor";
import { CustomFileSaver } from "path-to-your-file-saver";
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useGlobalInterceptors(
new FormdataInterceptor({
customFileName(context, originalFileName) {
return `${Date.now()}-${originalFileName}`;
},
fileSaver: new CustomFileSaver(),
})
);
await app.listen(3000);
}
bootstrap();
If you are using class-validator
describe dto and specify validation rules
import { IsArray, IsNotEmpty } from "class-validator";
import {
FileData,
HasMimeType,
IsFileData,
MaxFileSize,
MimeType,
MinFileSize,
} from "nestjs-formdata-interceptor";
export class CreateDto {
@IsArray()
@IsNotEmpty()
@IsFileData({ each: true })
@HasMimeType([MimeType["video/mp4"], "image/png"], { each: true })
@MinFileSize(2000000, { each: true })
@MaxFileSize(4000000, { each: true })
// array file
files: FileData[];
@IsFileData()
@IsNotEmpty()
@HasMimeType([MimeType["video/mp4"], "image/png"])
@MinFileSize(2000000)
@MaxFileSize(4000000)
// single file
file: FileData;
}
Define your controller to handle file uploads:
import { Body, Controller, Post } from "@nestjs/common";
import { AppService } from "./app.service";
import { CreateDto } from "./dto/create.dto";
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Post()
getHello(@Body() createDto: CreateDto) {
// save single file
createDto.file.save();
// save multiple file
createDto.files.map((file) => file.save());
}
}
With this setup, nestjs-formdata-interceptor
will manage multipart/form-data requests efficiently, allowing for structured handling of file uploads in your NestJS application.
FAQs
nest js formdata interceptor
The npm package nestjs-formdata-interceptor receives a total of 135 weekly downloads. As such, nestjs-formdata-interceptor popularity was classified as not popular.
We found that nestjs-formdata-interceptor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.