tRPC
Simple adapter tRPC for nhttp.
Install
install external libs @nhttp/trpc
.
- Deno
- deno.land
- npm
- Bun
- Yarn
- pnpm
deno add @nhttp/trpc
import {...} from "https://deno.land/x/nhttp/lib/trpc.ts";
npx jsr add @nhttp/trpc
bunx jsr add @nhttp/trpc
yarn dlx jsr add @nhttp/trpc
pnpm dlx jsr add @nhttp/trpc
Usage
import nhttp, { RequestEvent } from "@nhttp/nhttp";
import adapter from "@nhttp/trpc";
import { initTRPC } from "npm:@trpc/server";
// tRPC router
const t = initTRPC.context<RequestEvent>().create();
const proc = t.procedure;
const router = t.router;
interface User {
id: number;
name: string;
}
const userList: User[] = [
{
id: 1,
name: "John",
},
];
const appRouter = router({
userById: proc
.input((val: unknown) => {
if (typeof val === "number") return val;
throw new Error(`Invalid input: ${typeof val}`);
})
.query(({ input }) => {
const user = userList.find((it) => it.id === input);
return user;
}),
});
// share type to client
export type AppRouter = typeof appRouter;
const app = nhttp();
// add adapter to middleware
app.use("/trpc", adapter({ router: appRouter }));
app.listen(8000, (_err, info) => {
console.log(`Running on port ${info.port}`);
});
More info about tRPC => https://trpc.io