Skip to content

Commit 94f2bc2

Browse files
committed
Complete route, validator, directives docs
1 parent 2556344 commit 94f2bc2

File tree

3 files changed

+478
-86
lines changed

3 files changed

+478
-86
lines changed

docs/guide/web/route.md

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
# Route
1+
# Route and Endpoint
22

3-
**Route** is the main entrypoint that handles an HTTP request. All routes are defined in the `routes` directory.
3+
**Route** is the main entry point that handles an HTTP request in AIScript.
44

5-
## Define a route
5+
## Define An Endpoint
66

7-
Define a route is simple and straightforward. The syntax is:
7+
Defining an endpoint is simple and straightforward. The syntax is:
88

99
```
10-
verb path [(arg1: type1, arg2: type2, ...)] {
10+
verb path [, verb path...] {
11+
// query parameters
12+
query {
13+
// parameter definitions
14+
}
1115
16+
// request body
17+
body {
18+
// body field definitions
19+
}
1220
}
1321
```
1422

@@ -30,12 +38,14 @@ get /hello {
3038
}
3139
```
3240

33-
```
41+
```bash
3442
$ curl http://localhost:8080/hello
3543
Hello World
3644
```
3745

38-
## Parse query string
46+
## Parse Query String
47+
48+
Use the `query` block to declare fields in the query string. You can set a default value to make a parameter optional. To access query parameters, use the `query.name` or `query["name"]` syntax.
3949

4050
```py
4151
get /hello {
@@ -44,7 +54,7 @@ get /hello {
4454
name: str = "Alice"
4555
}
4656

47-
return "Hello, {name}!";
57+
return "Hello, " + query.name + "!";
4858
}
4959
```
5060

@@ -53,43 +63,47 @@ $ curl http://localhost:8080/hello?name=AIScript
5363
Hello, AIScript!
5464
```
5565

56-
Each field declared in the `query` block is a query parameter, the `str` is the type of the query parameter. You can also specify a default value for the query parameter.
66+
Each field declared in the `query` block is a query parameter, with `str` indicating the parameter type.
5767

5868
```bash
5969
$ curl http://localhost:8080/hello
6070
Hello, Alice!
6171
```
6272

63-
The `@string` is a validator. AIScript will validate the query parameter and return an error if the validation fails.
73+
The [@string](/reference/directives#string) is a validator. AIScript will validate the query parameter and return an error if validation fails.
6474

6575
```bash
66-
$ curl http://localhost:8080/hello?name=WS
76+
$ curl http://localhost:8080/hello?name=Le
6777
{
6878
"error": "Invalid query parameter: name, must be between 3 and 10 characters"
6979
}
7080
```
7181

72-
## Parse request body
82+
:::tip
83+
For more information about validators, see [Validator](./validator.md).
84+
:::
85+
86+
## Parse Request Body
7387

74-
```js
88+
```py
7589
post /hello {
7690
@json
7791
body {
7892
@string(min_len=3, max_len=10)
7993
name: str
8094
}
8195

82-
return "Hello, {name}!";
96+
return "Hello, " + body.name + "!";
8397
}
8498
```
8599

86-
```
100+
```bash
87101
$ curl -X POST -H "Content-Type: application/json" \
88102
-d '{"name": "AIScript"}' http://localhost:8080/hello
89103
Hello, AIScript!
90104
```
91105

92-
Each field declared in the `body` block is a request body parameter, the `str` is the type of the request body parameter. The `@json` directive tells AIScript to parse the request body as JSON. Another directive is `@form`, it tells AIScript to parse the request body as form data.
106+
Each field declared in the `body` block is a request body parameter, with `str` indicating the parameter type. The [@json](/reference/directives#json) directive tells AIScript to parse the request body as JSON. Alternatively, the [@form](/reference/directives#form) directive instructs AIScript to parse the request body as form data. Access body parameters using `body.name` or `body["name"]` syntax.
93107

94108
```py
95109
post /hello {
@@ -99,48 +113,48 @@ post /hello {
99113
name: str
100114
}
101115

102-
return "Hello, {name}!";
116+
return "Hello, " + body.name + "!";
103117
}
104118
```
105119

106-
```
120+
```bash
107121
$ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
108122
-d 'name=AIScript' http://localhost:8080/hello
109123
Hello, AIScript!
110124
```
111125

112-
## Header and cookie
126+
## Headers and Cookies
113127

114-
Access and set header and cookie is easy.
128+
Accessing and setting headers and cookies is straightforward using the `header` and `cookie` variables, which are request-scoped.
115129

116130
```py
117131
get /hello {
118132
let abc = header.abc;
119133
let xyz = cookie.xyz;
120134

121-
# set new header
135+
// Set new header
122136
header.test = "Test Header";
123-
# modify cookie
124-
cookie.xyz = "{xyz} v2";
137+
// Modify cookie
138+
cookie.xyz = "changed";
125139
return "header: abc={abc}, cookie: xyz={xyz}";
126140
}
127141
```
128142

129-
## Request object
143+
## Request Object
130144

131145
```py
132146
get /hello {
133147
let method = request.method;
134-
let url = request.url
135-
let path = request.path
136-
let scheme = request.scheme
137-
let host = request.host
138-
let port = request.port
139-
return "method: {method}, url: {url}, path: {path}, scheme: {scheme}, host: {host}, port: {port}"
148+
let url = request.url;
149+
let path = request.path;
150+
let scheme = request.scheme;
151+
let host = request.host;
152+
let port = request.port;
153+
return "method: {method}, url: {url}, path: {path}, scheme: {scheme}, host: {host}, port: {port}";
140154
}
141155
```
142156

143-
```
157+
```bash
144158
$ curl http://localhost:8080/hello
145159
method: GET, url: http://localhost:8080/hello, path: /hello, scheme: http, host: localhost, port: 8000
146160
```
@@ -149,22 +163,26 @@ method: GET, url: http://localhost:8080/hello, path: /hello, scheme: http, host:
149163

150164
```py
151165
get /hello {
152-
return temporaly_redirect("/hello2")
166+
return temporary_redirect("/hello2");
167+
}
168+
169+
get /hello2 {
170+
return "Hello, World!";
153171
}
154172
```
155173

156-
## Path parameter
174+
## Path Parameters
157175

158176
```py
159177
get /hello/<name:str> {
160178
return "Hello, {name}!";
161179
}
162180
```
163181

164-
## Multiple routes
182+
## Multiple Routes
165183

166-
```rust
167-
get /hello {
184+
```py
185+
get /hello, get /world {
168186
return "Hello, World!";
169187
}
170188

@@ -173,30 +191,30 @@ post /hello2 {
173191
}
174192
```
175193

176-
## Route programming
194+
## Route Programming
177195

178-
API routes normally never a simple text response, it often need programming logic to handle the request. In AIScript route, you can write any programming logic in the route just like other web frameworks in ohter languages.
196+
API routes typically require more than simple text responses; they often need programming logic to handle requests. In AIScript routes, you can write any programming logic just as you would in other web frameworks.
179197

180-
```rust
198+
```py
181199
get /hello {
182200
query {
183201
value: int
184202
}
185203

186-
if value > 10 {
204+
if query.value > 10 {
187205
return "Value is greater than 10";
188-
else {
206+
} else {
189207
return "Value is less than or equal to 10";
190208
}
191209
}
192210
```
193211

194-
```
212+
```bash
195213
$ curl http://localhost:8080/hello?value=11
196214
Value is greater than 10
197215
```
198216

199-
## Query database with SQL
217+
## Query Database with SQL
200218

201219
```py
202220
get /tweet/<id: int> {
@@ -205,4 +223,3 @@ get /tweet/<id: int> {
205223
return pg.query("SELECT * FROM tweet WHERE id = $1", id);
206224
}
207225
```
208-

0 commit comments

Comments
 (0)