Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/nest/src/implement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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'
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(() => {
Expand Down Expand Up @@ -311,6 +313,8 @@ describe('@Implement', async () => {
})

it('partial working on fastify', async () => {
const capturedFastifyLogs: string[] = []

@Controller()
class FastifyController {
@Implement(contract.ping)
Expand All @@ -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()

Expand Down Expand Up @@ -357,6 +370,8 @@ describe('@Implement', async () => {
expect(req).toBeDefined()
expect(req!.method).toEqual('POST')
expect(req!.url).toEqual('/ping?param=value&param2[]=value2&param2[]=value3')
expect(sendStandardFastifyResponseSpy).not.toHaveBeenCalled()
expect(capturedFastifyLogs.join('')).not.toContain('Reply was already sent')
})

it('should pass correct signal and lastEventId', async () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/nest/src/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,29 @@ 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) {
const fetchResponse = StandardServerFetch.toFetchResponse(standardResponse, this.config)
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 {
Expand Down