Logo vun Elysia Patterns

Elysia Patterns

Communautéit
j4flmao
elysia-patterns

Use this skill when the user says 'elysia pattern', 'elysia eden treaty', 'elysia websocket', 'elysia macro', 'elysia validation', 'elysia file upload', 'elysia custom plugin'. Implements Elysia recipes: Eden Treaty clients, built-in WebSocket pub/sub, parameterized macros, custom plugins with lifecycle hooks, TypeBox validation, uploads, error mapping and DI via decorate. Do NOT use for: overall project structure and lifecycle order (elysia-architecture), Express middleware, non-Elysia frameworks.

Iwwersiicht

Editeurj4flmao
Repositoryagent-skills
Skill-Nummelysia-patterns
Stären
41
Forks
6
Matgeliwwert Fichieren
6
LizenzMIT
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 6 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    Vum j4flmao op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Elysia Patterns an TypingMind, fir e mat all LLM ze benotzen, oder füügt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vun GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier hei drënner an a bestätegt.
    https://github.com/j4flmao/agent-skills/tree/main/skills/backend/elysia/patterns
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill afügen → Vu GitHub-URL, pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an de Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/j4flmao/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/skills/backend/elysia/patterns .claude/skills/elysia-patterns
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Elysia Patterns an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Elysia Patterns automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Elysia Patterns net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Keys.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

Elysia Patterns

Purpose

Implement Elysia.js-specific patterns — Eden Treaty type-safe client, WebSocket with Bun, macro-based guards, custom plugins, and validation patterns.

Agent Protocol

Trigger

User request includes: elysia pattern, elysia eden treaty, elysia websocket, elysia plugin, elysia macro, elysia validation, elysia type safe.

Input Context

  • Elysia version and plugin set
  • Server-side patterns needed (auth, middleware, validation)
  • Client-side (React, Svelte, Vue) for Eden Treaty

Output Artifact

Code examples for the requested patterns — plugin, macro, WebSocket handler, Eden client.

Response Format

Code-first: pattern name, problem, implementation. Compress output.

Completion Criteria

  • Pattern implemented with type safety
  • Schema validation applied
  • Error handling included
  • Test example provided

Max Response Length

4096 tokens

Architecture Decision Trees

Eden Treaty vs tRPC vs REST+OpenAPI

CriterionEden TreatytRPCREST+OpenAPI
BackendElysia onlyTypeScript servers (Node, Bun, Deno, edge adapters)Any
Type safetyFull-stack inferenceFull-stack inferenceGenerated client
SetupSingle typeof appManual procedure defsExternal tooling
File uploadDeclarativeManualMultipart
WebsocketVia pluginVia subscriptionSeparate
FlexibilityElysia-nativetRPC serverAny HTTP

Decision: Elysia backend + any frontend → Eden Treaty. tRPC backend → tRPC fits better. Multi-language clients → REST+OpenAPI.

Macro vs Middleware vs Plugin

PatternScopeUse Case
Macro (.macro(), enabled per route or guard)Route-levelAuth check, permission guard
Middleware (onRequest, beforeHandle)Global or groupLogging, CORS, rate limit
Plugin (new Elysia({name}))Modular featureUser module, auth module

Decision: Reusable route modifier → Macro. Cross-cutting concern → Middleware. Standalone feature → Plugin.

Workflow

Step 1: Eden Treaty Full-Stack Setup

typescript
// server/src/app.ts — EXPORT TYPE
import { Elysia } from 'elysia'
import { userModule } from './modules/users'

export const app = new Elysia()
  .use(userModule)
  .get('/health', () => ({ status: 'ok' }))

export type App = typeof app

// client/src/api.ts
import { treaty } from '@elysiajs/eden' // Eden Treaty 2
import type { App } from '../../server/src/app'

export const client = treaty<App>('localhost:3000', {
  headers: () => ({
    authorization: `Bearer ${localStorage.getItem('token')}`,
  }),
})

// Usage (e.g. inside a data-fetching hook)
export async function loadUser(id: string) {
  const { data, error } = await client.users({ id }).get()
  if (error) throw new Error(`Failed to load user ${id}: HTTP ${error.status}`)
  return data
}

Step 2: WebSocket Pattern

typescript
// src/plugins/realtime.ts — WebSocket support is built into Elysia (Bun's uWebSockets), no plugin needed
import { Elysia, t } from 'elysia'

export const realtimePlugin = new Elysia({ name: 'realtime' })
  .ws('/ws/orders/:orderId', {
    body: t.Object({
      type: t.String(),
      payload: t.Any(),
    }),
    async open(ws) {
      ws.subscribe(`order:${ws.data.params.orderId}`)
      ws.send({ type: 'connected', payload: { orderId: ws.data.params.orderId } })
    },
    async message(ws, message) {
      // Broadcast to all subscribers of this order
      ws.publish(`order:${ws.data.params.orderId}`, message)
    },
    async close(ws) {
      ws.unsubscribe(`order:${ws.data.params.orderId}`)
    },
  })

// Emit from service code (app = the listening Elysia instance)
function notifyOrderStatus(orderId: string, status: string) {
  app.server?.publish(`order:${orderId}`, JSON.stringify({
    type: 'status_update',
    payload: { orderId, status },
  }))
}
// Pub/sub is per process; fan out through Redis/NATS when running several instances.

Step 3: Custom Plugin with Lifecycle

typescript
// src/plugins/audit-log.ts
import { Elysia } from 'elysia'

interface AuditLogConfig {
  excludePaths?: string[]
  db: { save: (entry: AuditEntry) => Promise<void> }
}

interface AuditEntry {
  method: string
  path: string
  userId: string | null
  statusCode: number
  duration: number
  timestamp: Date
}

export const auditLogPlugin = (config: AuditLogConfig) =>
  new Elysia({ name: 'audit-log' })
    .derive({ as: 'global' }, () => ({ startTime: performance.now() }))
    .onAfterResponse({ as: 'global' }, (ctx) => {
      const { request, set, startTime } = ctx
      const url = new URL(request.url)
      if (config.excludePaths?.includes(url.pathname)) return

      const entry: AuditEntry = {
        method: request.method,
        path: url.pathname,
        userId: (ctx as { user?: { id: string } }).user?.id ?? null, // set by auth plugin if present
        statusCode: typeof set.status === 'number' ? set.status : 200,
        duration: performance.now() - startTime,
        timestamp: new Date(),
      }
      config.db.save(entry).catch(console.error)
    })

// Usage
app.use(auditLogPlugin({
  excludePaths: ['/health'],
  db: auditRepository,
}))

Step 4: Guard Macros

typescript
// src/plugins/rate-limit.ts
import { Elysia } from 'elysia'

const requestCounts = new Map<string, { count: number; resetAt: number }>()

// Single-instance demo only: per-process memory, no eviction. Use Redis for real deployments.
export const rateLimitPlugin = new Elysia({ name: 'rate-limit' })
  .macro({
    // parameterized macro: { rateLimit: 100 } on a route or guard
    rateLimit(limit: number) {
      return {
        beforeHandle({ request, server, set }) {
          const ip = server?.requestIP(request)?.address ?? 'unknown'
          const now = Date.now()
          const entry = requestCounts.get(ip)

          if (!entry || now > entry.resetAt) {
            requestCounts.set(ip, { count: 1, resetAt: now + 60_000 })
            return
          }

          entry.count++
          if (entry.count > limit) {
            const retryAfter = Math.ceil((entry.resetAt - now) / 1000)
            set.status = 429
            set.headers['retry-after'] = String(retryAfter)
            return { error: 'Rate limit exceeded', retryAfter }
          }
        },
      }
    },
  })

// Usage
app.guard({ rateLimit: 100 })
  .get('/api/data', () => fetchData())

Step 5: Validation Patterns

typescript
// src/modules/orders/schema.ts
import { t } from 'elysia'

// Composable schema parts
const AddressSchema = t.Object({
  street: t.String(),
  city: t.String(),
  zipCode: t.String({ pattern: '^\\d{5}$' }),
})

const OrderItemSchema = t.Object({
  productId: t.String({ format: 'uuid' }),
  quantity: t.Numeric({ minimum: 1, maximum: 999 }),
  price: t.Numeric({ exclusiveMinimum: 0 }),
})

const PaymentSchema = t.Object({
  method: t.Union([t.Literal('card'), t.Literal('transfer'), t.Literal('cod')]),
  cardToken: t.Optional(t.String()),
})

// Composed schema
export const CreateOrderSchema = t.Object({
  items: t.Array(OrderItemSchema, { minItems: 1 }),
  shippingAddress: AddressSchema,
  payment: PaymentSchema,
  notes: t.Optional(t.String({ maxLength: 500 })),
})

export const OrderResponseSchema = t.Object({
  id: t.String({ format: 'uuid' }),
  orderNumber: t.String(),
  status: t.String(),
  total: t.Number(),
  items: t.Array(t.Composite([OrderItemSchema, t.Object({ id: t.String() })])),
  createdAt: t.String({ format: 'date-time' }),
})

Step 6: Response Transformation

typescript
// src/plugins/response-transform.ts
import { Elysia } from 'elysia'

export const responseTransformPlugin = new Elysia({ name: 'response-transform' })
  .onAfterHandle({ as: 'global' }, ({ response, set }) => {
    if (response && typeof response === 'object' && !('success' in response) && !(response instanceof Response)) {
      return { success: true, data: response }
    }
  })

Step 7: File Upload with Elysia

typescript
import { Elysia, t } from 'elysia'

export const uploadPlugin = new Elysia({ name: 'upload' })
  .post('/api/upload', async ({ body: { file } }) => {
    const buffer = await file.arrayBuffer()
    const filename = `${crypto.randomUUID()}-${file.name}`
    await Bun.write(`./uploads/${filename}`, buffer)
    return { success: true, filename, size: buffer.byteLength }
  }, {
    body: t.Object({
      file: t.File({ maxSize: 10 * 1024 * 1024 }), // 10MB limit
    }),
  })
  .post('/api/upload/multiple', async ({ body: { files } }) => {
    const results = await Promise.all(
      files.map(async (f) => {
        const buf = await f.arrayBuffer()
        const name = `${crypto.randomUUID()}-${f.name}`
        await Bun.write(`./uploads/${name}`, buf)
        return { name, size: buf.byteLength }
      })
    )
    return { success: true, files: results }
  }, {
    body: t.Object({
      files: t.Array(t.File({ maxSize: 5 * 1024 * 1024 }), { maxItems: 10 }),
    }),
  })

Step 8: Error Handling Plugin

typescript
import { Elysia } from 'elysia'

export const errorPlugin = new Elysia({ name: 'error-handler' })
  .onError({ as: 'global' }, ({ error, code, set }) => {
    if (code === 'VALIDATION') {
      set.status = 400
      return { success: false, code: 'VALIDATION_ERROR', details: error.all }
    }
    if (code === 'NOT_FOUND') {
      set.status = 404
      return { success: false, code: 'NOT_FOUND', message: error.message }
    }
    if (code === 'PARSE') {
      set.status = 400
      return { success: false, code: 'PARSE_ERROR', message: 'Invalid request body' }
    }
    console.error('Unhandled error:', error)
    set.status = 500
    return { success: false, code: 'INTERNAL_ERROR', message: 'An unexpected error occurred' }
  })

// Usage
const app = new Elysia()
  .use(errorPlugin)
  .get('/users/:id', ({ params: { id }, error }) => {
    const user = findUser(id)
    if (!user) return error(404, 'User not found')
    return user
  })

Production Considerations

Performance Tuning

typescript
// Elysia production config
const app = new Elysia({
  precompile: true,   // compile routes/validators at startup: slower boot, faster first requests
  strictPath: true,   // /users and /users/ are different routes
})
  .use(cors({ origin: ['https://app.example.com'] }))
  .listen(3000)
  • precompile trades startup time for first-request latency; leave it off for serverless cold starts.
  • normalize (default on) strips properties not in the schema; keep it on so unexpected fields never reach handlers.
  • Response compression: usually done by the reverse proxy/CDN; community plugins exist if needed in-process.
  • Memory-constrained containers: run Bun with --smol.
  • Benchmark your own routes; framework micro-benchmarks rarely reflect database-bound workloads.

Caching

typescript
// Simple in-memory cache plugin (one Map per process, shared by all requests)
const cache = new Map<string, { data: unknown; expires: number }>()

export const cachePlugin = new Elysia({ name: 'cache' })
  .decorate({
    cacheGet: (key: string) => {
      const entry = cache.get(key)
      if (entry && entry.expires > Date.now()) return entry.data
      cache.delete(key)
      return null
    },
    cacheSet: (key: string, data: unknown, ttlMs: number = 60000) => {
      cache.set(key, { data, expires: Date.now() + ttlMs })
    },
  })

Graceful Shutdown

typescript
const app = createApp().listen(port)

process.on('SIGTERM', async () => {
  console.log('Shutting down...')
  await app.stop()   // stop accepting connections, let in-flight requests finish
  await db.close()
  process.exit(0)
})

Implementation Patterns

Pattern: Dependency Injection with Elysia State

typescript
import { Elysia } from 'elysia'

// Define service
class UserService {
  constructor(private db: Database) {}
  async findById(id: string) { return this.db.query('SELECT * FROM users WHERE id = $1', [id]) }
}

// One shared connection pool, injected into services
const db = new Database(process.env.DATABASE_URL!)

// Plugin-scoped DI
const userModule = new Elysia({ name: 'user-module' })
  .decorate('userService', new UserService(db))
  .get('/users/:id', ({ userService, params: { id } }) => userService.findById(id))
  .post('/users', ({ body, userService }) => userService.create(body))

const app = new Elysia().use(userModule)
// `userService` is only available inside userModule routes

Pattern: OpenAPI/Swagger Documentation

typescript
import { Elysia, t } from 'elysia'
import { swagger } from '@elysiajs/swagger'

const app = new Elysia()
  .use(swagger({
    path: '/docs',
    documentation: {
      info: { title: 'My API', version: '1.0.0', description: 'Elysia API documentation' },
      tags: [{ name: 'Users', description: 'User endpoints' }, { name: 'Orders', description: 'Order endpoints' }],
    },
  }))
  .get('/users', () => listUsers(), {
    detail: {
      tags: ['Users'],
      summary: 'List all users',
      parameters: [{ name: 'page', in: 'query', schema: { type: 'number' } }],
      responses: { 200: { description: 'User list' } },
    },
  })

Anti-Patterns

Anti-PatternWhyFix
Global schema reuse across modulesTight couplingEach module owns its schema
Eden Treaty with typeof without exportClient can't infer typesExport App type from app.ts
WebSocket in non-ws pluginUnclear ownershipCreate dedicated realtime plugin
Heavy computation in macrosBlocks request pipelineOffload to service layer
Expecting a plugin's hooks to apply to the parent appHooks are local by defaultSet { as: 'scoped' } or { as: 'global' } explicitly
Using Express middleware directlyIncompatible request modelConvert to Elysia onRequest/beforeHandle
Unnamed plugins used many timesHooks registered repeatedlyGive reusable plugins a name (and seed if configurable) for deduplication
Per-request derive creating caches/clientsNew object per request, no sharingdecorate/state for singletons

Security Considerations

  • CORS is a server concern: configure @elysiajs/cors with explicit origins; Eden does not change CORS behavior
  • WebSocket: validate origin and auth token on connection
  • Guard macros for auth — never trust client-side claims
  • Rate limiting for critical endpoints (login, register), backed by a shared store when running multiple instances
  • Content Security Policy via response headers

Testing Strategies

Plugin Testing

typescript
import { describe, expect, test } from 'bun:test'
import { Elysia } from 'elysia'
import { rateLimitPlugin } from './rate-limit'

test('rate limit blocks after threshold', async () => {
  const app = new Elysia()
    .use(rateLimitPlugin)
    .guard({ rateLimit: 2 })
    .get('/test', () => 'ok')

  await app.handle(new Request('http://localhost/test')) // 200
  await app.handle(new Request('http://localhost/test')) // 200
  const res = await app.handle(new Request('http://localhost/test')) // 429
  expect(res.status).toBe(429)
})

WebSocket Testing

Use bun test with WebSocket constructor. Test connection, message send/receive, and disconnect handlers.

Rules

  • Eden Treaty client uses typeof app — export the App type from the main entry point.
  • Plugins are named (new Elysia({name})) for proper deduplication by Elysia's plugin system.
  • Choose hook scope deliberately: local (default, plugin only), scoped (plugin + direct parent), global (everything).
  • Macro names are camelCase and map to lifecycle hooks (beforeHandle, onRequest, etc).
  • WebSocket routes isolated in dedicated plugin — never mixed with HTTP routes.
  • Register the error handler plugin before routes; hooks only apply to routes registered after them.

References

  • references/eden-treaty.md — Eden Treaty Client
  • references/elysia-auth.md — Authentication Patterns
  • references/elysia-performance.md — Performance Optimization
  • references/elysia-plugins.md — Plugin Development
  • references/elysia-validation.md — Validation and Schemas
  • references/elysia-websocket.md — WebSocket Patterns

Handoff

Hand off to backend/elysia/architecture/SKILL.md for project structure or backend/universal/api-response/SKILL.md for API response formatting.

Matgeliwwert Fichieren

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Elysia Patterns?

Use this skill when the user says 'elysia pattern', 'elysia eden treaty', 'elysia websocket', 'elysia macro', 'elysia validation', 'elysia file upload', 'elysia custom plugin'. Implements Elysia recipes: Eden Treaty clients, built-in WebSocket pub/sub, parameterized macros, custom plugins with lifecycle hooks, TypeBox validation, uploads, error mapping and DI via decorate. Do NOT use for: overall project structure and lifecycle order (elysia-architecture), Express middleware, non-Elysia frameworks.

Firwat Elysia Patterns op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Elysia Patterns besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt amplaz vun engem Abonnement pro Benotzerplaz. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Elysia Patterns an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/j4flmao/agent-skills/tree/main/skills/backend/elysia/patterns an. TypingMind liest d'Datei SKILL.md, bündelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Elysia Patterns benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Elysia Patterns benotzen?

Esou vill Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Elysia Patterns domat benotzen – GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama a méi – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Elysia Patterns gratis?

Jo. De Skill gëtt vum j4flmao ënner der Lizenz MIT op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbaart Pak vun Instruktiounen, dat engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt en nokënnt a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a keng Lafzäitëmfeld a funktionéiere mat all Modell.

All weisen

Elo Ären eegene KI-Workspace ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇