tRPCOptions extends `FetchHandlerRequestOptions`-ish (#42)

* tRPCOptions extends `FetchHandlerRequestOptions`-ish

* nix spaces

* add changeset
pull/43/head
Alex Errant 2023-02-11 23:44:06 -06:00 committed by GitHub
parent 0efd95e5a9
commit 520a453195
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/trpc-server': patch
---
`trpcServer` options extends FetchHandlerRequestOptions

View File

@ -1,18 +1,20 @@
import type { AnyRouter } from '@trpc/server' import type { AnyRouter } from '@trpc/server'
import type { FetchHandlerRequestOptions} from '@trpc/server/adapters/fetch'
import { fetchRequestHandler } from '@trpc/server/adapters/fetch' import { fetchRequestHandler } from '@trpc/server/adapters/fetch'
import type { MiddlewareHandler } from 'hono' import type { MiddlewareHandler } from 'hono'
type tRPCOptions = { type tRPCOptions =
endpoint?: string Omit<
router: AnyRouter FetchHandlerRequestOptions<AnyRouter>,
} 'req' | 'endpoint'
> & Partial<Pick<FetchHandlerRequestOptions<AnyRouter>, 'endpoint'>>
export const trpcServer = ({ router, endpoint = '/trpc' }: tRPCOptions): MiddlewareHandler => { export const trpcServer = ({ endpoint = '/trpc', ...rest }: tRPCOptions): MiddlewareHandler => {
return async (c) => { return async (c) => {
const res = fetchRequestHandler({ const res = fetchRequestHandler({
endpoint: endpoint, ...rest,
endpoint,
req: c.req, req: c.req,
router: router,
}) })
return res return res
} }