@dawsonjdv/elysia-trpc
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

@lobomfz/elysia-trpc

A plugin for elysia that adds support for using tRPC, integrated with trpc-docgen.

Notes:

  • The OpenAPI router removes tRPC's weird body input, so it accepts exactly what the procedure schema expects.
  • This requires a patched version of tRPC, see this PR.

Installation

bun add @lobomfz/elysia-trpc

Example

import { initTRPC } from "@trpc/server";
import { generateTrpcDocs, type OpenApiMeta } from "@lobomfz/trpc-docgen";
import { Type } from "@sinclair/typebox";
import Elysia from "elysia";
import { trpc } from "../src";

// this meta optional, but highly recommended
const t = initTRPC.meta<OpenApiMeta>().create();

const router = t.router;

const appRouter = router({
	createDate: t.procedure
		.input(
			Type.Object({
				date: Type.String(),
			}),
		)
		.mutation(({ input }) => ({
			date: input.date,
		})),
});

You can integrate with trpc-docgen to serve your restful OpenAPI router

const { mappings } = await generateTrpcDocs(appRouter, {
	baseUrl: "http://localhost:3000",
	title: "My API",
	version: "1.0.0",
});

new Elysia()
	.use(
		trpc(appRouter, {
			endpoint: "/trpc",
		}),
	)
	.use(
		trpc(appRouter, {
			endpoint: "/openapi",
			openApi: {
				// in this example, createDate will be mapped to a POST /trpc/date
				mappings,
				trpcEndpoint: "/trpc",
			},
		}),
	).listen(3000);

Readme

Keywords

Package Sidebar

Install

npm i @dawsonjdv/elysia-trpc

Weekly Downloads

2

Version

1.1.7

License

MIT

Unpacked Size

43.2 kB

Total Files

16

Last publish

Collaborators

  • dawsonjdv04