rainbowatcher / h3-trpc

h3 integration with trpc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GitHub Workflow Status GitHub License GitHub package.json version

h3-trpc

h3 integration with trpc.

Usage

  1. Install
pnpm i -D h3-trpc
  1. Import
// server.ts
import { createServer } from "node:http"
import { createH3Middleware } from "h3-trpc"
import { initTRPC } from "@trpc/server"
import * as h3 from "h3"

const app = createApp()
const t = initTRPC.create()

const appRouter = t.router({
  hello: t.procedure
      .input(z.string())
      .query(opts => `hello ${opts.input}`),
})

export type AppRouter = typeof appRouter

const app = h3.createApp()
app.use("/trpc", createH3Middleware({ router: appRouter }))
// You can set the route path to "/" or simply leave it unset
// e.g. app.use(createH3Middleware({ router: appRouter }))
createServer(h3.toNodeListener(app)).listen(3000)
import { createTRPCProxyClient, httpBatchLink } from "@trpc/client"

client = createTRPCProxyClient<AppRouter>({
    links: [
        httpBatchLink({
            // The base path of the request URL depends on how the route path is configured.
            url: "http://localhost:3000/trpc",
        }),
    ],
})
const resp = await client.hello.query("world")

License

MIT © Made by ❤️

About

h3 integration with trpc

License:MIT License


Languages

Language:TypeScript 83.8%Language:JavaScript 14.6%Language:Shell 1.6%