Example: Added a basic example with a product call

This commit is contained in:
Ryan Gannon
2023-06-13 15:00:38 +01:00
parent ad85a78350
commit 3fc261d5e7
15 changed files with 343 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
import { createNuxtApiHandler } from 'trpc-nuxt'
import { appRouter } from '../../trpc/routers'
import { createContext } from '../../trpc/context'
// export API handler
export default createNuxtApiHandler({
router: appRouter,
createContext,
})

View 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>

View 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

View 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

View File

@@ -0,0 +1,3 @@
{
"extends": "../.nuxt/tsconfig.server.json"
}