diff --git a/playground/app.vue b/playground/app.vue index 9111440..63de883 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -1,7 +1,7 @@ diff --git a/playground/server/trpc/index.ts b/playground/server/trpc/index.ts deleted file mode 100644 index c943b6a..0000000 --- a/playground/server/trpc/index.ts +++ /dev/null @@ -1,35 +0,0 @@ -import * as trpc from '@trpc/server' -import { z } from 'zod' -import type { CompatibilityEvent } from 'h3' - -export const router = trpc - .router() - .query('getUser', { - input: z.object({ name: z.string().min(5) }), - async resolve(req) { - return { id: 1, name: req.input.name } - }, - }) - .query('hello', { - resolve: () => 'world', - }) - -// optional -export const createContext = (event: CompatibilityEvent) => { - return { - user: { - id: 69, - name: 'robert', - }, - } -} - -// optional -export const responseMeta = () => { - // ... - return { - // { headers: ... } - } -} - -export type Router = typeof router diff --git a/src/runtime/api.ts b/src/runtime/api.ts index 0fd1ca9..99fe867 100644 --- a/src/runtime/api.ts +++ b/src/runtime/api.ts @@ -11,6 +11,7 @@ import { createURL } from 'ufo' import type { CompatibilityEvent } from 'h3' import { defineEventHandler, isMethod, useBody } from 'h3' import type { TRPCResponse } from '@trpc/server/dist/declarations/src/rpc' +import type { OnErrorFunction } from '@trpc/server/dist/declarations/src/internals/OnErrorFunction' type MaybePromise = T | Promise @@ -28,17 +29,18 @@ export function createTRPCHandler({ router, createContext, responseMeta, + onError, }: { router: Router createContext?: CreateContextFn responseMeta?: ResponseMetaFn + onError?: OnErrorFunction }) { - const url = '/trpc' - return defineEventHandler(async (event) => { const { req, res, + context, } = event const $url = createURL(req.url) @@ -51,9 +53,15 @@ export function createTRPCHandler({ body: isMethod(event, 'GET') ? null : await useBody(event), query: $url.searchParams, }, - path: $url.pathname.substring(url.length + 1), + path: context.params.path, createContext: async () => createContext?.(event), responseMeta, + onError: (o) => { + onError?.({ + ...o, + req, + }) + }, }) const { status, headers, body } = httpResponse