-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tsp
230 lines (194 loc) · 3.88 KB
/
main.tsp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import "@typespec/http";
import "@typespec/rest";
import "@typespec/versioning";
import "@typespec/openapi3";
using TypeSpec.Versioning;
using TypeSpec.Http;
using TypeSpec.Rest;
@service({
title: "Hexlet Fastify Rest Api Example",
})
@versioned(Versions)
namespace FastifyRestApiExample;
enum Versions {
v1, v2
}
/**
* https://www.rfc-editor.org/rfc/rfc9457.html
*/
@error
model ProblemDetails {
@header("content-type") contentType: "application/problem+json";
type?: string;
title?: string;
status?: integer;
detail?: string;
instance?: string;
}
@error
model NotFoundError {
@statusCode _: 404;
...ProblemDetails;
}
@error
model UnauthorizedError {
@statusCode _: 401;
...ProblemDetails;
}
@error
model ForbiddenError {
@statusCode _: 403;
...ProblemDetails;
}
@error
model UnprocessableEntityError {
@statusCode _: 422;
...ProblemDetails;
errors: Array<{
message: string;
rule: string;
field: string;
}>;
}
model Timestamps {
createdAt: utcDateTime;
}
model User {
id: numeric;
@minLength(2)
@maxLength(100)
fullName: string | null;
@added(Versions.v2) phone: string;
@format("email")
email: string;
...Timestamps;
}
model UserCreateDTO {
fullName?: string;
email: string;
}
model UserEditDTO {
fullName?: string;
}
model Course {
id: numeric;
name: string;
description: string;
...Timestamps;
}
model CourseCreateDTO {
name: string;
description: string;
}
model CourseEditDTO {
name?: string;
description?: string;
}
model CourseLesson {
id: numeric;
courseId: numeric;
name: string;
body: string;
...Timestamps;
}
model AuthInfo {
email: string;
password: string;
}
model TokenInfo {
token: string;
}
model CourseLessonCreateDTO {
name: string;
body: string;
}
@route("/users")
namespace users {
@get
@useAuth(BearerAuth)
op index(@query page?: numeric = 1): {
@body users: {
data: User[];
};
};
@get
@useAuth(BearerAuth)
op show(@path id: numeric): {
@body _: User;
} | NotFoundError;
@post
op create(@body _: UserCreateDTO): {
@body _: User;
@statusCode statusCode: 201;
} | UnprocessableEntityError;
@patch
@useAuth(BearerAuth)
op update(@path id: numeric, @body user: UserEditDTO): {
@body user: User;
} | NotFoundError | UnprocessableEntityError;
@delete
@useAuth(BearerAuth)
op destroy(@path id: numeric): {
@statusCode statusCode: 204;
} | NotFoundError;
}
@route("/tokens")
namespace tokens {
@post
op create(@body auth_info: AuthInfo): {
@body token: TokenInfo;
@statusCode statusCode: 201;
} | UnprocessableEntityError;
}
@route("/courses")
namespace courses {
@get
op index(@query page?: numeric = 1): {
@body courses: {
data: Course[];
};
};
@get
op show(@path id: numeric): {
@body course: Course;
} | NotFoundError;
@post
@useAuth(BearerAuth)
op create(@body course: CourseCreateDTO): {
@body course: Course;
@statusCode statusCode: 201;
} | UnprocessableEntityError;
@patch
@useAuth(BearerAuth)
op update(@path id: numeric, @body course: CourseEditDTO):
| {
@body course: Course;
}
| NotFoundError
| UnprocessableEntityError
| UnauthorizedError;
@delete
@useAuth(BearerAuth)
op destroy(@path id: numeric): {
@statusCode statusCode: 204;
} | NotFoundError | UnauthorizedError;
}
@route("/courses/{courseId}/lessons")
namespace courses_lessons {
@get
op index(@path courseId: numeric, @query page?: numeric = 1): {
@body lessons: {
data: CourseLesson[];
};
};
@get
op show(@path courseId: numeric, @path id: numeric): {
@body lessons: CourseLesson;
} | NotFoundError;
@post
@useAuth(BearerAuth)
op create(@path courseId: numeric, @body course: CourseLessonCreateDTO): {
@body lesson: CourseLesson;
@statusCode statusCode: 201;
} | UnprocessableEntityError | UnauthorizedError;
}