App Router

Creating a tRPC app router

Utils

Create a server/trpc/utils.ts file with the following content:

import { initTRPC } from '@trpc/server'
import type { IContext } from './context'

export const t = initTRPC.context<IContext>().create()

export const router = t.router
export const procedure = t.procedure

Context

Create a server/trpc/context.ts file with the following content:

import type { inferAsyncReturnType } from '@trpc/server'
import { createSolidAPIHandlerContext } from '@solid-mediakit/trpc/handler'

export const createContextInner = async (
  opts: createSolidAPIHandlerContext
) => {
  return {
    ...opts,
  }
}

export const createContext = async (opts: createSolidAPIHandlerContext) => {
  return await createContextInner(opts)
}

export type IContext = inferAsyncReturnType<typeof createContext>

Router

Example Router

Create a server/trpc/router/example.ts file with the following content:

import { z } from 'zod'
import { procedure, router } from '../utils'

export default router({
  hello: procedure.input(z.object({ name: z.string() })).query(({ input }) => {
    return `Hello ${input.name}`
  }),
  random: procedure
    .input(z.object({ num: z.number() }))
    .mutation(({ input }) => {
      return Math.floor(Math.random() * 100) / input.num
    }),
})

Main App Router

The actual app router that will includes all of the other routers, like example, create a server/trpc/router/_app.ts file with the following content:

import { router } from '../utils'
import example from './example'

export const appRouter = router({
  example,
})

export type IAppRouter = typeof appRouter

Now go ahead an setup and API handler here