ElysiaJS
Elysia
A TypeScript web framework based on Bun. End-to-end type safety.
Elysia
A TypeScript web framework based on Bun. End-to-end type safety.
ElysiaJS is a TypeScript web framework optimized for the Bun runtime. Although it is a new framework released in 2023, it is growing rapidly thanks to its end-to-end type safety and excellent performance. Benchmark results show it to be 18 times faster than Express.js and 3 times faster than Fastify.
Its core feature is a client generator called 'Eden Treaty'. Based on the API schema defined on the server, it automatically generates a client SDK in which types are fully inferred. OpenAPI/Swagger documentation is also generated automatically without separate configuration, which greatly reduces the burden of API documentation.
ElysiaJS has a powerful plugin system. There are various official plugins, such as JWT authentication, CORS, static file serving, WebSocket, and GraphQL, and when plugins are chained, type inference is extended along with them. This is similar to Express middleware, but far superior in terms of type safety.
However, it is tied to the Bun runtime, so it cannot be used in Node.js environments, and its ecosystem is smaller than those of Express or Fastify. There are also not yet many production-proven cases, so it is recommended to start with a new project or a personal project.
// ElysiaJS μλ² λ° Eden Treaty ν΄λΌμ΄μΈνΈ
import { Elysia, t } from 'elysia';
import { swagger } from '@elysiajs/swagger';
import { jwt } from '@elysiajs/jwt';
// μλ² μ μ
const app = new Elysia()
.use(swagger())
.use(jwt({ name: 'jwt', secret: process.env.JWT_SECRET! }))
// μ¬μ©μ λΌμ°νΈ κ·Έλ£Ή
.group('/users', (app) =>
app
.post('/signup', async ({ body, jwt }) => {
const token = await jwt.sign({ email: body.email });
return { token };
}, {
body: t.Object({
email: t.String({ format: 'email' }),
password: t.String({ minLength: 8 })
}),
response: t.Object({ token: t.String() })
})
.get('/:id', ({ params }) => {
return { id: params.id, name: 'User Name' };
}, {
params: t.Object({ id: t.Numeric() }),
response: t.Object({ id: t.Number(), name: t.String() })
})
)
.listen(3000);
// νμ
λ΄λ³΄λ΄κΈ° (ν΄λΌμ΄μΈνΈμμ μ¬μ©)
export type App = typeof app;
// βββββββββββββββββββββββββββββββββββββββββ
// ν΄λΌμ΄μΈνΈ (Eden Treaty)
import { treaty } from '@elysiajs/eden';
import type { App } from './server';
const client = treaty<App>('localhost:3000');
// μλ²½ν νμ
μΆλ‘ !
const { data, error } = await client.users.signup.post({
email: 'user@example.com',
password: 'securepassword123'
});
// data.token β string νμ
μΌλ‘ μΆλ‘ λ¨
const user = await client.users({ id: 1 }).get();
// user.data.name β string νμ
μΌλ‘ μΆλ‘ λ¨
"Shall we try ElysiaJS for the new microservice? Bun 1.0 has only been out for a short while, but the performance is really good, and with Eden Treaty the frontend type integration is automatic, so you can use it like tRPC. There are risks, but it's worth trying out on a small service first."
"Interviewer: What are the advantages of ElysiaJS?
Candidate: There are three. First, because it is Bun-native, it can handle 2.7 million requests per second by benchmark. Second, with Eden Treaty, server types are automatically propagated all the way to the client, so API contract mismatches are caught at compile time. Third, OpenAPI documentation is generated automatically."
"When defining t.Object in the body schema, please wrap optional fields with t.Optional(). That way, the client will also infer those fields as optional. Right now they are being picked up as required, so the frontend always has to supply a value."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free