@@ -4,47 +4,24 @@ description: Use when creating or modifying Elysia API routes. Ensures proper va
44allowed-tools : Read, Edit, Write, Glob, Grep, Bash
55---
66
7- API routes use [ Elysia ] ( https://elysiajs.com ) with TypeBox validation:
7+ See [ API patterns ] ( ../../docs/api-patterns.md ) for full reference.
88
9+ ** Quick example** :
910``` typescript
1011import { Elysia , t } from ' elysia'
1112import { prisma } from ' @/lib/prisma'
1213
13- const app = new Elysia ()
14- .get (' /tasks/:id' , async ({ params , query , status }) => {
15- const limit = query .limit ?? 10
14+ new Elysia ()
15+ .get (' /tasks/:id' , async ({ params , status }) => {
1616 const task = await prisma .task .findUnique ({
1717 where: { id: params .id },
1818 select: { id: true , title: true }
1919 })
2020 if (! task ) return status (404 , { error: ' Not found' })
2121 return task
2222 }, {
23- params: t .Object ({ id: t .String () }),
24- query: t .Object ({ limit: t .Optional (t .Numeric ()) })
23+ params: t .Object ({ id: t .String () })
2524 })
26- .post (' /tasks' , async ({ body , status }) => {
27- const task = await prisma .task .create ({ data: body })
28- return status (201 , task )
29- }, {
30- body: t .Object ({
31- title: t .String ({ minLength: 1 }),
32- fullScore: t .Number ({ minimum: 0 })
33- })
34- })
35- ```
36-
37- ** Auth guard pattern** :
38- ``` typescript
39- .derive (async ({ headers , status }) => {
40- const user = await getUser (headers .authorization )
41- if (! user ) return status (401 , { error: ' Unauthorized' })
42- return { user }
43- })
44- .get (' /admin' , ({ user , status }) => {
45- if (! user .admin ) return status (403 , { error: ' Forbidden' })
46- return ' admin only'
47- })
4825```
4926
50- ** Checklist** : ` t.Object ` validation, auth derive/guard , selective Prisma fields, pagination, ` status() ` for errors .
27+ ** Checklist** : ` t.Object ` validation, auth guards , selective Prisma fields, pagination, proper status codes .
0 commit comments