update server routes in playground

This commit is contained in:
Robert Soriano
2022-10-28 15:27:50 -07:00
parent 000ac03295
commit 95f5478cdc

View File

@@ -1,7 +1,7 @@
import * as trpc from '@trpc/server' import { initTRPC } from '@trpc/server'
import type { inferAsyncReturnType } from '@trpc/server'
import { z } from 'zod' import { z } from 'zod'
import type { H3Event } from 'h3' import type { H3Event } from 'h3'
import type { inferAsyncReturnType } from '@trpc/server'
const baseURL = 'https://jsonplaceholder.typicode.com' const baseURL = 'https://jsonplaceholder.typicode.com'
@@ -14,28 +14,34 @@ const TodoShape = z.object({
export type Todo = z.infer<typeof TodoShape> export type Todo = z.infer<typeof TodoShape>
export const router = trpc.router<Context>() const t = initTRPC.context<Context>().create()
.query('getTodos', {
async resolve() { // We explicitly export the methods we use here
return await $fetch<Todo[]>(`${baseURL}/todos`) // This allows us to create reusable & protected base procedures
}, export const middleware = t.middleware
}) export const router = t.router
.query('getTodo', { export const publicProcedure = t.procedure
input: z.number(),
async resolve(req) { export const appRouter = router({
return await $fetch<Todo>(`${baseURL}/todos/${req.input}`) getTodos: publicProcedure.query(() => {
}, return $fetch<Todo[]>(`${baseURL}/todos`)
}) }),
.mutation('addTodo', { getTodo: publicProcedure
input: TodoShape, .input(z.number())
async resolve(req) { .query((req) => {
console.log(req.input) return $fetch<Todo>(`${baseURL}/todos/${req.input}`)
return await $fetch<Todo>(`${baseURL}/todos`, { }),
addTodo: publicProcedure
.input(TodoShape)
.mutation((req) => {
return $fetch<Todo>(`${baseURL}/todos`, {
method: 'POST', method: 'POST',
body: req.input, body: req.input,
}) })
}, }),
}) })
export type AppRouter = typeof appRouter
export async function createContext(event: H3Event) { export async function createContext(event: H3Event) {
// Create your context based on the request object // Create your context based on the request object
@@ -50,4 +56,4 @@ export async function createContext(event: H3Event) {
} }
} }
type Context = inferAsyncReturnType<typeof createContext> type Context = inferAsyncReturnType<typeof createContext>