@@ -15,11 +15,19 @@ import { getRouter, isProcedure, unlazy } from '@orpc/server'
1515import { StandardHandler } from '@orpc/server/standard'
1616import { get , intercept , toArray } from '@orpc/shared'
1717import * as StandardServerFastify from '@orpc/standard-server-fastify'
18+ import * as StandardServerFetch from '@orpc/standard-server-fetch'
1819import * as StandardServerNode from '@orpc/standard-server-node'
1920import { mergeMap } from 'rxjs'
2021import { ORPC_MODULE_CONFIG_SYMBOL } from './module'
2122import { toNestPattern } from './utils'
2223
24+ interface HonoContext {
25+ req : { raw : globalThis . Request }
26+ res : globalThis . Response
27+ finalized : boolean
28+ newResponse : ( body : BodyInit | null , init ?: ResponseInit ) => globalThis . Response
29+ }
30+
2331const MethodDecoratorMap = {
2432 HEAD : Head ,
2533 GET : Get ,
@@ -122,12 +130,17 @@ export class ImplementInterceptor implements NestInterceptor {
122130 ` )
123131 }
124132
125- const req : Request | FastifyRequest = ctx . switchToHttp ( ) . getRequest ( )
126- const res : Response | FastifyReply = ctx . switchToHttp ( ) . getResponse ( )
133+ const req : Request | FastifyRequest | HonoContext [ 'req' ] = ctx . switchToHttp ( ) . getRequest ( )
134+ const res : Response | FastifyReply | HonoContext = ctx . switchToHttp ( ) . getResponse ( )
127135
128- const standardRequest = 'raw' in req
129- ? StandardServerFastify . toStandardLazyRequest ( req , res as FastifyReply )
130- : StandardServerNode . toStandardLazyRequest ( req , res as Response )
136+ const isHono = 'finalized' in res && typeof ( res as HonoContext ) . newResponse === 'function'
137+ const isFastify = 'raw' in req && ! isHono
138+
139+ const standardRequest = isHono
140+ ? StandardServerFetch . toStandardLazyRequest ( ( req as HonoContext [ 'req' ] ) . raw )
141+ : isFastify
142+ ? StandardServerFastify . toStandardLazyRequest ( req as FastifyRequest , res as FastifyReply )
143+ : StandardServerNode . toStandardLazyRequest ( req as Request , res as Response )
131144
132145 const handler = new StandardHandler ( procedure , {
133146 init : ( ) => { } ,
@@ -148,11 +161,14 @@ export class ImplementInterceptor implements NestInterceptor {
148161 toArray ( this . config . sendResponseInterceptors ) ,
149162 { request : req , response : res , standardResponse : result . response } ,
150163 async ( { response, standardResponse } ) => {
151- if ( 'raw' in response ) {
152- await StandardServerFastify . sendStandardResponse ( response , standardResponse , this . config )
164+ if ( isHono ) {
165+ ( response as HonoContext ) . res = StandardServerFetch . toFetchResponse ( standardResponse , this . config )
166+ }
167+ else if ( isFastify ) {
168+ await StandardServerFastify . sendStandardResponse ( response as FastifyReply , standardResponse , this . config )
153169 }
154170 else {
155- await StandardServerNode . sendStandardResponse ( response , standardResponse , this . config )
171+ await StandardServerNode . sendStandardResponse ( response as Response , standardResponse , this . config )
156172 }
157173 } ,
158174 )
0 commit comments