Skip to content

Typed Elysia, Eden and OpenAPI

Typed Elysia plugin + Eden client (flagsElysia)

Section titled “Typed Elysia plugin + Eden client (flagsElysia)”

flagsPanel mounts a web-standard handler — simplest, but opaque to Eden (the typed client can’t see the routes). For a fully typed Eden client, use flagsElysia, which declares the admin routes on a typed Elysia plugin (handlers delegate to the same pipeline, so auth/validation/logic are reused):

import { Elysia } from "elysia";
import { flagsElysia } from "@xtandard/flags/elysia";
import { createRedisStorage } from "@xtandard/flags/storage/redis";
const app = new Elysia()
.use(
flagsElysia({
prefix: "/flags",
sourceStorage: createRedisStorage({ url: process.env.REDIS_URL! }),
}),
)
.listen(3000);
export type App = typeof app;
// client side — fully typed paths/methods/params:
import { treaty } from "@elysiajs/eden";
import type { App } from "./server";
const client = treaty<App>("localhost:3000");
await client.flags.config.get();
const env = client.flags.api
.projects({ projectKey: "default" })
.environments({ environmentKey: "production" });
await env.flags.get();
await env.publish.post({ message: "ship it" });

Every adapter exposes the admin API as an OpenAPI 3.1 document:

const panel = flagsPanel({ sourceStorage }); // or flagsElysia(...), hono, express
panel.openapi(); // → OpenAPI 3.1 object

It’s also served at {basePath}/api/openapi.json. Merge it into your host app’s docs — e.g. with Elysia’s @elysiajs/openapi references, mirroring how better-auth integrates its schema:

import { openapi } from "@elysiajs/openapi";
const flags = flagsElysia({ prefix: "/flags", sourceStorage });
app.use(openapi({ references: flags.openapi() as never })).use(flags);

buildOpenApiDocument({ basePath, title }) is also exported from @xtandard/flags for generating the spec without a running server.

Elysia 2 (currently elysia@rc) changed two things that matter to the typed plugin: routes take (path, hook, handler) instead of (path, handler, hook), and the Elysia class generics changed — so a plugin’s .d.ts built against one major doesn’t type-check (or feed Eden) on the other. @xtandard/flags/elysia2 is the same surface — flagsPanel and flagsElysia — built and typed against Elysia 2:

import { Elysia } from "elysia"; // 2.x
import { flagsElysia } from "@xtandard/flags/elysia2";
const app = new Elysia().use(flagsElysia({ prefix: "/flags", sourceStorage })).listen(3000);
export type App = typeof app;
import { treaty } from "@elysia/eden"; // Elysia 2's Eden (was @elysiajs/eden)
const client = treaty<App>("localhost:3000");
await client.flags!.api.projects.get();
Elysia Import from Eden client
1.x @xtandard/flags/elysia @elysiajs/eden
2.x @xtandard/flags/elysia2 @elysia/eden

flagsPanel (the .mount() handler) is version-agnostic and works from either import; only the typed flagsElysia plugin is tied to a major.

Elysia 2’s .wrap() wraps the whole fetch pipeline (mounted handlers included), which makes it the place to set OpenFeature’s transaction context — handlers then evaluate flags without passing a context object around:

import {
AsyncLocalStorageTransactionContextPropagator,
OpenFeature,
} from "@openfeature/server-sdk";
OpenFeature.setTransactionContextPropagator(new AsyncLocalStorageTransactionContextPropagator());
new Elysia()
.wrap(
(fetch) => (request) =>
// setTransactionContext returns void — capture the response via a Promise.
new Promise<Response>((resolve, reject) =>
OpenFeature.setTransactionContext({ targetingKey: userIdFrom(request) }, () =>
Promise.resolve(fetch(request)).then(resolve, reject),
),
),
)
.get("/", () => client.getBooleanValue("new-dashboard", false)); // no context arg

See examples/elysia2.