mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 20:19:33 +01:00
docs: Infer Context type
This commit is contained in:
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user