Skip to content

Conversation

@gianlazz
Copy link

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, unknown, FastifySchema, FastifyTypeProviderDefault, unknown>'.ts(2339)

PR Checklist

Please check if your PR fulfills the following requirements:

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
  • 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
  • No

Other information

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)
@gianlazz
Copy link
Author

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,
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant