File tree 6 files changed +80
-23
lines changed
theme/components/Landingpage
6 files changed +80
-23
lines changed Original file line number Diff line number Diff line change @@ -95,21 +95,50 @@ get /api/products {
95
95
96
96
### Path Parameters
97
97
98
+ Path parameters allow you to capture values from the URL path. The syntax similar to ` query {} ` block, you can add type validators to each parameter:
99
+
98
100
``` py
99
- get / api/ users/ < id : int > {
101
+ get / api/ users/ : id {
100
102
"""
101
103
Get user by ID
102
-
103
- Retrieves detailed information about a specific user.
104
-
105
- Parameters:
106
- id: The unique identifier of the user
107
- """
104
+ ""
105
+
106
+ path {
107
+ """ User ID (must be a valid string) """
108
+ id: str
109
+ }
108
110
109
111
// Route implementation
110
112
}
111
113
```
112
114
115
+ You can combine multiple path parameters and add type validation:
116
+
117
+ ```py
118
+ get /users/:userId/posts/:postId {
119
+ """
120
+ Get user' s post
121
+ """
122
+
123
+ path {
124
+ """ User ID (alphanumeric with minimum length)"""
125
+ @string(min_len=3)
126
+ userId: str,
127
+
128
+ """ Post ID (must be a positive integer)"""
129
+ @number(min=1)
130
+ postId: int
131
+ }
132
+
133
+ // Implementation example
134
+ print(f"Fetching post {path.postId} for user {path.userId} ");
135
+ return {
136
+ "userId": path.userId,
137
+ "postId": path.postId
138
+ };
139
+ }
140
+ ```
141
+
113
142
### Request Body
114
143
115
144
```py
@@ -139,7 +168,7 @@ post /api/products {
139
168
You can document the responses a route might return:
140
169
141
170
```py
142
- get / api/ orders/ < id : int > {
171
+ get /api/orders/:id {
143
172
"""
144
173
Get order details
145
174
@@ -150,6 +179,10 @@ get /api/orders/<id:int> {
150
179
404 : Order not found
151
180
403 : Unauthorized access
152
181
"""
182
+
183
+ path {
184
+ id: str
185
+ }
153
186
154
187
// Route implementation
155
188
}
@@ -194,7 +227,7 @@ get /api/users {
194
227
}
195
228
196
229
@docs(tag="Users")
197
- get / api/ users/ < id : int > {
230
+ get /api/users/:id {
198
231
""" Get user by ID """
199
232
}
200
233
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ get /api/users {
67
67
// Return all users
68
68
}
69
69
70
- get / api/ users/ < id : int > {
70
+ get / api/ users/ : id {
71
71
// Return user by ID
72
72
}
73
73
Original file line number Diff line number Diff line change @@ -142,7 +142,11 @@ get /outdated {
142
142
### Conditional Response
143
143
144
144
``` js
145
- get / api/ users/ < id: int> {
145
+ get / api/ users/ : id {
146
+ path {
147
+ id: str
148
+ }
149
+
146
150
use std .db .pg ;
147
151
148
152
let user = pg .query_one (" SELECT * FROM users WHERE id = $1" , id);
Original file line number Diff line number Diff line change @@ -22,13 +22,13 @@ verb path [, verb path...] {
22
22
23
23
The ` verb ` is the HTTP method:
24
24
25
- | Verb | Description |
26
- | --- | --- |
27
- | ` get ` | GET request |
28
- | ` post ` | POST request |
29
- | ` put ` | PUT request |
25
+ | Verb | Description |
26
+ | -------- | ----------- --- |
27
+ | ` get ` | GET request |
28
+ | ` post ` | POST request |
29
+ | ` put ` | PUT request |
30
30
| ` delete ` | DELETE request |
31
- | ` head ` | HEAD request |
31
+ | ` head ` | HEAD request |
32
32
33
33
The ` path ` is the URL path.
34
34
@@ -174,8 +174,12 @@ get /hello2 {
174
174
## Path Parameters
175
175
176
176
``` py
177
- get / hello/ < name:str > {
178
- return " Hello, {name} !" ;
177
+ get / hello/ :name {
178
+ path {
179
+ name: str
180
+ }
181
+
182
+ return f " Hello, { name} ! " ;
179
183
}
180
184
```
181
185
@@ -217,7 +221,11 @@ Value is greater than 10
217
221
## Query Database with SQL
218
222
219
223
``` py
220
- get / tweet/ < id : int > {
224
+ get / tweet/ :id {
225
+ path {
226
+ id : str
227
+ }
228
+
221
229
use std.db.pg;
222
230
223
231
return pg.query(" SELECT * FROM tweet WHERE id = $1" , id );
Original file line number Diff line number Diff line change @@ -242,7 +242,11 @@ post /login {
242
242
### No Content Response
243
243
244
244
``` js
245
- delete / api/ resource/ < id: int> {
245
+ delete / api/ resource/ : id {
246
+ path {
247
+ id: str
248
+ }
249
+
246
250
delete_resource (id);
247
251
return response (status_code= 204 ); // No content
248
252
}
Original file line number Diff line number Diff line change @@ -156,8 +156,12 @@ web:
156
156
}
157
157
filename: post.ai
158
158
- code : |
159
- put /repo/<id:int> {
159
+ put /repo/:id {
160
160
"""Update a repository"""
161
+ path {
162
+ """The repository id"""
163
+ id: int
164
+ }
161
165
162
166
@json
163
167
body {
@@ -170,9 +174,13 @@ web:
170
174
}
171
175
filename: put.ai
172
176
- code : |
173
- delete /repo/<id:int> {
177
+ delete /repo/:id {
174
178
"""Delete a repository"""
175
179
180
+ path {
181
+ id: str
182
+ }
183
+
176
184
return f"delete repo: {id}";
177
185
}
178
186
filename: delete.ai
You can’t perform that action at this time.
0 commit comments