Home › Bun › Elysia on Bun Windsurf rules

Elysia on Bun Windsurf rules

Windsurf rules for Elysia APIs on Bun: schema validation with t, plugins and groups, typed errors, Eden Treaty clients, lifecycle hooks, and bun test.

WindsurfBun

How to use

Save this as .windsurf/rules/elysia.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.windsurf/rules/elysia.mdDownload  
---
trigger: glob
globs: src/**/*.ts
---

# Elysia (Bun)

## Commands
`bun run dev` (`bun --watch src/index.ts`) · `bun test` · `bunx tsc --noEmit` · build with `bun build` only if the repo does.

## Structure
- `src/index.ts` creates the app and `.listen()`s; features are Elysia plugins in `src/modules/<feature>/index.ts` (`new Elysia({ prefix: '/orders', name: 'orders' })`) mounted with `.use()`. Business logic lives in plain service functions, not inside handlers.
- Keep method chaining intact: Elysia infers types through the chain, so register routes on the same instance rather than mutating it in separate statements.
- Export `type App = typeof app` for Eden Treaty clients; frontend code calls the API through `treaty<App>(url)` instead of hand-written fetch wrappers.

## Validation and errors
- Every route declares schemas with `t` (`body`, `query`, `params`, `headers`, and `response` for each status). Don't hand-validate inside handlers.
- Reuse models via `.model({ 'order.create': t.Object({...}) })` and reference them by name.
- Throw with `error(status, body)` (or `status()` in newer versions, per the installed version's docs) for expected failures; handle unexpected ones in a single `.onError` that logs and returns a safe message without stack traces.

## Hooks and plugins
- Auth via a plugin using `.derive()`/`.resolve()` to attach the user, and `beforeHandle` guards on protected groups. Scope hooks deliberately (`{ as: 'scoped' }` or `'global'`); the default is local to the instance.
- Use official plugins (`@elysiajs/cors`, `@elysiajs/swagger`/openapi, `@elysiajs/jwt`) configured explicitly; restrict CORS origins.
- Read config with `Bun.env` validated at startup; secrets never hardcoded.
- Database access through the repo's client created once and provided with `.decorate()`.

## Tests
`bun test` calling `app.handle(new Request('http://localhost/orders', {...}))` or an Eden Treaty client against the app instance, covering validation failures and auth.

More: all Bun rules · all Windsurf rules

Related rules

Bun runtime CLAUDE.md

CLAUDE.md for TypeScript projects on Bun 1.2+: bun test, Bun.serve, built-in SQLite/Postgres, and bundling.

Claude Code (CLAUDE.md)Bun

Django Windsurf rules

Windsurf rules for Django 5.2 LTS projects: apps, models and migrations, ORM performance, forms, and tests.

WindsurfDjango

FastAPI Windsurf rules

Windsurf rules for FastAPI apps with Pydantic v2, SQLAlchemy 2.0 async, dependency injection, and pytest.

WindsurfPython / FastAPI

Go concurrency Windsurf rules

Windsurf rules for safe Go concurrency: goroutine lifetimes, context cancellation, errgroup, channels, and the race detector.

WindsurfGo

Go Fiber web API Windsurf rules

Windsurf rules for Go REST APIs on Fiber: fasthttp gotchas, handler layout, validation, middleware, graceful shutdown and app.Test.

WindsurfGo

Go + Gin web API Windsurf rules

Windsurf rules for Go 1.27 REST APIs built with Gin: handlers, binding/validation, middleware, and tests.

WindsurfGo

Get the best new AI prompts every week

One short email a week: the best new prompts and rules for ChatGPT, Claude, Gemini, Midjourney, Cursor and more, plus what changed in the tools. Free, unsubscribe anytime.

Finishes on our newsletter page at beehiiv (opens in a new tab). Or subscribe directly.