### Initialize a new Elysia project Source: https://elysiajs.com Command to scaffold a new Elysia application using Bun. ```bash bun create elysia app ``` -------------------------------- ### Define a basic Elysia server Source: https://elysiajs.com Demonstrates basic routing, file serving, streaming, and WebSocket handling in a single Elysia instance. ```typescript import { Elysia, file } from 'elysia' new Elysia() .get('/', 'Hello World') .get('/image', file('mika.webp')) .get('/stream', function* () { yield 'Hello' yield 'World' }) .ws('/realtime', { message(ws, message) { ws.send('got:' + message) } }) .listen(3000) ``` -------------------------------- ### Generating OpenAPI Documentation Source: https://elysiajs.com Automatically generate OpenAPI documentation by using the openapi plugin. ```typescript import { Elysia } from 'elysia' import { openapi } from '@elysia/openapi' new Elysia() .use(openapi()) ``` -------------------------------- ### Using Auth Plugin in Elysia Source: https://elysiajs.com Integrate an authentication plugin into an Elysia instance to protect routes. ```typescript import { Elysia } from 'elysia' import { auth } from './auth' new Elysia () . use ( auth ) . get ('/profile', ({ user }) => user , { auth : true }) ``` -------------------------------- ### Generate OpenAPI from TypeScript Source: https://elysiajs.com Use the @elysia/openapi plugin to automatically generate API documentation from TypeScript types without manual annotations. ```typescript import { Elysia } from 'elysia' import { openapi, fromTypes } from '@elysia/openapi' export const app = new Elysia() .use( openapi({ // ↓ Where magic happens references: fromTypes() }) ) ``` -------------------------------- ### Defining an Auth Macro Source: https://elysiajs.com Create a reusable authentication macro that validates cookies and resolves user data. ```typescript import { Elysia, t } from 'elysia' export const auth = new Elysia() .macro('auth', { cookie: t.Object({ ssid: t.String() }), resolve({ cookie, status }) { if(!cookie.ssid.value) return status(401) return { user: cookie.ssid.value } } }) ``` -------------------------------- ### Test Elysia Server with Type Safety Source: https://elysiajs.com Utilize Eden Treaty within Bun tests to ensure type-safe API interactions and catch schema mismatches at compile time. ```typescript import { treaty } from '@elysia/eden' import { app } from './index' import { test , expect } from 'bun:test' const server = treaty ( app ) test ('should handle duplicated user', async () => { const { error } = await server . user . put ({ Argument of type '{ username: string; }' is not assignable to parameter of type '{ username: string; password: string; }'. Property 'password' is missing in type '{ username: string; }' but required in type '{ username: string; password: string; }'. username : 'mika', }) expect ( error ?. value ). toEqual ({ success : false, message : 'Username already taken' }) }) ``` -------------------------------- ### Client-Server Communication with Eden Source: https://elysiajs.com Share types between client and server using treaty to ensure type-safe API calls. ```typescript import { treaty } from '@elysia/eden' import type { App } from 'server' const api = treaty < App >('api.elysiajs.com') const { data } = await api .profile.patch({ age : 21 }) ``` -------------------------------- ### Validate request body with schema Source: https://elysiajs.com Uses Elysia's type system to validate and normalize incoming request bodies, specifically for file uploads. ```typescript import { Elysia , t } from 'elysia' new Elysia () . put ('/', ({ body : { file } }) => file , { body : t . Object ({ file : t . File ({ type : 'image' }) }) }) ``` -------------------------------- ### Validate Request Body with Multiple Libraries Source: https://elysiajs.com Elysia supports various validation libraries like TypeBox, Zod, Valibot, ArkType, and Effect for request body validation with full type inference. ```ts import { Elysia, t } from 'elysia' new Elysia() // Try hover body ↓ .post('/user', ({ body }) => body, { body: t.Object({ name: t.Literal('SaltyAom'), age: t.Number(), friends: t.Array(t.String()) }) }) ``` ```ts import { Elysia } from 'elysia' import { z } from 'zod' new Elysia() // Try hover body ↓ .post('/user', ({ body }) => body, { body: z.object({ name: z.literal('SaltyAom'), age: z.number(), friends: z.array(z.string()) }) }) ``` ```ts import { Elysia } from 'elysia' import * as v from 'valibot' new Elysia() // Try hover body ↓ .post('/user', ({ body }) => body, { body: v.object({ name: v.literal('SaltyAom'), age: v.number(), friends: v.array(v.string()) }) }) ``` ```ts import { Elysia } from 'elysia' import { type } from 'arktype' new Elysia() // Try hover body ↓ .post('/user', ({ body }) => body, { body: type({ name: '"Elysia"', age: 'number', friends: 'string[]' }) }) ``` ```ts import { Elysia } from 'elysia' import { Schema } from 'effect' new Elysia() // Try hover body ↓ .post('/user', ({ body }) => body, { body: Schema.standardSchemaV1( Schema.Struct({ name: Schema.Literal('Elysia'), age: Schema.Number, friends: Schema.Array(Schema.String) }) ) }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.