I’m configuring a NestJS controller and currently using any
as the type for the request. Is there a type I can use to make this a bit more typesafe?
@Controller('endpoint')
@UsePipes(new MongoSanitizePipe())
export class RequestController {
constructor(private service: Service) {}
@Post('post')
async onAction(
@Body() body: RequestBody,
@Request() request: any,
): Promise<void> {
return this.service.onAction(body, request.headers);
}
}
What did you try and what were you expecting?
I couldn’t find a type for my HttpRequest.
I expect to properly type it.
The example on NestJs’ site uses the Request
class from express, and so do I in my company’s projects:
import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';
@Controller('cats')
export class CatsController {
@Get()
findAll(@Req() request: Request): string {
return 'This action returns all cats';
}
}
If you’re using fastify, the package includes a FastifyRequest
you can import instead.