Skip to content

Commit 9402a2d

Browse files
committed
docs(rest-api): add descriptive comments to code examples ✏️
- Add validation comments to payload validation examples - Add semantic comments to HTTP method examples - Add error envelope comments to status code examples - Add URI design comments to resource-first examples - Add RESTful endpoint comments to API fundamentals
1 parent fca50e0 commit 9402a2d

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

DEEP-NOTES/Rest-API/api-fundamentals.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ You are usually in REST territory when:
6666
**Good snippet (uniform contract):**
6767

6868
```http
69+
# RESTful endpoints using proper HTTP methods
6970
GET /v1/prompts
7071
POST /v1/prompts
7172
PATCH /v1/prompts/{id}
@@ -75,6 +76,7 @@ DELETE /v1/prompts/{id}
7576
**Bad snippet (action endpoints):**
7677

7778
```http
79+
# Action-based endpoints violate REST principles
7880
POST /v1/getPromptsList
7981
POST /v1/removePromptById
8082
POST /v1/updatePromptById

DEEP-NOTES/Rest-API/http-semantics.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ Common API design decisions:
6767
**Good snippet (Express style):**
6868

6969
```ts
70+
// PUT endpoint - replace entire user resource
7071
app.put('/v1/users/:id', async (req, res) => {
7172
const user = await replaceUser(req.params.id, req.body)
7273
return res.status(200).json(user)
7374
})
7475

76+
// PATCH endpoint - partial user update
7577
app.patch('/v1/users/:id', async (req, res) => {
7678
const user = await updateUserPartial(req.params.id, req.body)
7779
return res.status(200).json(user)
@@ -81,8 +83,9 @@ app.patch('/v1/users/:id', async (req, res) => {
8183
**Bad snippet (semantic mismatch):**
8284

8385
```ts
86+
// Wrong: GET request that mutates server state
8487
app.get('/v1/logout', async (_req, res) => {
85-
await revokeSession()
88+
await revokeSession() // Mutation in GET violates HTTP semantics
8689
return res.status(200).json({ ok: true })
8790
})
8891
```

DEEP-NOTES/Rest-API/payload-validation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ You need strong validation in:
6161
**Good snippet (schema first):**
6262

6363
```ts
64+
// Validate request body using schema before processing
6465
const result = createPromptSchema.safeParse(req.body)
6566
if (!result.success) {
67+
// Return structured validation errors with 422 status
6668
return res.status(422).json({
6769
code: 'validation_failed',
6870
details: result.error.issues
@@ -74,9 +76,11 @@ if (!result.success) {
7476

7577
```ts
7678
try {
79+
// Direct database insert without validation - bad practice
7780
await db.prompts.insert(req.body)
7881
return res.status(201).json({ ok: true })
7982
} catch {
83+
// Generic error hides real validation issues
8084
return res.status(500).json({ message: 'Something went wrong' })
8185
}
8286
```

DEEP-NOTES/Rest-API/status-errors.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Critical response mapping areas:
7272

7373
```ts
7474
if (!token) {
75+
// Return 401 with structured error envelope
7576
return res.status(401).json({
7677
code: 'auth_missing',
7778
message: 'Authentication required',
@@ -84,6 +85,7 @@ if (!token) {
8485

8586
```ts
8687
if (!token) {
88+
// Wrong: 200 status hides the actual error condition
8789
return res.status(200).json({
8890
error: true,
8991
message: 'Unauthorized'
@@ -107,6 +109,7 @@ if (!token) {
107109
- Include `requestId` for tracing support tickets
108110

109111
```json
112+
// Structured error response with all required fields
110113
{
111114
"code": "validation_failed",
112115
"message": "Payload validation failed",

DEEP-NOTES/Rest-API/uri-design.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ You apply URI design on:
6060
**Good snippet (resource-first):**
6161

6262
```http
63+
// Resource-first URI with query parameters for filtering
6364
GET /v1/users/42/sessions?limit=20&offset=0
6465
```
6566

6667
**Bad snippet (action-first):**
6768

6869
```http
70+
// Action-first URI violates REST principles
6971
POST /v1/getUserSessionListById
7072
```
7173

0 commit comments

Comments
 (0)