🌐Web Development

ElysiaJS

Elysia

A TypeScript web framework based on Bun. End-to-end type safety.

πŸ“–Detailed Explanation

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.

πŸ’»Code Example

// 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 νƒ€μž…μœΌλ‘œ 좔둠됨

πŸ—£οΈPractical Conversation Examples

πŸ’ΌIn a Tech Stack Selection Meeting

"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."

🎯In a Technical Interview

"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."

πŸ”In a Code Review

"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

⚠️Cautions

πŸ”—Related Terms

πŸ“šLearn More