Krzysztof Kaczyński
Krzysztof Kaczyński

Reputation: 5071

Is there anyway to get types interfaces for request, response in Nest.js with Fastify

I am learing Nest.js and on the beging of documentation I read that I can use it not only with express but also with fastify so I setuped up my first project with fastify then I started to read about controllers and I found a problem. For example if I want to get more information about user request i can slightly use @Req req: Reguest and this req is type of Request and it is very easy to get this interface from express based application, yuo only have to install @types/express and then you can inport Request interface from express but how(if it is possible) I can get Request interface if I am using fastify?

Upvotes: 7

Views: 19678

Answers (3)

Maksym B.
Maksym B.

Reputation: 81

Install the fastify package and import FastifyRequest and FastifyReply from there:

import { Controller, Get, Req, Res } from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';

@Controller('feature')
export class FeatureController {
  @Get()
  async handler(
    @Req() req: FastifyRequest,
    @Res() reply: FastifyReply,
  ) { }
}

Upvotes: 2

Krzysztof Kaczyński
Krzysztof Kaczyński

Reputation: 5071

So I lay down that types for fasify are already inside Nest projects because they are coming from @types/node. If you want to use interfaces fro fastify just import them from fastify module. Example:

import { Controller, Get, Query, Req } from '@nestjs/common';
import { AppService } from './app.service';
import { DefaultQuery } from 'fastify';

@Controller('math')
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get('add')
  addTwoNumbers(@Query() query: DefaultQuery): number {
    return this.appService.addTwoNumbers(query.value);
  }
}

If you want to read more about types in fastify visit this link: Fastify Types

Upvotes: 5

Jay McDoniel
Jay McDoniel

Reputation: 70412

There should be types from @types/fastify that you can install. I believe Fastify uses Request and Reply as Request and Response.

Upvotes: 0

Related Questions