Skip to content

Commit c3068b4

Browse files
authored
feat(server): improve hono compatibility (#119)
1 parent ad0709a commit c3068b4

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

packages/server/src/adapters/hono/middleware.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,23 @@ export type CreateMiddlewareRest<T extends Context> =
1414

1515
export function createMiddleware<T extends Context>(handler: FetchHandler<T>, ...[options]: CreateMiddlewareRest<T>): MiddlewareHandler {
1616
return async (c, next) => {
17+
const bodyProps = new Set(['arrayBuffer', 'blob', 'formData', 'json', 'text'] as const)
18+
type BodyProp = typeof bodyProps extends Set<infer T> ? T : never
19+
20+
const request = c.req.method === 'GET' || c.req.method === 'HEAD'
21+
? c.req.raw
22+
: new Proxy(c.req.raw, { // https://github.com/honojs/middleware/blob/main/packages/trpc-server/src/index.ts#L39
23+
get(target, prop) {
24+
if (bodyProps.has(prop as any)) {
25+
return () => c.req[prop as BodyProp]()
26+
}
27+
return Reflect.get(target, prop, target)
28+
},
29+
})
30+
1731
const context = await value(options?.context ?? {}, c) as any
1832

19-
const { matched, response } = await handler.handle(c.req.raw, { ...options, context })
33+
const { matched, response } = await handler.handle(request, { ...options, context })
2034

2135
if (matched) {
2236
return c.body(response.body, response)

0 commit comments

Comments
 (0)