Skip to content

Commit f4a84e0

Browse files
committed
update
Signed-off-by: Kirill Mokevnin <[email protected]>
1 parent 67455c7 commit f4a84e0

File tree

9 files changed

+998
-27
lines changed

9 files changed

+998
-27
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ types-to-typebox:
2727

2828
types: types-to-openapi types-to-typebox
2929

30+
mock:
31+
npx prism mock ./tsp-output/@typespec/openapi3/openapi.v1.json
32+
3033
tsp-build:
3134

3235
.PHONY: test routes

main.tsp

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@ import "@typespec/http";
22
import "@typespec/rest";
33
import "@typespec/versioning";
44
import "@typespec/openapi3";
5-
import "@typespec/json-schema";
65

76
using TypeSpec.Versioning;
87
using TypeSpec.Http;
98
using TypeSpec.Rest;
10-
using TypeSpec.JsonSchema;
119

1210
@service({
1311
title: "Hexlet Fastify Rest Api Example",
1412
})
15-
@server("https://localhost", "Single server endpoint")
16-
@jsonSchema
1713
@versioned(Versions)
1814
namespace FastifyRestApiExample;
1915

2016
enum Versions {
21-
v1,
17+
v1, v2
2218
}
2319

2420
/**
@@ -73,6 +69,7 @@ model User {
7369
@minLength(2)
7470
@maxLength(100)
7571
fullName: string | null;
72+
@added(Versions.v2) phone: string;
7673

7774
@format("email")
7875
email: string;
@@ -129,32 +126,35 @@ model CourseLessonCreateDTO {
129126
}
130127

131128
@route("/users")
132-
@useAuth(BearerAuth)
133129
namespace users {
134130
@get
131+
@useAuth(BearerAuth)
135132
op index(@query page?: numeric = 1): {
136133
@body users: {
137134
data: User[];
138135
};
139136
};
140137

141138
@get
139+
@useAuth(BearerAuth)
142140
op show(@path id: numeric): {
143-
@body user: User;
141+
@body _: User;
144142
} | NotFoundError;
145143

146144
@post
147-
op create(@body user: UserCreateDTO): {
148-
@body user: User;
145+
op create(@body _: UserCreateDTO): {
146+
@body _: User;
149147
@statusCode statusCode: 201;
150148
} | UnprocessableEntityError;
151149

152150
@patch
151+
@useAuth(BearerAuth)
153152
op update(@path id: numeric, @body user: UserEditDTO): {
154153
@body user: User;
155154
} | NotFoundError | UnprocessableEntityError;
156155

157156
@delete
157+
@useAuth(BearerAuth)
158158
op destroy(@path id: numeric): {
159159
@statusCode statusCode: 204;
160160
} | NotFoundError;

policies/Course.js

Whitespace-only changes.

policies/CoursePolicy.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default class CoursePolicy {
2+
canShowIndex(creatorId, user) {
3+
// Проверка
4+
}
5+
}

routes/api/users.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export default async function (fastify) {
2828
...getPagingOptions(page, 1),
2929
})
3030

31-
return users
31+
// return users
32+
return { data: users, meta: {} }
3233
})
3334

3435
fastify.get(
@@ -49,7 +50,6 @@ export default async function (fastify) {
4950
fastify.post(
5051
'/users',
5152
{
52-
onRequest: [fastify.authenticate],
5353
schema: {
5454
body: schema['/users'].POST.args.properties.body,
5555
response: {

schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ const _components = {
490490
User: CloneType(ComponentsSchemasUser),
491491
UserCreateDTO: CloneType(ComponentsSchemasUserCreateDto),
492492
UserEditDTO: CloneType(ComponentsSchemasUserEditDto),
493-
Versions: T.Literal('v1')
493+
Versions: T.Union([T.Literal('v1'), T.Literal('v2')])
494494
}
495495
}
496496

serializers/UserSerializer.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default class UserSerializer {
2+
index(users) {
3+
return { data: users, meta: {} }
4+
}
5+
}

tsp-output/@typespec/openapi3/openapi.v1.json

+4-15
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,7 @@
506506
}
507507
}
508508
}
509-
},
510-
"security": [
511-
{
512-
"BearerAuth": []
513-
}
514-
]
509+
}
515510
}
516511
},
517512
"/users/{id}": {
@@ -962,7 +957,8 @@
962957
"Versions": {
963958
"type": "string",
964959
"enum": [
965-
"v1"
960+
"v1",
961+
"v2"
966962
]
967963
}
968964
},
@@ -972,12 +968,5 @@
972968
"scheme": "bearer"
973969
}
974970
}
975-
},
976-
"servers": [
977-
{
978-
"url": "https://localhost",
979-
"description": "Single server endpoint",
980-
"variables": {}
981-
}
982-
]
971+
}
983972
}

0 commit comments

Comments
 (0)