Skip to content

Commit 371d8cc

Browse files
committed
add paging
Signed-off-by: Kirill Mokevnin <[email protected]>
1 parent 17d27f4 commit 371d8cc

File tree

8 files changed

+37
-47
lines changed

8 files changed

+37
-47
lines changed

Diff for: Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ types-to-openapi:
1919
types-to-typebox:
2020
npx openapi-box ./tsp-output/@typespec/openapi3/openapi.json
2121

22+
types: types-to-openapi types-to-typebox
23+
2224
tsp-build:
2325

2426
.PHONY: test

Diff for: main.tsp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ model User {
2929
@route("/users")
3030
namespace users {
3131
@get
32-
op index(): {
32+
op index(@query page?: numeric = 1): {
3333
@body users: {
3434
data: User[];
3535
};

Diff for: routes/users.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ export default async function (fastify) {
1111

1212
fastify.get(
1313
'/users',
14-
async function () {
15-
const users = await db.query.users.findMany()
14+
{
15+
schema: {
16+
querystring: schema['/users'].GET.args.properties.query,
17+
},
18+
},
19+
async function (request) {
20+
const perPage = 10
21+
const users = await db.query
22+
.users
23+
.findMany({
24+
limit: (perPage),
25+
offset: (request.query.page - 1) * perPage,
26+
})
1627

1728
return users
1829
})

Diff for: schema.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ const ComponentsSchemasNotFoundError = T.Object({
103103
const schema = {
104104
'/users': {
105105
GET: {
106-
args: T.Void(),
106+
args: T.Optional(
107+
T.Object({
108+
query: T.Optional(
109+
T.Object({
110+
page: T.Optional(T.Number({ default: 1, 'x-in': 'query' }))
111+
})
112+
)
113+
})
114+
),
107115
data: T.Object(
108116
{
109117
data: T.Array(CloneType(ComponentsSchemasUser))

Diff for: tsp-output/@typespec/json-schema/NotFoundError.json

-18
This file was deleted.

Diff for: tsp-output/@typespec/json-schema/User.json

-21
This file was deleted.

Diff for: tsp-output/@typespec/openapi3/openapi.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99
"/users": {
1010
"get": {
1111
"operationId": "users_index",
12-
"parameters": [],
12+
"parameters": [
13+
{
14+
"name": "page",
15+
"in": "query",
16+
"required": false,
17+
"schema": {
18+
"type": "number",
19+
"default": 1
20+
},
21+
"explode": false
22+
}
23+
],
1324
"responses": {
1425
"200": {
1526
"description": "The request has succeeded.",

Diff for: tspconfig.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
emit:
22
- "@typespec/openapi3"
3-
- "@typespec/json-schema"
43

54
options:
65
"@typespec/openapi3":
76
file-type: json
8-
"@typespec/json-schema":
9-
file-type: json

0 commit comments

Comments
 (0)