@@ -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,21 @@ 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 = ( ( ) => {
140+ if ( isHono ) {
141+ return StandardServerFetch . toStandardLazyRequest ( ( req as HonoContext [ 'req' ] ) . raw )
142+ }
143+ if ( isFastify ) {
144+ return StandardServerFastify . toStandardLazyRequest ( req as FastifyRequest , res as FastifyReply )
145+ }
146+ return StandardServerNode . toStandardLazyRequest ( req as Request , res as Response )
147+ } ) ( )
131148
132149 const handler = new StandardHandler ( procedure , {
133150 init : ( ) => { } ,
@@ -148,11 +165,14 @@ export class ImplementInterceptor implements NestInterceptor {
148165 toArray ( this . config . sendResponseInterceptors ) ,
149166 { request : req , response : res , standardResponse : result . response } ,
150167 async ( { response, standardResponse } ) => {
151- if ( 'raw' in response ) {
152- await StandardServerFastify . sendStandardResponse ( response , standardResponse , this . config )
168+ if ( isHono ) {
169+ ( response as HonoContext ) . res = StandardServerFetch . toFetchResponse ( standardResponse , this . config )
170+ }
171+ else if ( isFastify ) {
172+ await StandardServerFastify . sendStandardResponse ( response as FastifyReply , standardResponse , this . config )
153173 }
154174 else {
155- await StandardServerNode . sendStandardResponse ( response , standardResponse , this . config )
175+ await StandardServerNode . sendStandardResponse ( response as Response , standardResponse , this . config )
156176 }
157177 } ,
158178 )
0 commit comments