Commit 8d190dc
feat(api): 全业务 API 统一 {code, data, message, requestId} envelope (#49)
* WIP: envelope refactor before merging origin/main
* feat(api): unify every business endpoint on a `{code, data, message, requestId}` envelope
Why
- SaaS SDKs (TS now, C#/Go/Python planned) need a stable response contract —
across 30 modules / 459 operations we had no uniform success shape, only a
loose `{error, code?, requestId?}` on failure. Each codegen target would
have to special-case every route's return type.
- Admin / client wrappers have no single unwrap point, so error toasts and
loading / success branching are ad-hoc everywhere.
- Validation errors escaped the envelope entirely (`@hono/zod-openapi` default
`{success:false, error}`), so the frontend had to handle two error shapes.
What
- `apps/server/src/lib/response.ts` — `ok()` / `fail()` helpers that read
`requestId` from the `requestContext` AsyncLocalStorage (no plumbing through
every handler); `envelopeOf<T>(schema)` factory to wrap any zod response
schema in the envelope; `ErrorEnvelopeSchema` / `NullDataEnvelopeSchema`
+ `commonErrorResponses` map for every router's `responses` block.
- `apps/server/src/lib/openapi.ts` — `createAdminRouter` /
`createClientRouter` / `createPublicRouter` now bundle the
`defaultHook` (validation failure → envelope with `code: "validation_error"`)
and an `onError` that maps every `ModuleError` subclass to the envelope
using the subclass's `code` + `httpStatus`. No module writes its own
`router.onError` anymore.
- All 30 business modules (admin + client routes) reapplied: every 2xx
success schema wrapped in `envelopeOf(...)`, every handler return wrapped
in `ok(...)`, every `c.body(null, 204)` turned into `c.json(ok(null), 200)`
with `NullDataEnvelopeSchema`. DELETE / ack endpoints no longer return
204 — everything is 200 + envelope so the SDK/frontend unwrap doesn't
need a status branch.
- Deleted the per-module `ErrorResponseSchema` (29 of 30 modules via agent,
level via merge resolution) — everything now references the shared
`ApiErrorEnvelope` component.
- `apps/server/src/index.ts` global `app.onError` → `fail(INTERNAL_ERROR_CODE,
err.message)` + HTTP 500; new `app.notFound` returns
`{code:"not_found", message:"Not found: <method> <path>"}` + HTTP 404
so unknown paths stop leaking plain-text "404 Not Found".
- `apps/admin/src/lib/api-client.ts` unwraps `body.data` transparently on
success and synthesizes `ApiError.body.error` from the envelope's
`message` for backward compatibility (80+ call sites reading
`err.body.error` stay untouched). Added `err.code` / `err.requestId`
getters for new code paths.
- Drive-by fix in `apps/server/src/db.ts`: added `import type { Pool as PgPool }
from "pg"` — the local-Postgres branch referenced `pg.Pool` as a namespace
at the top level but the runtime `pg` import was function-scoped, so
`tsc --noEmit` failed on main before this PR.
- `apps/server/CLAUDE.md` — retired the "Error handling — throw, map in
router onError" section in favor of a "Response envelope" section that
walks through `createAdminRouter` / `createAdminRoute` / `envelopeOf` /
`ok()` / `NullDataEnvelopeSchema` as the canonical route recipe.
- SDK regenerated: `packages/sdk-core/specs/openapi*.json` (459 ops, 403
schemas with `ApiErrorEnvelope` + `ApiNullEnvelope` replacing the 30 old
per-module `XxxErrorResponse` types); `packages/sdk-{admin,client}-ts`
generated clients are rewired accordingly.
Not in scope (follow-ups)
- `serializeXxx` functions in every module are 70% redundant boilerplate —
left untouched deliberately; a separate PR will replace them with either
zod `.parse()` or direct Drizzle-row pass-through.
- Auth middlewares (`require-admin-or-api-key`, `require-client-credential`,
etc.) still return `{error, requestId}` on 401/403. Routing them through
`throw new ModuleError("unauthorized", 401, ...)` so the factory `onError`
handles them will unify the last 401/403 gap.
Validation
- `pnpm --filter=server check-types` + `lint --max-warnings 0` — green
- `pnpm --filter=server test src/modules/check-in` — 28/28 (admin + client
+ service layers cover envelope contract on create, delete, error codes)
- `pnpm --filter=@apollokit/admin check-types` + `@apollokit/client
check-types` — green after SDK regen
- Live smoke test in browser against admin dev: create check-in config
→ 201 envelope; delete → 200 + `data:null`; create banner group → 201
envelope + detail page loads; friend `GET /settings` nullable returns
200 + `data:null` and the UI shows the empty state cleanly; curl to an
unknown path returns the envelope 404.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent f4969fa commit 8d190dc
105 files changed
Lines changed: 192768 additions & 14526 deletions
File tree
- apps
- admin/src/lib
- server
- src
- lib
- modules
- activity
- analytics
- announcement
- assist-pool
- banner
- cdkey
- check-in
- client-credentials
- collection
- currency
- dialogue
- end-user
- entity
- event-catalog
- exchange
- friend-gift
- friend
- guild
- invite
- item
- leaderboard
- level
- lottery
- mail
- media-library
- rank
- shop
- storage-box
- task
- team
- packages
- sdk-admin-ts/src/generated
- sdk-client-ts/src/generated
- sdk-core/specs
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
1 | 21 | | |
2 | 22 | | |
3 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
4 | 32 | | |
5 | 33 | | |
6 | 34 | | |
7 | | - | |
| 35 | + | |
8 | 36 | | |
9 | | - | |
| 37 | + | |
10 | 38 | | |
11 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
12 | 112 | | |
13 | 113 | | |
14 | 114 | | |
| |||
24 | 124 | | |
25 | 125 | | |
26 | 126 | | |
27 | | - | |
28 | | - | |
29 | | - | |
| 127 | + | |
30 | 128 | | |
31 | 129 | | |
32 | | - | |
| 130 | + | |
33 | 131 | | |
34 | 132 | | |
35 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
36 | 143 | | |
37 | 144 | | |
38 | 145 | | |
| |||
55 | 162 | | |
56 | 163 | | |
57 | 164 | | |
58 | | - | |
59 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
60 | 168 | | |
61 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
62 | 177 | | |
63 | | - | |
| 178 | + | |
64 | 179 | | |
65 | 180 | | |
66 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
172 | 188 | | |
173 | | - | |
174 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
175 | 193 | | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
187 | 283 | | |
188 | | - | |
189 | 284 | | |
190 | | - | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
191 | 288 | | |
192 | 289 | | |
193 | 290 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | | - | |
| 91 | + | |
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
127 | 128 | | |
128 | 129 | | |
129 | 130 | | |
130 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
131 | 135 | | |
132 | 136 | | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
| 137 | + | |
137 | 138 | | |
138 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
139 | 151 | | |
140 | 152 | | |
141 | 153 | | |
| |||
265 | 277 | | |
266 | 278 | | |
267 | 279 | | |
268 | | - | |
| 280 | + | |
269 | 281 | | |
270 | 282 | | |
271 | 283 | | |
| |||
0 commit comments