@api ๋ฐ์ฝ๋ ์ดํฐ๋ Model ๋ฉ์๋๋ฅผ HTTP API ์๋ํฌ์ธํธ๋ก ์๋ ๋ณํํฉ๋๋ค. ๋ฉ์๋์ ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ฉด ๋ผ์ฐํ
, ํ์
๊ฒ์ฆ, ํด๋ผ์ด์ธํธ ์ฝ๋๊ฐ ์๋ ์์ฑ๋ฉ๋๋ค.
๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
- HTTP ์๋ํฌ์ธํธ:
GET /user/findById?id=1 - TypeScript ํด๋ผ์ด์ธํธ ํจ์
- TanStack Query hooks (์ ํ ์)
- API ๋ฌธ์
๋ฐ์ฝ๋ ์ดํฐ ์ต์
์ ์ฒด ์ต์
httpMethod
HTTP ๋ฉ์๋๋ฅผ ์ง์ ํฉ๋๋ค.clients
์์ฑํ ํด๋ผ์ด์ธํธ ์ฝ๋ ํ์ ์ ์ง์ ํฉ๋๋ค.path
์ปค์คํ API ๊ฒฝ๋ก๋ฅผ ์ง์ ํฉ๋๋ค.๊ฒฝ๋ก๋ฅผ ์๋ตํ๋ฉด
/{model}/{method} ํ์์ผ๋ก ์๋ ์์ฑ๋ฉ๋๋ค.- Model:
UserModelโuser - Method:
findByIdโfindById - ๊ฒฐ๊ณผ:
/user/findById
resourceName
API ๋ฆฌ์์ค ์ด๋ฆ์ ์ง์ ํฉ๋๋ค. TanStack Query์ queryKey์ ์ฌ์ฉ๋ฉ๋๋ค.guards
์ธ์ฆ ๋ฐ ๊ถํ ๊ฒ์ฌ๋ฅผ ์ค์ ํฉ๋๋ค.Guard ๋ก์ง์
sonamu.config.ts์ guardHandler์์ ์ ์ํฉ๋๋ค.sonamu.config.ts
contentType
์๋ต์ Content-Type์ ์ง์ ํฉ๋๋ค.timeout
API ํ์์์์ ๋ฐ๋ฆฌ์ด ๋จ์๋ก ์ง์ ํฉ๋๋ค.cacheControl
HTTP Cache-Control ํค๋๋ฅผ ์ค์ ํฉ๋๋ค.compress
์๋ต ์์ถ ์ค์ ์ ์ ์ดํฉ๋๋ค.์ฌ๋ฌ ๋ฐ์ฝ๋ ์ดํฐ ์กฐํฉ
@api + @transactional
ํธ๋์ญ์ ๋ด์์ API๋ฅผ ์คํํฉ๋๋ค.@upload (๋ ๋ฆฝ ์ฌ์ฉ)
ํ์ผ ์ ๋ก๋ API๋ฅผ ๋ง๋ญ๋๋ค.@upload๋ @api ์์ด ๋
๋ฆฝ์ ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
๋จ์ผ/๋ค์ค ํ์ผ ์ฌ๋ถ๋ ํ๋ผ๋ฏธํฐ ํ์
(
UploadedFile vs UploadedFile[])์ผ๋ก ๊ฒฐ์ ๋ฉ๋๋ค.
API ๊ฒฝ๋ก ๊ท์น
๊ธฐ๋ณธ ๊ฒฝ๋ก๋ ๋ค์ ๊ท์น์ผ๋ก ์์ฑ๋ฉ๋๋ค:- Model ์ด๋ฆ: PascalCase โ camelCase
UserModelโuserBlogPostModelโblogPost
- Method ์ด๋ฆ: ๊ทธ๋๋ก ์ฌ์ฉ
findByIdโfindById
์ค์ ์์
๊ธฐ๋ณธ CRUD API
์ธ์ฆ API
Sonamu๋ ์ธ์ฆ์ better-auth์ HTTP ์๋ํฌ์ธํธ๋ก ์ฒ๋ฆฌํฉ๋๋ค. ๋ก๊ทธ์ธ(
/api/auth/sign-in/email), ๋ก๊ทธ์์(/api/auth/sign-out) ๋ฑ์ Sonamu @api ๋ฐ์ฝ๋ ์ดํฐ๊ฐ ์๋ better-auth๊ฐ ์ง์ ์ ๊ณตํ๋ ์๋ํฌ์ธํธ๋ฅผ ์ฌ์ฉํ์ธ์.me() API๋ context.user๋ฅผ ํตํด ๊ตฌํํฉ๋๋ค.
๋ค์ ๋จ๊ณ
Business Logic
๋น์ฆ๋์ค ๋ก์ง ์์ฑ ํจํด ๋ฐฐ์ฐ๊ธฐ
Stream Decorator
@stream์ผ๋ก ์ค์๊ฐ ์ด๋ฒคํธ ์ ์กํ๊ธฐ
Upload Decorator
@upload๋ก ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌํ๊ธฐ
Guards
์ธ์ฆ๊ณผ ๊ถํ ๊ฒ์ฌ ๊ตฌํํ๊ธฐ