Exposing types
When using types and interfaces in your routes, Kita automatically detects and generates a JSON Schema for them. However, there is a rule you must follow to ensure that types and interfaces are exposed in the OpenAPI.
WARNING
The following concept applies to any type or interface used by Kita, not just for body types, as shown in the example.
Consider these two example routes:
ts
import type { Body } from '@kitajs/runtime';
interface PostBody {
name: string;
}
export function post(content: Body<PostBody>) {
return true;
}1
ts
import type { Body } from '@kitajs/runtime';
export interface PutBody {
age: number;
}
export function put(content: Body<PutBody>) {
return true;
}1
The only difference between the PUT and POST routes is that one exports the used interface and the other does not, respectively.
See how this affects the generated OpenAPI:
json
{
// Components does not contain the `PostBody` type
"components": { "schemas": {} },
"paths": {
"/": {
"post": {
"operationId": "postIndex",
"requestBody": {
"content": {
"application/json": {
// The body type was inlined here.
"schema": {
"additionalProperties": false,
"properties": { "name": { "type": "string" } },
"required": ["name"],
"type": "object"
}
}
},
"required": true
},
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "boolean" } }
}
}
}
}
}
}
}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
32
33
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
32
33
json
{
// Components contains the `PutBody` type
"components": {
"schemas": {
"PutBody": {
"additionalProperties": false,
"properties": { "age": { "type": "number" } },
"required": ["age"],
"type": "object"
}
}
},
"paths": {
"/": {
"put": {
"operationId": "putIndex",
"requestBody": {
"content": {
"application/json": {
// The body type is a reference to the `PutBody` type
"schema": { "$ref": "#/components/schemas/PutBody" }
}
}
},
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "boolean" } }
}
}
}
}
}
}
}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
32
33
34
35
36
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
32
33
34
35
36
As you can see, the generated OpenAPI for the POST route does not contain the PostBody interface, while the PUT route contains the PutBody interface.