Skip to content

Commit 2cde267

Browse files
Zfinixnielsenko
andauthored
docs: Add documentation for basic routing, requests and responses (#223)
## Description This PR adds comprehensive documentation and examples for Relic's core concepts, including: - **Basic Routing Documentation**: Added complete guide (`03-basic-routing.md`) covering route definition, HTTP methods, handlers, and pipeline composition with working examples - **Requests Documentation**: Added comprehensive guide (`04-requests.md`) covering request properties, query parameters, type-safe headers, body parsing, and validation best practices - **Responses Documentation**: Added detailed guide (`05-responses.md`) covering response creation, status codes, headers, body types, and error handling patterns - **Enhanced Code Documentation**: Added extensive inline documentation to core classes including `Body`, `Request`, `Response`, `Headers`, `Router`, `Handler`, `Pipeline`, and `Middleware` ### Code Examples - **Basic Routing Example**: Created focused `basic_routing.dart` demonstrating fundamental HTTP routing patterns - **Comprehensive Example**: Completely refactored `requets_response_example.dart` with all documentation examples in a runnable format - **Cross-Referenced Examples**: All documentation examples are validated and present in the example files - **Inline Documentation**: Updated core library classes with practical examples and usage patterns ## Related Issues - Closes: ? ## Pre-Launch Checklist Please ensure that your PR meets the following requirements before submitting: - [x] This update focuses on a single feature or bug fix. (For multiple fixes, please submit separate PRs.) - [x] I have read and followed the [Dart Style Guide](https://dart.dev/guides/language/effective-dart/style) and formatted the code using [dart format](https://dart.dev/tools/dart-format). - [ ] I have referenced at least one issue this PR fixes or is related to. - [x] I have updated/added relevant documentation (doc comments with `///`), ensuring consistency with existing project documentation. - [x] I have added new tests to verify the changes. - [x] All existing and new tests pass successfully. - [x] I have documented any breaking changes below. ## Breaking Changes - [x] No breaking changes. --------- Co-authored-by: Kasper Overgård Nielsen <[email protected]>
1 parent e58c951 commit 2cde267

File tree

17 files changed

+1821
-320
lines changed

17 files changed

+1821
-320
lines changed

doc/outline.md

Lines changed: 295 additions & 227 deletions
Large diffs are not rendered by default.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# Basic Routing
6+
7+
_Routing_ refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
8+
9+
Each route can have one or more handler functions, which are executed when the route is matched.
10+
11+
Route definition takes the following structure:
12+
13+
```dart
14+
router.METHOD(PATH, HANDLER)
15+
```
16+
17+
Where:
18+
19+
- `router` is an instance of `Router<Handler>`.
20+
- `METHOD` is an HTTP request method, in lowercase.
21+
- `PATH` is a path on the server.
22+
- `HANDLER` is the function executed when the route is matched.
23+
24+
## The `add` method and syntactic sugar
25+
26+
At the core of routing is the `add` method:
27+
28+
```dart
29+
router.add(Method.get, '/', handler);
30+
```
31+
32+
The convenience methods like `.get()`, `.post()`, `.anyOf()`, and `.any()` are all syntactic sugar that call this underlying `add` method:
33+
34+
- `.get(path, handler)``.add(Method.get, path, handler)`
35+
- `.post(path, handler)``.add(Method.post, path, handler)`
36+
- `.anyOf({Method.get, Method.post}, path, handler)` → calls `.add()` for each method in the set
37+
- `.any(path, handler)` → calls `.anyOf()` with all HTTP methods
38+
39+
## Breaking Down the Routes
40+
41+
The following examples break down each route from the complete example above.
42+
43+
### Convenience methods (syntactic sugar)
44+
45+
These methods are syntactic sugar for the core `.add()` method:
46+
47+
**Respond with `Hello World!` on the homepage:**
48+
49+
```dart
50+
app.get('/', (ctx) {
51+
return ctx.respond(
52+
Response.ok(
53+
body: Body.fromString('Hello World!'),
54+
),
55+
);
56+
});
57+
```
58+
59+
**Respond to a POST request on the root route:**
60+
61+
```dart
62+
app.post('/', (ctx) {
63+
return ctx.respond(
64+
Response.ok(
65+
body: Body.fromString('Got a POST request'),
66+
),
67+
);
68+
});
69+
```
70+
71+
**Respond to a PUT request to the `/user` route:**
72+
73+
```dart
74+
app.put('/user', (ctx) {
75+
return ctx.respond(
76+
Response.ok(
77+
body: Body.fromString('Got a PUT request at /user'),
78+
),
79+
);
80+
});
81+
```
82+
83+
**Respond to a DELETE request to the `/user` route:**
84+
85+
```dart
86+
app.delete('/user', (ctx) {
87+
return ctx.respond(
88+
Response.ok(
89+
body: Body.fromString('Got a DELETE request at /user'),
90+
),
91+
);
92+
});
93+
```
94+
95+
### Using the `add` method
96+
97+
This is what the convenience methods call internally:
98+
99+
**Respond to a PATCH request using the core `.add()` method:**
100+
101+
```dart
102+
app.add(Method.patch, '/api', (ctx) {
103+
return ctx.respond(
104+
Response.ok(
105+
body: Body.fromString('Got a PATCH request at /api'),
106+
),
107+
);
108+
});
109+
```
110+
111+
### Using `anyOf` for Multiple Methods
112+
113+
Handle multiple HTTP methods with the same handler:
114+
115+
**Handle both GET and POST requests to `/admin`:**
116+
117+
```dart
118+
app.anyOf({Method.get, Method.post}, '/admin', (ctx) {
119+
final method = ctx.request.method.name.toUpperCase();
120+
return ctx.respond(Response.ok(body: Body.fromString('Admin page - $method request')));
121+
});
122+
```
123+
124+
## Examples
125+
126+
- **[`basic_routing.dart`](https://github.com/serverpod/relic/blob/main/example/basic_routing.dart)** - The complete working example from this guide

0 commit comments

Comments
 (0)