You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/by-design/method-override.md
+21-5Lines changed: 21 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,26 +29,42 @@ import type { Context } from '@neabyte/deserve'
29
29
// ---cut---
30
30
// Read one item by id
31
31
exportfunction GET(ctx:Context):Response {
32
-
returnctx.send.json({ id: ctx.param('id') })
32
+
returnctx.send.json({
33
+
id: ctx.param('id')
34
+
})
33
35
}
34
36
35
37
// Replace the item
36
38
exportfunction PUT(ctx:Context):Response {
37
-
returnctx.send.json({ updated: true })
39
+
returnctx.send.json({
40
+
updated: true
41
+
})
38
42
}
39
43
40
44
// Remove the item
41
45
exportfunction DELETE(ctx:Context):Response {
42
-
returnctx.send.json({ deleted: true })
46
+
returnctx.send.json({
47
+
deleted: true
48
+
})
43
49
}
44
50
```
45
51
46
52
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.
47
53
48
54
```typescript twoslash
49
55
// Each call hits its own handler
50
-
awaitfetch('/items/42', { method: 'PUT' })
51
-
awaitfetch('/items/42', { method: 'DELETE' })
56
+
awaitfetch(
57
+
'/items/42',
58
+
{
59
+
method: 'PUT'
60
+
}
61
+
)
62
+
awaitfetch(
63
+
'/items/42',
64
+
{
65
+
method: 'DELETE'
66
+
}
67
+
)
52
68
```
53
69
54
70
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.
0 commit comments