diff --git a/packages/nest/src/implement.test.ts b/packages/nest/src/implement.test.ts index 317e04f07..68842deeb 100644 --- a/packages/nest/src/implement.test.ts +++ b/packages/nest/src/implement.test.ts @@ -9,6 +9,7 @@ import { FastifyAdapter } from '@nestjs/platform-fastify' import { Test } from '@nestjs/testing' import { oc, ORPCError } from '@orpc/contract' import { implement, lazy } from '@orpc/server' +import * as StandardServerFastify from '@orpc/standard-server-fastify' import * as StandardServerNode from '@orpc/standard-server-node' import supertest from 'supertest' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -16,6 +17,7 @@ import * as z from 'zod' import { Implement } from './implement' import { ORPCModule } from './module' +const sendStandardFastifyResponseSpy = vi.spyOn(StandardServerFastify, 'sendStandardResponse') const sendStandardResponseSpy = vi.spyOn(StandardServerNode, 'sendStandardResponse') beforeEach(() => { @@ -311,6 +313,8 @@ describe('@Implement', async () => { }) it('partial working on fastify', async () => { + const capturedFastifyLogs: string[] = [] + @Controller() class FastifyController { @Implement(contract.ping) @@ -324,7 +328,16 @@ describe('@Implement', async () => { controllers: [FastifyController], }).compile() - const app = moduleRef.createNestApplication(new FastifyAdapter()) + const app = moduleRef.createNestApplication(new FastifyAdapter({ + logger: { + level: 'info', + stream: { + write: (message: string) => { + capturedFastifyLogs.push(message) + }, + }, + }, + })) await app.init() await app.getHttpAdapter().getInstance().ready() @@ -357,6 +370,8 @@ describe('@Implement', async () => { expect(req).toBeDefined() expect(req!.method).toEqual('POST') expect(req!.url).toEqual('/ping?param=value¶m2[]=value2¶m2[]=value3') + expect(sendStandardFastifyResponseSpy).not.toHaveBeenCalled() + expect(capturedFastifyLogs.join('')).not.toContain('Reply was already sent') }) it('should pass correct signal and lastEventId', async () => { diff --git a/packages/nest/src/implement.ts b/packages/nest/src/implement.ts index 55ed6f2a9..66b46e713 100644 --- a/packages/nest/src/implement.ts +++ b/packages/nest/src/implement.ts @@ -163,8 +163,10 @@ export class ImplementInterceptor implements NestInterceptor { }) if (result.matched) { + const sendResponseInterceptors = toArray(this.config.sendResponseInterceptors) + return intercept( - toArray(this.config.sendResponseInterceptors), + sendResponseInterceptors, { request: req, response: res, standardResponse: result.response }, async ({ response, standardResponse }) => { if (isHono) { @@ -172,6 +174,18 @@ export class ImplementInterceptor implements NestInterceptor { return (response as HonoContext).newResponse(fetchResponse.body, fetchResponse) } else if (isFastify) { + if (sendResponseInterceptors.length === 0) { + const headers = { ...standardResponse.headers } + const body = StandardServerNode.toNodeHttpBody(standardResponse.body, headers, this.config) + const fastifyReply = response as FastifyReply + + fastifyReply + .status(standardResponse.status) + .headers(StandardServerNode.toNodeHttpHeaders(headers)) + + return body + } + await StandardServerFastify.sendStandardResponse(response as FastifyReply, standardResponse, this.config) } else {