Skip to content

Commit 951a1de

Browse files
committed
style(docs): expand code examples to multi-line literals
- Normalize description quoting and table padding in touched files - Reformat single-line object and call literals to multi-line
1 parent 2d3ff01 commit 951a1de

86 files changed

Lines changed: 1094 additions & 374 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/by-design/bearer-auth.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ const bearer = WrapMware('Bearer', async (ctx: Context, next) => {
102102

103103
// Apply the guard and error shape
104104
router.use(bearer)
105-
router.catch((ctx, err) => ctx.send.json({ error: err.error?.message }, { status: 401 }))
105+
router.catch((ctx, err) => {
106+
return ctx.send.json(
107+
{
108+
error: err.error?.message
109+
},
110+
{
111+
status: 401
112+
}
113+
)
114+
})
106115
```
107116

108117
This is the same wrapping pattern [Basic Auth](/middleware/basic-auth) uses internally, now carrying a token check instead of a credential compare.

docs/by-design/cache.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ export async function GET(ctx: Context): Promise<Response> {
3434
// Serve the cached value when present
3535
const hit = cache.get(key)
3636
if (hit !== undefined) {
37-
return ctx.send.json({ source: 'cache', data: hit })
37+
return ctx.send.json({
38+
source: 'cache',
39+
data: hit
40+
})
3841
}
3942

4043
// Build it once, then store for next time
4144
const data = await buildExpensiveData()
4245
cache.set(key, data)
43-
return ctx.send.json({ source: 'fresh', data })
46+
return ctx.send.json({
47+
source: 'fresh',
48+
data
49+
})
4450
}
4551

4652
declare function buildExpensiveData(): Promise<unknown>
@@ -63,13 +69,22 @@ export function GET(ctx: Context): Response {
6369
6470
// Fresh entry wins, expired one is dropped
6571
if (entry && Date.now() < entry.expiresAt) {
66-
return ctx.send.json({ source: 'cache', data: entry.value })
72+
return ctx.send.json({
73+
source: 'cache',
74+
data: entry.value
75+
})
6776
}
6877
6978
// Recompute and stamp a new expiry
7079
const value = { time: Date.now() }
71-
cache.set(key, { value, expiresAt: Date.now() + ttlMs })
72-
return ctx.send.json({ source: 'fresh', data: value })
80+
cache.set(key, {
81+
value,
82+
expiresAt: Date.now() + ttlMs
83+
})
84+
return ctx.send.json({
85+
source: 'fresh',
86+
data: value
87+
})
7388
}
7489
```
7590

docs/by-design/compress.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ import type { Context } from '@neabyte/deserve'
4242
// ---cut---
4343
export function GET(ctx: Context): Response {
4444
// Send plain, runtime compresses when able
45-
return ctx.send.json({ message: 'compressed by the runtime' })
45+
return ctx.send.json({
46+
message: 'compressed by the runtime'
47+
})
4648
}
4749
```
4850

@@ -56,7 +58,9 @@ import type { Context } from '@neabyte/deserve'
5658
export function GET(ctx: Context): Response {
5759
// Block any layer from rewriting the body
5860
ctx.setHeader('Cache-Control', 'no-transform')
59-
return ctx.send.json({ message: 'sent verbatim' })
61+
return ctx.send.json({
62+
message: 'sent verbatim'
63+
})
6064
}
6165
```
6266

docs/by-design/https-redirect.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ import type { Context } from '@neabyte/deserve'
4646
export function GET(ctx: Context): Response {
4747
// Scheme the client actually used
4848
const proto = ctx.header('x-forwarded-proto') ?? 'http'
49-
return ctx.send.json({ secure: proto === 'https' })
49+
return ctx.send.json({
50+
secure: proto === 'https'
51+
})
5052
}
5153
```
5254

docs/by-design/method-override.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,42 @@ import type { Context } from '@neabyte/deserve'
2929
// ---cut---
3030
// Read one item by id
3131
export function GET(ctx: Context): Response {
32-
return ctx.send.json({ id: ctx.param('id') })
32+
return ctx.send.json({
33+
id: ctx.param('id')
34+
})
3335
}
3436

3537
// Replace the item
3638
export function PUT(ctx: Context): Response {
37-
return ctx.send.json({ updated: true })
39+
return ctx.send.json({
40+
updated: true
41+
})
3842
}
3943

4044
// Remove the item
4145
export function DELETE(ctx: Context): Response {
42-
return ctx.send.json({ deleted: true })
46+
return ctx.send.json({
47+
deleted: true
48+
})
4349
}
4450
```
4551

4652
The client targets each one by sending the matching method. See [file-based routing](/core-concepts/file-based-routing) for how a file maps to a route.
4753

4854
```typescript twoslash
4955
// Each call hits its own handler
50-
await fetch('/items/42', { method: 'PUT' })
51-
await fetch('/items/42', { method: 'DELETE' })
56+
await fetch(
57+
'/items/42',
58+
{
59+
method: 'PUT'
60+
}
61+
)
62+
await fetch(
63+
'/items/42',
64+
{
65+
method: 'DELETE'
66+
}
67+
)
5268
```
5369

5470
Building stateless or stateful is the same move, just drop the files. A stateless REST endpoint is a handler that reads the request and replies, while a stateful flow adds the [session middleware](/middleware/session) and reads per-user data from [`ctx.state`](/core-concepts/context-object#sharing-state). The method stays real either way, with nothing to disguise on the way in.

docs/by-design/pretty-json.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import type { Context } from '@neabyte/deserve'
2121
// ---cut---
2222
export function GET(ctx: Context): Response {
2323
// Compact body, no extra whitespace
24-
return ctx.send.json({ id: 1, name: 'Alice' })
24+
return ctx.send.json({
25+
id: 1,
26+
name: 'Alice'
27+
})
2528
}
2629
```
2730

@@ -36,11 +39,20 @@ import type { Context } from '@neabyte/deserve'
3639
// ---cut---
3740
export function GET(ctx: Context): Response {
3841
// Indent on purpose for this route
39-
const body = JSON.stringify({ id: 1, name: 'Alice' }, null, 2)
42+
const body = JSON.stringify(
43+
{
44+
id: 1,
45+
name: 'Alice'
46+
},
47+
null,
48+
2
49+
)
4050

4151
// Send as text, keep JSON type
4252
return ctx.send.text(body, {
43-
headers: { 'Content-Type': 'application/json' }
53+
headers: {
54+
'Content-Type': 'application/json'
55+
}
4456
})
4557
}
4658
```

docs/by-design/rate-limit.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ router.use(async (ctx, next) => {
4646

4747
// Fresh window when missing or expired
4848
if (!entry || now > entry.resetAt) {
49-
hits.set(key, { count: 1, resetAt: now + windowMs })
49+
hits.set(key, {
50+
count: 1,
51+
resetAt: now + windowMs
52+
})
5053
return await next()
5154
}
5255

@@ -57,7 +60,12 @@ router.use(async (ctx, next) => {
5760
if (entry.count > maxRequests) {
5861
const retryAfter = Math.ceil((entry.resetAt - now) / 1000)
5962
ctx.setHeader('Retry-After', String(retryAfter))
60-
return ctx.send.text('Too Many Requests', { status: 429 })
63+
return ctx.send.text(
64+
'Too Many Requests',
65+
{
66+
status: 429
67+
}
68+
)
6169
}
6270

6371
// Still under the cap, continue
@@ -89,7 +97,10 @@ router.use(async (ctx, next) => {
8997

9098
// Start a fresh window when needed
9199
if (!entry || now > entry.resetAt) {
92-
entry = { count: 0, resetAt: now + windowMs }
100+
entry = {
101+
count: 0,
102+
resetAt: now + windowMs
103+
}
93104
hits.set(key, entry)
94105
}
95106

@@ -105,7 +116,14 @@ router.use(async (ctx, next) => {
105116

106117
// Block once the cap is passed
107118
if (entry.count > maxRequests) {
108-
return ctx.send.json({ error: 'Too Many Requests' }, { status: 429 })
119+
return ctx.send.json(
120+
{
121+
error: 'Too Many Requests'
122+
},
123+
{
124+
status: 429
125+
}
126+
)
109127
}
110128

111129
return await next()
@@ -127,7 +145,14 @@ declare function isOverLimit(key: string): boolean
127145
router.use('/auth', async (ctx, next) => {
128146
const key = ctx.ip ?? 'unknown'
129147
if (isOverLimit(key)) {
130-
return ctx.send.json({ error: 'Slow down' }, { status: 429 })
148+
return ctx.send.json(
149+
{
150+
error: 'Slow down'
151+
},
152+
{
153+
status: 429
154+
}
155+
)
131156
}
132157
return await next()
133158
})

docs/by-design/request-id.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ For correlating logs, the [lifecycle events](/middleware/observability/overview)
3333
```typescript twoslash
3434
import { Router } from '@neabyte/deserve'
3535

36-
const router = new Router({ routesDir: './routes' })
36+
const router = new Router({
37+
routesDir: './routes'
38+
})
3739
// ---cut---
3840
router.on((event) => {
3941
// Correlate by real IP and time

docs/by-design/server-timing.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Every `request:complete` event carries `durationMs`, the measured time for the w
1919
```typescript twoslash
2020
import { Router } from '@neabyte/deserve'
2121

22-
const router = new Router({ routesDir: './routes' })
22+
const router = new Router({
23+
routesDir: './routes'
24+
})
2325
// ---cut---
2426
router.on((event) => {
2527
// Read the measured request duration

docs/by-design/tracing.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ This listener turns each finished request into a span-shaped record and hands it
4141
```typescript twoslash
4242
import { Router } from '@neabyte/deserve'
4343

44-
const router = new Router({ routesDir: './routes' })
44+
const router = new Router({
45+
routesDir: './routes'
46+
})
4547
declare function exportSpan(span: Record<string, unknown>): void
4648
// ---cut---
4749
router.on((event) => {

0 commit comments

Comments
 (0)