Member-only story

NestJS: Unwrapping the Magic with Top 10 Utility Classes

Mertcan Arguç
3 min readOct 12, 2023

https://nestjs.com/

Ever felt like coding needs some magic? NestJS is waving its wand! Here are ten of its most enchanting tools ready to sprinkle some magic on your code.

1. Graceful Oopsies with HttpExceptionFilter

Errors can be drama queens, always seeking attention. But HttpExceptionFilter ensures they make a classy appearance, not a clumsy entrance.

import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';

@Catch(HttpException)
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const status = exception.getStatus();

response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: ctx.getRequest().url,
});
}
}
// "Drama alert! But I've got the spotlight controls."

2. Pipes: The App’s VIP Bouncers

Pipes are the guardians at the gate, ensuring that the data walks the talk.

import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';

@Controller('items')
export class ItemsController {
@Get(':id')
findOne(@Param('id', new ParseIntPipe())…

No responses yet

Write a response