@juho0719/cckit 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/agents/architect.md +211 -0
- package/assets/agents/build-error-resolver.md +114 -0
- package/assets/agents/ccwin-code-reviewer.md +224 -0
- package/assets/agents/database-reviewer.md +91 -0
- package/assets/agents/doc-updater.md +107 -0
- package/assets/agents/e2e-runner.md +107 -0
- package/assets/agents/planner.md +212 -0
- package/assets/agents/python-reviewer.md +98 -0
- package/assets/agents/refactor-cleaner.md +85 -0
- package/assets/agents/security-reviewer.md +108 -0
- package/assets/agents/superpower-code-reviewer.md +48 -0
- package/assets/agents/tdd-guide.md +80 -0
- package/assets/commands/build-fix.md +62 -0
- package/assets/commands/checkpoint.md +74 -0
- package/assets/commands/code-review.md +40 -0
- package/assets/commands/e2e.md +362 -0
- package/assets/commands/eval.md +120 -0
- package/assets/commands/orchestrate.md +172 -0
- package/assets/commands/plan.md +113 -0
- package/assets/commands/python-review.md +297 -0
- package/assets/commands/refactor-clean.md +80 -0
- package/assets/commands/sessions.md +305 -0
- package/assets/commands/tdd.md +326 -0
- package/assets/commands/test-coverage.md +69 -0
- package/assets/commands/update-codemaps.md +72 -0
- package/assets/commands/update-docs.md +84 -0
- package/assets/commands/verify.md +59 -0
- package/assets/hooks/post-edit-format.js +49 -0
- package/assets/hooks/post-edit-typecheck.js +96 -0
- package/assets/mcps/mcp-servers.json +92 -0
- package/assets/rules/common/agents.md +49 -0
- package/assets/rules/common/coding-style.md +48 -0
- package/assets/rules/common/git-workflow.md +45 -0
- package/assets/rules/common/hooks.md +30 -0
- package/assets/rules/common/patterns.md +31 -0
- package/assets/rules/common/performance.md +55 -0
- package/assets/rules/common/security.md +29 -0
- package/assets/rules/common/testing.md +29 -0
- package/assets/rules/python/coding-style.md +42 -0
- package/assets/rules/python/hooks.md +19 -0
- package/assets/rules/python/patterns.md +39 -0
- package/assets/rules/python/security.md +30 -0
- package/assets/rules/python/testing.md +38 -0
- package/assets/rules/typescript/coding-style.md +18 -0
- package/assets/rules/typescript/hooks.md +19 -0
- package/assets/rules/typescript/patterns.md +39 -0
- package/assets/rules/typescript/security.md +30 -0
- package/assets/rules/typescript/testing.md +38 -0
- package/assets/skills/api-design/SKILL.md +522 -0
- package/assets/skills/backend-patterns/SKILL.md +597 -0
- package/assets/skills/brainstorming/SKILL.md +96 -0
- package/assets/skills/coding-standards/SKILL.md +529 -0
- package/assets/skills/database-migrations/SKILL.md +334 -0
- package/assets/skills/deployment-patterns/SKILL.md +426 -0
- package/assets/skills/dispatching-parallel-agents/SKILL.md +180 -0
- package/assets/skills/docker-patterns/SKILL.md +363 -0
- package/assets/skills/e2e-testing/SKILL.md +325 -0
- package/assets/skills/eval-harness/SKILL.md +235 -0
- package/assets/skills/executing-plans/SKILL.md +84 -0
- package/assets/skills/finishing-a-development-branch/SKILL.md +200 -0
- package/assets/skills/frontend-patterns/SKILL.md +641 -0
- package/assets/skills/iterative-retrieval/SKILL.md +210 -0
- package/assets/skills/postgres-patterns/SKILL.md +145 -0
- package/assets/skills/python-patterns/SKILL.md +749 -0
- package/assets/skills/python-testing/SKILL.md +815 -0
- package/assets/skills/receiving-code-review/SKILL.md +213 -0
- package/assets/skills/requesting-code-review/SKILL.md +105 -0
- package/assets/skills/requesting-code-review/code-reviewer-template.md +146 -0
- package/assets/skills/subagent-driven-development/SKILL.md +242 -0
- package/assets/skills/subagent-driven-development/code-quality-reviewer-prompt.md +20 -0
- package/assets/skills/subagent-driven-development/implementer-prompt.md +78 -0
- package/assets/skills/subagent-driven-development/spec-reviewer-prompt.md +61 -0
- package/assets/skills/systematic-debugging/CREATION-LOG.md +114 -0
- package/assets/skills/systematic-debugging/SKILL.md +296 -0
- package/assets/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
- package/assets/skills/systematic-debugging/condition-based-waiting.md +115 -0
- package/assets/skills/systematic-debugging/defense-in-depth.md +122 -0
- package/assets/skills/systematic-debugging/root-cause-tracing.md +169 -0
- package/assets/skills/systematic-debugging/scripts/find-polluter.sh +63 -0
- package/assets/skills/systematic-debugging/test-academic.md +14 -0
- package/assets/skills/systematic-debugging/test-pressure-1.md +58 -0
- package/assets/skills/systematic-debugging/test-pressure-2.md +68 -0
- package/assets/skills/systematic-debugging/test-pressure-3.md +69 -0
- package/assets/skills/tdd-workflow/SKILL.md +409 -0
- package/assets/skills/test-driven-development/SKILL.md +371 -0
- package/assets/skills/test-driven-development/testing-anti-patterns.md +299 -0
- package/assets/skills/using-git-worktrees/SKILL.md +218 -0
- package/assets/skills/verification-before-completion/SKILL.md +139 -0
- package/assets/skills/verification-loop/SKILL.md +125 -0
- package/assets/skills/writing-plans/SKILL.md +116 -0
- package/dist/agents-AEKT67A6.js +9 -0
- package/dist/chunk-3GUKEMND.js +28 -0
- package/dist/chunk-3UNN3IBE.js +54 -0
- package/dist/chunk-3Y26YU4R.js +27 -0
- package/dist/chunk-5XOKKPAA.js +21 -0
- package/dist/chunk-6B46AIFM.js +136 -0
- package/dist/chunk-EYY2IZ7N.js +27 -0
- package/dist/chunk-K25UZZVG.js +17 -0
- package/dist/chunk-KEENFBLL.js +24 -0
- package/dist/chunk-RMUKD7CW.js +44 -0
- package/dist/chunk-W63UKEIT.js +50 -0
- package/dist/cli-VZRGF733.js +238 -0
- package/dist/commands-P5LILVZ5.js +9 -0
- package/dist/hooks-IIG2XK4I.js +9 -0
- package/dist/index.js +131 -0
- package/dist/mcps-67Q7TBGW.js +6 -0
- package/dist/paths-FT6KBIRD.js +10 -0
- package/dist/registry-EGXWYWWK.js +17 -0
- package/dist/rules-2CPBVNNJ.js +7 -0
- package/dist/skills-ULMW3UCM.js +8 -0
- package/package.json +36 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: api-design
|
|
3
|
+
description: REST API design patterns including resource naming, status codes, pagination, filtering, error responses, versioning, and rate limiting for production APIs.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# API Design Patterns
|
|
7
|
+
|
|
8
|
+
Conventions and best practices for designing consistent, developer-friendly REST APIs.
|
|
9
|
+
|
|
10
|
+
## When to Activate
|
|
11
|
+
|
|
12
|
+
- Designing new API endpoints
|
|
13
|
+
- Reviewing existing API contracts
|
|
14
|
+
- Adding pagination, filtering, or sorting
|
|
15
|
+
- Implementing error handling for APIs
|
|
16
|
+
- Planning API versioning strategy
|
|
17
|
+
- Building public or partner-facing APIs
|
|
18
|
+
|
|
19
|
+
## Resource Design
|
|
20
|
+
|
|
21
|
+
### URL Structure
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
# Resources are nouns, plural, lowercase, kebab-case
|
|
25
|
+
GET /api/v1/users
|
|
26
|
+
GET /api/v1/users/:id
|
|
27
|
+
POST /api/v1/users
|
|
28
|
+
PUT /api/v1/users/:id
|
|
29
|
+
PATCH /api/v1/users/:id
|
|
30
|
+
DELETE /api/v1/users/:id
|
|
31
|
+
|
|
32
|
+
# Sub-resources for relationships
|
|
33
|
+
GET /api/v1/users/:id/orders
|
|
34
|
+
POST /api/v1/users/:id/orders
|
|
35
|
+
|
|
36
|
+
# Actions that don't map to CRUD (use verbs sparingly)
|
|
37
|
+
POST /api/v1/orders/:id/cancel
|
|
38
|
+
POST /api/v1/auth/login
|
|
39
|
+
POST /api/v1/auth/refresh
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Naming Rules
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
# GOOD
|
|
46
|
+
/api/v1/team-members # kebab-case for multi-word resources
|
|
47
|
+
/api/v1/orders?status=active # query params for filtering
|
|
48
|
+
/api/v1/users/123/orders # nested resources for ownership
|
|
49
|
+
|
|
50
|
+
# BAD
|
|
51
|
+
/api/v1/getUsers # verb in URL
|
|
52
|
+
/api/v1/user # singular (use plural)
|
|
53
|
+
/api/v1/team_members # snake_case in URLs
|
|
54
|
+
/api/v1/users/123/getOrders # verb in nested resource
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## HTTP Methods and Status Codes
|
|
58
|
+
|
|
59
|
+
### Method Semantics
|
|
60
|
+
|
|
61
|
+
| Method | Idempotent | Safe | Use For |
|
|
62
|
+
|--------|-----------|------|---------|
|
|
63
|
+
| GET | Yes | Yes | Retrieve resources |
|
|
64
|
+
| POST | No | No | Create resources, trigger actions |
|
|
65
|
+
| PUT | Yes | No | Full replacement of a resource |
|
|
66
|
+
| PATCH | No* | No | Partial update of a resource |
|
|
67
|
+
| DELETE | Yes | No | Remove a resource |
|
|
68
|
+
|
|
69
|
+
*PATCH can be made idempotent with proper implementation
|
|
70
|
+
|
|
71
|
+
### Status Code Reference
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
# Success
|
|
75
|
+
200 OK — GET, PUT, PATCH (with response body)
|
|
76
|
+
201 Created — POST (include Location header)
|
|
77
|
+
204 No Content — DELETE, PUT (no response body)
|
|
78
|
+
|
|
79
|
+
# Client Errors
|
|
80
|
+
400 Bad Request — Validation failure, malformed JSON
|
|
81
|
+
401 Unauthorized — Missing or invalid authentication
|
|
82
|
+
403 Forbidden — Authenticated but not authorized
|
|
83
|
+
404 Not Found — Resource doesn't exist
|
|
84
|
+
409 Conflict — Duplicate entry, state conflict
|
|
85
|
+
422 Unprocessable Entity — Semantically invalid (valid JSON, bad data)
|
|
86
|
+
429 Too Many Requests — Rate limit exceeded
|
|
87
|
+
|
|
88
|
+
# Server Errors
|
|
89
|
+
500 Internal Server Error — Unexpected failure (never expose details)
|
|
90
|
+
502 Bad Gateway — Upstream service failed
|
|
91
|
+
503 Service Unavailable — Temporary overload, include Retry-After
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Common Mistakes
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
# BAD: 200 for everything
|
|
98
|
+
{ "status": 200, "success": false, "error": "Not found" }
|
|
99
|
+
|
|
100
|
+
# GOOD: Use HTTP status codes semantically
|
|
101
|
+
HTTP/1.1 404 Not Found
|
|
102
|
+
{ "error": { "code": "not_found", "message": "User not found" } }
|
|
103
|
+
|
|
104
|
+
# BAD: 500 for validation errors
|
|
105
|
+
# GOOD: 400 or 422 with field-level details
|
|
106
|
+
|
|
107
|
+
# BAD: 200 for created resources
|
|
108
|
+
# GOOD: 201 with Location header
|
|
109
|
+
HTTP/1.1 201 Created
|
|
110
|
+
Location: /api/v1/users/abc-123
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Response Format
|
|
114
|
+
|
|
115
|
+
### Success Response
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"data": {
|
|
120
|
+
"id": "abc-123",
|
|
121
|
+
"email": "alice@example.com",
|
|
122
|
+
"name": "Alice",
|
|
123
|
+
"created_at": "2025-01-15T10:30:00Z"
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Collection Response (with Pagination)
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"data": [
|
|
133
|
+
{ "id": "abc-123", "name": "Alice" },
|
|
134
|
+
{ "id": "def-456", "name": "Bob" }
|
|
135
|
+
],
|
|
136
|
+
"meta": {
|
|
137
|
+
"total": 142,
|
|
138
|
+
"page": 1,
|
|
139
|
+
"per_page": 20,
|
|
140
|
+
"total_pages": 8
|
|
141
|
+
},
|
|
142
|
+
"links": {
|
|
143
|
+
"self": "/api/v1/users?page=1&per_page=20",
|
|
144
|
+
"next": "/api/v1/users?page=2&per_page=20",
|
|
145
|
+
"last": "/api/v1/users?page=8&per_page=20"
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Error Response
|
|
151
|
+
|
|
152
|
+
```json
|
|
153
|
+
{
|
|
154
|
+
"error": {
|
|
155
|
+
"code": "validation_error",
|
|
156
|
+
"message": "Request validation failed",
|
|
157
|
+
"details": [
|
|
158
|
+
{
|
|
159
|
+
"field": "email",
|
|
160
|
+
"message": "Must be a valid email address",
|
|
161
|
+
"code": "invalid_format"
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
"field": "age",
|
|
165
|
+
"message": "Must be between 0 and 150",
|
|
166
|
+
"code": "out_of_range"
|
|
167
|
+
}
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Response Envelope Variants
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
// Option A: Envelope with data wrapper (recommended for public APIs)
|
|
177
|
+
interface ApiResponse<T> {
|
|
178
|
+
data: T;
|
|
179
|
+
meta?: PaginationMeta;
|
|
180
|
+
links?: PaginationLinks;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
interface ApiError {
|
|
184
|
+
error: {
|
|
185
|
+
code: string;
|
|
186
|
+
message: string;
|
|
187
|
+
details?: FieldError[];
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Option B: Flat response (simpler, common for internal APIs)
|
|
192
|
+
// Success: just return the resource directly
|
|
193
|
+
// Error: return error object
|
|
194
|
+
// Distinguish by HTTP status code
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Pagination
|
|
198
|
+
|
|
199
|
+
### Offset-Based (Simple)
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
GET /api/v1/users?page=2&per_page=20
|
|
203
|
+
|
|
204
|
+
# Implementation
|
|
205
|
+
SELECT * FROM users
|
|
206
|
+
ORDER BY created_at DESC
|
|
207
|
+
LIMIT 20 OFFSET 20;
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**Pros:** Easy to implement, supports "jump to page N"
|
|
211
|
+
**Cons:** Slow on large offsets (OFFSET 100000), inconsistent with concurrent inserts
|
|
212
|
+
|
|
213
|
+
### Cursor-Based (Scalable)
|
|
214
|
+
|
|
215
|
+
```
|
|
216
|
+
GET /api/v1/users?cursor=eyJpZCI6MTIzfQ&limit=20
|
|
217
|
+
|
|
218
|
+
# Implementation
|
|
219
|
+
SELECT * FROM users
|
|
220
|
+
WHERE id > :cursor_id
|
|
221
|
+
ORDER BY id ASC
|
|
222
|
+
LIMIT 21; -- fetch one extra to determine has_next
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
```json
|
|
226
|
+
{
|
|
227
|
+
"data": [...],
|
|
228
|
+
"meta": {
|
|
229
|
+
"has_next": true,
|
|
230
|
+
"next_cursor": "eyJpZCI6MTQzfQ"
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Pros:** Consistent performance regardless of position, stable with concurrent inserts
|
|
236
|
+
**Cons:** Cannot jump to arbitrary page, cursor is opaque
|
|
237
|
+
|
|
238
|
+
### When to Use Which
|
|
239
|
+
|
|
240
|
+
| Use Case | Pagination Type |
|
|
241
|
+
|----------|----------------|
|
|
242
|
+
| Admin dashboards, small datasets (<10K) | Offset |
|
|
243
|
+
| Infinite scroll, feeds, large datasets | Cursor |
|
|
244
|
+
| Public APIs | Cursor (default) with offset (optional) |
|
|
245
|
+
| Search results | Offset (users expect page numbers) |
|
|
246
|
+
|
|
247
|
+
## Filtering, Sorting, and Search
|
|
248
|
+
|
|
249
|
+
### Filtering
|
|
250
|
+
|
|
251
|
+
```
|
|
252
|
+
# Simple equality
|
|
253
|
+
GET /api/v1/orders?status=active&customer_id=abc-123
|
|
254
|
+
|
|
255
|
+
# Comparison operators (use bracket notation)
|
|
256
|
+
GET /api/v1/products?price[gte]=10&price[lte]=100
|
|
257
|
+
GET /api/v1/orders?created_at[after]=2025-01-01
|
|
258
|
+
|
|
259
|
+
# Multiple values (comma-separated)
|
|
260
|
+
GET /api/v1/products?category=electronics,clothing
|
|
261
|
+
|
|
262
|
+
# Nested fields (dot notation)
|
|
263
|
+
GET /api/v1/orders?customer.country=US
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Sorting
|
|
267
|
+
|
|
268
|
+
```
|
|
269
|
+
# Single field (prefix - for descending)
|
|
270
|
+
GET /api/v1/products?sort=-created_at
|
|
271
|
+
|
|
272
|
+
# Multiple fields (comma-separated)
|
|
273
|
+
GET /api/v1/products?sort=-featured,price,-created_at
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Full-Text Search
|
|
277
|
+
|
|
278
|
+
```
|
|
279
|
+
# Search query parameter
|
|
280
|
+
GET /api/v1/products?q=wireless+headphones
|
|
281
|
+
|
|
282
|
+
# Field-specific search
|
|
283
|
+
GET /api/v1/users?email=alice
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Sparse Fieldsets
|
|
287
|
+
|
|
288
|
+
```
|
|
289
|
+
# Return only specified fields (reduces payload)
|
|
290
|
+
GET /api/v1/users?fields=id,name,email
|
|
291
|
+
GET /api/v1/orders?fields=id,total,status&include=customer.name
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## Authentication and Authorization
|
|
295
|
+
|
|
296
|
+
### Token-Based Auth
|
|
297
|
+
|
|
298
|
+
```
|
|
299
|
+
# Bearer token in Authorization header
|
|
300
|
+
GET /api/v1/users
|
|
301
|
+
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
|
|
302
|
+
|
|
303
|
+
# API key (for server-to-server)
|
|
304
|
+
GET /api/v1/data
|
|
305
|
+
X-API-Key: sk_live_abc123
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### Authorization Patterns
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
// Resource-level: check ownership
|
|
312
|
+
app.get("/api/v1/orders/:id", async (req, res) => {
|
|
313
|
+
const order = await Order.findById(req.params.id);
|
|
314
|
+
if (!order) return res.status(404).json({ error: { code: "not_found" } });
|
|
315
|
+
if (order.userId !== req.user.id) return res.status(403).json({ error: { code: "forbidden" } });
|
|
316
|
+
return res.json({ data: order });
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// Role-based: check permissions
|
|
320
|
+
app.delete("/api/v1/users/:id", requireRole("admin"), async (req, res) => {
|
|
321
|
+
await User.delete(req.params.id);
|
|
322
|
+
return res.status(204).send();
|
|
323
|
+
});
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Rate Limiting
|
|
327
|
+
|
|
328
|
+
### Headers
|
|
329
|
+
|
|
330
|
+
```
|
|
331
|
+
HTTP/1.1 200 OK
|
|
332
|
+
X-RateLimit-Limit: 100
|
|
333
|
+
X-RateLimit-Remaining: 95
|
|
334
|
+
X-RateLimit-Reset: 1640000000
|
|
335
|
+
|
|
336
|
+
# When exceeded
|
|
337
|
+
HTTP/1.1 429 Too Many Requests
|
|
338
|
+
Retry-After: 60
|
|
339
|
+
{
|
|
340
|
+
"error": {
|
|
341
|
+
"code": "rate_limit_exceeded",
|
|
342
|
+
"message": "Rate limit exceeded. Try again in 60 seconds."
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Rate Limit Tiers
|
|
348
|
+
|
|
349
|
+
| Tier | Limit | Window | Use Case |
|
|
350
|
+
|------|-------|--------|----------|
|
|
351
|
+
| Anonymous | 30/min | Per IP | Public endpoints |
|
|
352
|
+
| Authenticated | 100/min | Per user | Standard API access |
|
|
353
|
+
| Premium | 1000/min | Per API key | Paid API plans |
|
|
354
|
+
| Internal | 10000/min | Per service | Service-to-service |
|
|
355
|
+
|
|
356
|
+
## Versioning
|
|
357
|
+
|
|
358
|
+
### URL Path Versioning (Recommended)
|
|
359
|
+
|
|
360
|
+
```
|
|
361
|
+
/api/v1/users
|
|
362
|
+
/api/v2/users
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
**Pros:** Explicit, easy to route, cacheable
|
|
366
|
+
**Cons:** URL changes between versions
|
|
367
|
+
|
|
368
|
+
### Header Versioning
|
|
369
|
+
|
|
370
|
+
```
|
|
371
|
+
GET /api/users
|
|
372
|
+
Accept: application/vnd.myapp.v2+json
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
**Pros:** Clean URLs
|
|
376
|
+
**Cons:** Harder to test, easy to forget
|
|
377
|
+
|
|
378
|
+
### Versioning Strategy
|
|
379
|
+
|
|
380
|
+
```
|
|
381
|
+
1. Start with /api/v1/ — don't version until you need to
|
|
382
|
+
2. Maintain at most 2 active versions (current + previous)
|
|
383
|
+
3. Deprecation timeline:
|
|
384
|
+
- Announce deprecation (6 months notice for public APIs)
|
|
385
|
+
- Add Sunset header: Sunset: Sat, 01 Jan 2026 00:00:00 GMT
|
|
386
|
+
- Return 410 Gone after sunset date
|
|
387
|
+
4. Non-breaking changes don't need a new version:
|
|
388
|
+
- Adding new fields to responses
|
|
389
|
+
- Adding new optional query parameters
|
|
390
|
+
- Adding new endpoints
|
|
391
|
+
5. Breaking changes require a new version:
|
|
392
|
+
- Removing or renaming fields
|
|
393
|
+
- Changing field types
|
|
394
|
+
- Changing URL structure
|
|
395
|
+
- Changing authentication method
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
## Implementation Patterns
|
|
399
|
+
|
|
400
|
+
### TypeScript (Next.js API Route)
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
import { z } from "zod";
|
|
404
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
405
|
+
|
|
406
|
+
const createUserSchema = z.object({
|
|
407
|
+
email: z.string().email(),
|
|
408
|
+
name: z.string().min(1).max(100),
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
export async function POST(req: NextRequest) {
|
|
412
|
+
const body = await req.json();
|
|
413
|
+
const parsed = createUserSchema.safeParse(body);
|
|
414
|
+
|
|
415
|
+
if (!parsed.success) {
|
|
416
|
+
return NextResponse.json({
|
|
417
|
+
error: {
|
|
418
|
+
code: "validation_error",
|
|
419
|
+
message: "Request validation failed",
|
|
420
|
+
details: parsed.error.issues.map(i => ({
|
|
421
|
+
field: i.path.join("."),
|
|
422
|
+
message: i.message,
|
|
423
|
+
code: i.code,
|
|
424
|
+
})),
|
|
425
|
+
},
|
|
426
|
+
}, { status: 422 });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const user = await createUser(parsed.data);
|
|
430
|
+
|
|
431
|
+
return NextResponse.json(
|
|
432
|
+
{ data: user },
|
|
433
|
+
{
|
|
434
|
+
status: 201,
|
|
435
|
+
headers: { Location: `/api/v1/users/${user.id}` },
|
|
436
|
+
},
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
### Python (Django REST Framework)
|
|
442
|
+
|
|
443
|
+
```python
|
|
444
|
+
from rest_framework import serializers, viewsets, status
|
|
445
|
+
from rest_framework.response import Response
|
|
446
|
+
|
|
447
|
+
class CreateUserSerializer(serializers.Serializer):
|
|
448
|
+
email = serializers.EmailField()
|
|
449
|
+
name = serializers.CharField(max_length=100)
|
|
450
|
+
|
|
451
|
+
class UserSerializer(serializers.ModelSerializer):
|
|
452
|
+
class Meta:
|
|
453
|
+
model = User
|
|
454
|
+
fields = ["id", "email", "name", "created_at"]
|
|
455
|
+
|
|
456
|
+
class UserViewSet(viewsets.ModelViewSet):
|
|
457
|
+
serializer_class = UserSerializer
|
|
458
|
+
permission_classes = [IsAuthenticated]
|
|
459
|
+
|
|
460
|
+
def get_serializer_class(self):
|
|
461
|
+
if self.action == "create":
|
|
462
|
+
return CreateUserSerializer
|
|
463
|
+
return UserSerializer
|
|
464
|
+
|
|
465
|
+
def create(self, request):
|
|
466
|
+
serializer = CreateUserSerializer(data=request.data)
|
|
467
|
+
serializer.is_valid(raise_exception=True)
|
|
468
|
+
user = UserService.create(**serializer.validated_data)
|
|
469
|
+
return Response(
|
|
470
|
+
{"data": UserSerializer(user).data},
|
|
471
|
+
status=status.HTTP_201_CREATED,
|
|
472
|
+
headers={"Location": f"/api/v1/users/{user.id}"},
|
|
473
|
+
)
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### Go (net/http)
|
|
477
|
+
|
|
478
|
+
```go
|
|
479
|
+
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
|
480
|
+
var req CreateUserRequest
|
|
481
|
+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
482
|
+
writeError(w, http.StatusBadRequest, "invalid_json", "Invalid request body")
|
|
483
|
+
return
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if err := req.Validate(); err != nil {
|
|
487
|
+
writeError(w, http.StatusUnprocessableEntity, "validation_error", err.Error())
|
|
488
|
+
return
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
user, err := h.service.Create(r.Context(), req)
|
|
492
|
+
if err != nil {
|
|
493
|
+
switch {
|
|
494
|
+
case errors.Is(err, domain.ErrEmailTaken):
|
|
495
|
+
writeError(w, http.StatusConflict, "email_taken", "Email already registered")
|
|
496
|
+
default:
|
|
497
|
+
writeError(w, http.StatusInternalServerError, "internal_error", "Internal error")
|
|
498
|
+
}
|
|
499
|
+
return
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
w.Header().Set("Location", fmt.Sprintf("/api/v1/users/%s", user.ID))
|
|
503
|
+
writeJSON(w, http.StatusCreated, map[string]any{"data": user})
|
|
504
|
+
}
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
## API Design Checklist
|
|
508
|
+
|
|
509
|
+
Before shipping a new endpoint:
|
|
510
|
+
|
|
511
|
+
- [ ] Resource URL follows naming conventions (plural, kebab-case, no verbs)
|
|
512
|
+
- [ ] Correct HTTP method used (GET for reads, POST for creates, etc.)
|
|
513
|
+
- [ ] Appropriate status codes returned (not 200 for everything)
|
|
514
|
+
- [ ] Input validated with schema (Zod, Pydantic, Bean Validation)
|
|
515
|
+
- [ ] Error responses follow standard format with codes and messages
|
|
516
|
+
- [ ] Pagination implemented for list endpoints (cursor or offset)
|
|
517
|
+
- [ ] Authentication required (or explicitly marked as public)
|
|
518
|
+
- [ ] Authorization checked (user can only access their own resources)
|
|
519
|
+
- [ ] Rate limiting configured
|
|
520
|
+
- [ ] Response does not leak internal details (stack traces, SQL errors)
|
|
521
|
+
- [ ] Consistent naming with existing endpoints (camelCase vs snake_case)
|
|
522
|
+
- [ ] Documented (OpenAPI/Swagger spec updated)
|