docs: Infer Context type

This commit is contained in:
Alex Kozack
2022-12-12 15:56:53 +02:00
parent 01c2bd5701
commit 4ff57dfb97

View File

@@ -36,11 +36,12 @@ Initialize your tRPC backend using the `initTRPC` function and create your first
```ts [server/trpc/trpc.ts]
import { initTRPC } from '@trpc/server'
import { Context } from '@/server/trpc/context'
// Avoid exporting the entire t-object since it's not very
// descriptive and can be confusing to newcomers used to t
// meaning translation in i18n libraries.
const t = initTRPC.create()
const t = initTRPC.context<Context>().create()
// Base router and procedure helpers
export const router = t.router
@@ -72,14 +73,27 @@ export type AppRouter = typeof appRouter
```ts [server/api/trpc/[trpc].ts]
import { createNuxtApiHandler } from 'trpc-nuxt'
import { appRouter } from '@/server/trpc/routers'
import { createContext } from '@/server/trpc/context'
// export API handler
export default createNuxtApiHandler({
router: appRouter,
createContext: () => ({}),
createContext,
})
```
```ts [server/trpc/context.ts]
import { inferAsyncReturnType } from '@trpc/server'
/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export const createContext = () => {}
export type Context = inferAsyncReturnType<typeof createContext>;
```
::
::alert{type=info}