diff --git a/playground/server/api/trpc/[trpc].ts b/playground/server/api/trpc/[trpc].ts index 63cbabd..c8eb4d6 100644 --- a/playground/server/api/trpc/[trpc].ts +++ b/playground/server/api/trpc/[trpc].ts @@ -1,12 +1,23 @@ import { createNuxtApiHandler } from 'trpc-nuxt' import { appRouter } from '../../trpc/routers' +import { createContext } from '~~/server/trpc/context' export default createNuxtApiHandler({ router: appRouter, - createContext: async () => { - return {} - }, + /** + * @link https://trpc.io/docs/context + */ + createContext, onError({ error }) { - console.log('Error', error) + if (error.code === 'INTERNAL_SERVER_ERROR') { + // send to bug reporting + console.error('Something went wrong', error) + } }, + /** + * @link https://trpc.io/docs/caching#api-response-caching + */ + // responseMeta() { + // // ... + // }, }) diff --git a/playground/server/trpc/context.ts b/playground/server/trpc/context.ts new file mode 100644 index 0000000..9543a9f --- /dev/null +++ b/playground/server/trpc/context.ts @@ -0,0 +1,17 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import type { inferAsyncReturnType } from '@trpc/server' +import type { H3Event } from 'h3' + +export type Context = inferAsyncReturnType + +/** + * Creates context for an incoming request + * @link https://trpc.io/docs/context + */ +export async function createContext( + opts: H3Event, +) { + // for API-response caching see https://trpc.io/docs/caching + + return {} +} diff --git a/playground/server/trpc/index.ts b/playground/server/trpc/index.ts index 6926262..195a134 100644 --- a/playground/server/trpc/index.ts +++ b/playground/server/trpc/index.ts @@ -1,6 +1,7 @@ import { initTRPC } from '@trpc/server' +import type { Context } from './context' -const t = initTRPC.context().create() +const t = initTRPC.context().create() /** * Create a router