mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 12:14:40 +01:00
@@ -36,11 +36,12 @@ Initialize your tRPC backend using the `initTRPC` function and create your first
|
|||||||
|
|
||||||
```ts [server/trpc/trpc.ts]
|
```ts [server/trpc/trpc.ts]
|
||||||
import { initTRPC } from '@trpc/server'
|
import { initTRPC } from '@trpc/server'
|
||||||
|
import { Context } from '@/server/trpc/context'
|
||||||
|
|
||||||
// Avoid exporting the entire t-object since it's not very
|
// Avoid exporting the entire t-object since it's not very
|
||||||
// descriptive and can be confusing to newcomers used to t
|
// descriptive and can be confusing to newcomers used to t
|
||||||
// meaning translation in i18n libraries.
|
// meaning translation in i18n libraries.
|
||||||
const t = initTRPC.create()
|
const t = initTRPC.context<Context>().create()
|
||||||
|
|
||||||
// Base router and procedure helpers
|
// Base router and procedure helpers
|
||||||
export const router = t.router
|
export const router = t.router
|
||||||
@@ -72,14 +73,27 @@ export type AppRouter = typeof appRouter
|
|||||||
```ts [server/api/trpc/[trpc].ts]
|
```ts [server/api/trpc/[trpc].ts]
|
||||||
import { createNuxtApiHandler } from 'trpc-nuxt'
|
import { createNuxtApiHandler } from 'trpc-nuxt'
|
||||||
import { appRouter } from '@/server/trpc/routers'
|
import { appRouter } from '@/server/trpc/routers'
|
||||||
|
import { createContext } from '@/server/trpc/context'
|
||||||
|
|
||||||
// export API handler
|
// export API handler
|
||||||
export default createNuxtApiHandler({
|
export default createNuxtApiHandler({
|
||||||
router: appRouter,
|
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}
|
::alert{type=info}
|
||||||
|
|||||||
Reference in New Issue
Block a user