Skip to content

Commit a3be8ea

Browse files
committed
docs(rest-api): cleanup and document code examples 🌐
- Add JSDoc to HTTP semantics code examples - Document payload validation functions - Remove unnecessary comments from status-errors - Update URI design examples with documentation
1 parent c86cc1c commit a3be8ea

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

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

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

6969
```ts
70-
// PUT endpoint - replace entire user resource
70+
/**
71+
* Replace user resource completely.
72+
* @description Performs full update of user data.
73+
* @param req - Express request object
74+
* @param res - Express response object
75+
*/
7176
app.put('/v1/users/:id', async (req, res) => {
7277
const user = await replaceUser(req.params.id, req.body)
7378
return res.status(200).json(user)
7479
})
7580

76-
// PATCH endpoint - partial user update
81+
/**
82+
* Partially update user resource.
83+
* @description Applies partial modifications to user.
84+
* @param req - Express request object
85+
* @param res - Express response object
86+
*/
7787
app.patch('/v1/users/:id', async (req, res) => {
7888
const user = await updateUserPartial(req.params.id, req.body)
7989
return res.status(200).json(user)
@@ -83,9 +93,14 @@ app.patch('/v1/users/:id', async (req, res) => {
8393
**Bad snippet (semantic mismatch):**
8494

8595
```ts
86-
// Wrong: GET request that mutates server state
96+
/**
97+
* Mutate server state incorrectly.
98+
* @description Demonstrates anti-pattern of state mutation in GET.
99+
* @param _req - Express request object unused
100+
* @param res - Express response object
101+
*/
87102
app.get('/v1/logout', async (_req, res) => {
88-
await revokeSession() // Mutation in GET violates HTTP semantics
103+
await revokeSession()
89104
return res.status(200).json({ ok: true })
90105
})
91106
```

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

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

6363
```ts
64-
// Validate request body using schema before processing
64+
/**
65+
* Validate request against schema.
66+
* @description Parses and validates incoming request body.
67+
* @param req - Express request with body
68+
* @param res - Express response object
69+
*/
6570
const result = createPromptSchema.safeParse(req.body)
6671
if (!result.success) {
67-
// Return structured validation errors with 422 status
6872
return res.status(422).json({
6973
code: 'validation_failed',
7074
details: result.error.issues
@@ -76,11 +80,9 @@ if (!result.success) {
7680

7781
```ts
7882
try {
79-
// Direct database insert without validation - bad practice
8083
await db.prompts.insert(req.body)
8184
return res.status(201).json({ ok: true })
8285
} catch {
83-
// Generic error hides real validation issues
8486
return res.status(500).json({ message: 'Something went wrong' })
8587
}
8688
```

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

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

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

8685
```ts
8786
if (!token) {
88-
// Wrong: 200 status hides the actual error condition
8987
return res.status(200).json({
9088
error: true,
9189
message: 'Unauthorized'
@@ -109,7 +107,6 @@ if (!token) {
109107
- Include `requestId` for tracing support tickets
110108

111109
```json
112-
// Structured error response with all required fields
113110
{
114111
"code": "validation_failed",
115112
"message": "Payload validation failed",

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ You apply URI design on:
5959

6060
**Good snippet (resource-first):**
6161

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

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

70+
Action-first URI violates REST principles:
71+
6972
```http
70-
// Action-first URI violates REST principles
7173
POST /v1/getUserSessionListById
7274
```
7375

0 commit comments

Comments
 (0)