Path
Routes defined with /[variable]
are automatically registered as path parameter routes. The value of the variable is passed as an argument to the parameter with the type Path
.
ts
import type { Path } from '@kitajs/runtime';
export function get(username: Path) {
return `Hello, ${username}!`;
}
1
json
{
"paths": {
"/{username}": {
"get": {
"operationId": "getUsername",
"parameters": [
{
"schema": { "type": "string" },
"in": "path",
"name": "username",
"required": true
}
],
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Multiple path parameters
You can also combine multiple path parameters.
ts
import type { Path } from '@kitajs/runtime';
export function get(username: Path, id: Path) {
return `Hello, ${username}! Your ID is ${id}.`;
}
1
json
{
"paths": {
"/{username}/{id}": {
"get": {
"operationId": "getUsernameId",
"parameters": [
{
"schema": { "type": "string" },
"in": "path",
"name": "id",
"required": true
},
{
"schema": { "type": "string" },
"in": "path",
"name": "username",
"required": true
}
],
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Custom names
Or even use a custom name for the path parameter.
ts
import type { Path } from '@kitajs/runtime';
export function get(user: Path<'username'>) {
return `Hello, ${user}!`;
}
1
json
{
"paths": {
"/{username}": {
"get": {
"operationId": "getUsername",
"parameters": [
{
"schema": { "type": "string", "enum": ["username"] },
"in": "path",
"name": "user",
"required": true
}
],
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Wildcard routes
You can define a wildcard route by naming your file as [...].ts
.
You can use Path<string, '*'>
to capture the remaining path.
ts
import type { Path } from '@kitajs/runtime';
export function get(remaining: Path<string, '*'>) {
return `Wildcard route of: ${remaining}`;
}
1
json
{
"paths": {
"/{*}": {
"get": {
"operationId": "getWildcard",
"parameters": [
{
"schema": { "type": "string" },
"in": "path",
"name": "*",
"required": true
}
],
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25