mcpField
B
bun-elysia

bun-elysia

github.com/ymc182开发066 次安装41 周前更新
代码仓库

关于 bun-elysia

bun-elysia 是面向 Bun 与 Elysia.js 开发者的参考技能,涵盖从项目搭建到路由、验证和原生 API 使用,并提供 Fastify 迁移指南。

主要功能

  1. 01Bun 与 Elysia.js 项目搭建
  2. 02路由、处理器与请求验证
  3. 03生命周期钩子、插件与 WebSocket
  4. 04Bun 原生数据库及存储 API
  5. 05OpenAPI 文档与 Eden 类型安全客户端
  6. 06Fastify 迁移参考

适用场景

  1. 01创建 Elysia 应用并设置开发服务器
  2. 02为 API 添加路由和请求验证
  3. 03接入 Bun 数据库或 Redis API
  4. 04将 Fastify 功能迁移到 Elysia

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/ymc182/bun-elysia-skill --skill bun-elysia

安装后在 Claude Code 中输入/bun-elysia确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
monorepo.md
references/elysia/
swagger.md
lifecycle.md
core.md
websocket.md
eden.md
validation.md
references/migration/
fastify-advanced.md
fastify-hooks.md
fastify-auth.md
fastify-plugins.md
fastify-basics.md
fastify-validation.md
references/native-apis/
http-server.md
redis.md
s3.md
sql.md

详细介绍

Bun + Elysia.js 开发

使用 Bun 运行时和 Elysia.js 框架构建高性能全栈应用。

快速开始

创建新的 Elysia 项目
bun create elysia my-app
cd my-app
bun run dev
基本服务
import { Elysia, t } from "elysia";

const app = new Elysia()
  .get("/", () => "Hello, World!")
  .get("/users/:id", ({ params }) => ({ id: params.id }), {
    params: t.Object({ id: t.Number() })
  })
  .post("/users", ({ body }) => ({ created: body }), {
    body: t.Object({
      name: t.String(),
      email: t.String({ format: "email" })
    })
  })
  .listen(3000);
使用 Bun 原生 API
import { Elysia } from "elysia";
import { sql } from "bun";
import { redis } from "bun";

const app = new Elysia()
  .decorate("sql", sql)
  .decorate("redis", redis)
  .get("/users", async ({ sql }) => {
    return sql`SELECT * FROM users`;
  })
  .get("/cache/:key", async ({ redis, params }) => {
    return redis.get(params.key);
  })
  .listen(3000);

参考导航

你想做什么?

设置项目:

使用 Bun 原生 API:

使用 Elysia 构建:

从 Fastify 迁移:


常见模式

身份验证中间件
import { Elysia } from "elysia";
import { jwt } from "@elysiajs/jwt";

const app = new Elysia()
  .use(jwt({ secret: process.env.JWT_SECRET! }))
  .derive(async ({ headers, jwt }) => {
    const token = headers.authorization?.replace("Bearer ", "");
    const user = token ? await jwt.verify(token) : null;
    return { user };
  })
  .get("/profile", ({ user, error }) => {
    if (!user) return error(401);
    return user;
  });
带验证的数据库
import { Elysia, t } from "elysia";
import { sql } from "bun";

const app = new Elysia()
  .post("/users", async ({ body }) => {
    const [user] = await sql`
      INSERT INTO users (name, email)
      VALUES (${body.name}, ${body.email})
      RETURNING *
    `;
    return user;
  }, {
    body: t.Object({
      name: t.String({ minLength: 1 }),
      email: t.String({ format: "email" })
    })
  });
带守卫的分组路由
app.group("/api", (app) =>
  app
    .guard({
      headers: t.Object({
        authorization: t.String()
      }),
      beforeHandle: ({ headers, error }) => {
        if (!isValidToken(headers.authorization)) {
          return error(401);
        }
      }
    }, (app) =>
      app
        .get("/users", getUsers)
        .post("/users", createUser)
    )
);
WebSocket 聊天
app.ws("/chat", {
  body: t.Object({
    room: t.String(),
    message: t.String()
  }),
  open(ws) {
    ws.subscribe("general");
  },
  message(ws, { room, message }) {
    ws.publish(room, { from: ws.id, message });
  }
});

关键差异:Fastify 与 Elysia

FastifyElysia
request.params{ params }
request.query{ query }
request.body{ body }
reply.code(n).send(x)set.status = n; return x
reply.header(k, v)set.headers[k] = v
JSON SchemaTypeBox (t.*)
preHandler hookbeforeHandle
fastify.decorate().decorate()
fastify.register(plugin).use(plugin)

安装

# Elysia and plugins
bun add elysia @elysiajs/swagger @elysiajs/jwt @elysiajs/cors @elysiajs/static @elysiajs/eden

# Types
bun add -d @types/bun typescript

评论

支持 Markdown