Generate SDK from Elysia #1775
Unanswered
gabrielgrs
asked this question in
Q&A
Replies: 1 comment
-
|
Elysia has a built-in Eden Treaty client that gives you end-to-end type safety without needing OpenAPI at all. This is the idiomatic way to consume an Elysia API from TypeScript. Step 1: Export your Elysia app type // server.ts
import { Elysia, t } from "elysia";
const app = new Elysia()
.get("/users", () => getUsers())
.post("/users", ({ body }) => createUser(body), {
body: t.Object({ name: t.String(), email: t.String() }),
})
.listen(3000);
export type App = typeof app;Step 2: Use Eden Treaty on the client import { treaty } from "@elysiajs/eden";
import type { App } from "./server"; // or from your published package
const api = treaty<App>("localhost:3000");
// Fully typed - params, body, response all inferred
const { data, error } = await api.users.get();
const { data: newUser } = await api.users.post({ name: "John", email: "john@example.com" });To publish as an SDK on npm:
// packages/sdk/index.ts
import { treaty } from "@elysiajs/eden";
import type { App } from "../server";
export type { App };
export function createClient(baseUrl: string) {
return treaty<App>(baseUrl);
}
If you really need OpenAPI-based codegen (for non-TypeScript clients), use the But Eden Treaty is the much simpler path if your consumers are all TypeScript. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone!
I'm building an API with Elysia and I need to generate an SDK for it. I've seen several tools that do this using openapi.json, but they all have explicit typing requirements for body, parameters, etc.
Is there a simpler way to generate all of this, using Elysia's own type API to publish on NPM?
Beta Was this translation helpful? Give feedback.
All reactions