mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-14 20:19:33 +01:00
Example: Added a basic example with a product call
This commit is contained in:
9
examples/basic-example/server/trpc/context.ts
Normal file
9
examples/basic-example/server/trpc/context.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
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>
|
||||
27
examples/basic-example/server/trpc/routers/index.ts
Normal file
27
examples/basic-example/server/trpc/routers/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { z } from 'zod'
|
||||
import { publicProcedure, router } from '../trpc'
|
||||
|
||||
export const appRouter = router({
|
||||
hello: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
text: z.string().nullish(),
|
||||
})
|
||||
)
|
||||
.query(({ input }) => {
|
||||
console.log(input)
|
||||
return {
|
||||
greeting: `hello ${input?.text ?? 'world'}`,
|
||||
}
|
||||
}),
|
||||
products: publicProcedure.query(async () => {
|
||||
return {
|
||||
title: 'TRPC Nuxt',
|
||||
price: 12.0,
|
||||
description: 'Check out the trpc-nuxt repo',
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
// export type definition of API
|
||||
export type AppRouter = typeof appRouter
|
||||
20
examples/basic-example/server/trpc/trpc.ts
Normal file
20
examples/basic-example/server/trpc/trpc.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* This is your entry point to setup the root configuration for tRPC on the server.
|
||||
* - `initTRPC` should only be used once per app.
|
||||
* - We export only the functionality that we use so we can enforce which base procedures should be used
|
||||
*
|
||||
* Learn how to create protected base procedures and other things below:
|
||||
* @see https://trpc.io/docs/v10/router
|
||||
* @see https://trpc.io/docs/v10/procedures
|
||||
*/
|
||||
import { initTRPC } from '@trpc/server'
|
||||
import { Context } from '../trpc/context'
|
||||
|
||||
const t = initTRPC.context<Context>().create()
|
||||
|
||||
/**
|
||||
* Unprotected procedure
|
||||
**/
|
||||
export const publicProcedure = t.procedure
|
||||
export const router = t.router
|
||||
export const middleware = t.middleware
|
||||
Reference in New Issue
Block a user