docs.nestjs.com icon indicating copy to clipboard operation
docs.nestjs.com copied to clipboard

Import '@fastify/view' for Fastify reply type augmentation

Open gianlazz opened this issue 2 months ago • 1 comments

Added import statement for '@fastify/view' to enhance Fastify reply type with view property to avoid the type error below:

Property 'view' does not exist on type 'FastifyReply<RouteGenericInterface, RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>'.ts(2339)

PR Checklist

Please check if your PR fulfills the following requirements:

  • [ ] The commit message follows our guidelines: https://github.com/nestjs/docs.nestjs.com/blob/master/CONTRIBUTING.md

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [ ] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [x] Docs
  • [ ] Other... Please describe:

What is the current behavior?

Calling reply.view() results in the folling error if import '@fastify/view'; is not called in main.ts

Property 'view' does not exist on type 'FastifyReply<RouteGenericInterface, RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>'.ts(2339)

Issue Number: N/A

What is the new behavior?

view property is found on reply object.

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

gianlazz avatar Oct 24 '25 05:10 gianlazz

Hmm, actually, I'm still getting response.view is not a defined... maybe this isn't a documentation issue. I'm encountering this in my exception filter.

import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';
import '@fastify/view';

@Catch()
export class ErrorViewFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<FastifyReply>();
    const request = ctx.getRequest<FastifyRequest>();

    const status =
      exception instanceof HttpException
        ? exception.getStatus()
        : HttpStatus.INTERNAL_SERVER_ERROR;

    const message =
      exception instanceof HttpException
        ? exception.getResponse()
        : 'Internal server error';

    // Render with layout
    response.view('/error.hbs', {
      layout: 'layout',
      statusCode: status,
      message: typeof message === 'string' ? message : (message as any).message,
      timestamp: new Date().toISOString(),
      path: request.url,
    });
  }
}

gianlazz avatar Oct 24 '25 05:10 gianlazz