2023-02-04 22:37:15 +08:00
|
|
|
import type { AnyRouter } from '@trpc/server'
|
2023-02-12 13:44:06 +08:00
|
|
|
import type { FetchHandlerRequestOptions} from '@trpc/server/adapters/fetch'
|
2023-02-04 22:37:15 +08:00
|
|
|
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'
|
|
|
|
import type { MiddlewareHandler } from 'hono'
|
|
|
|
|
2023-02-12 13:44:06 +08:00
|
|
|
type tRPCOptions =
|
|
|
|
Omit<
|
|
|
|
FetchHandlerRequestOptions<AnyRouter>,
|
|
|
|
'req' | 'endpoint'
|
|
|
|
> & Partial<Pick<FetchHandlerRequestOptions<AnyRouter>, 'endpoint'>>
|
2023-02-04 22:37:15 +08:00
|
|
|
|
2023-02-12 13:44:06 +08:00
|
|
|
export const trpcServer = ({ endpoint = '/trpc', ...rest }: tRPCOptions): MiddlewareHandler => {
|
2023-02-04 22:37:15 +08:00
|
|
|
return async (c) => {
|
|
|
|
const res = fetchRequestHandler({
|
2023-02-12 13:44:06 +08:00
|
|
|
...rest,
|
|
|
|
endpoint,
|
2023-02-04 22:37:15 +08:00
|
|
|
req: c.req,
|
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|