-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tsp
125 lines (106 loc) · 2.6 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
import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi";
import "@typespec/openapi3";
using TypeSpec.Http;
using TypeSpec.OpenAPI;
scalar ID extends int32;
@error
model Error {
message: string;
}
alias HTTPSuccessStatusCode = 200 | 201;
alias HTTPErrorStatusCode = 400 | 401 | 404 | 422;
alias HTTPStatusCode = HTTPSuccessStatusCode | HTTPErrorStatusCode;
model ErrorResponse<S extends HTTPErrorStatusCode> {
@statusCode _: S;
@body body: Error;
}
model NotFound is ErrorResponse<404>;
model Unauthorized is ErrorResponse<401>;
model BadRequest is ErrorResponse<400>;
model UnprocessableEntity is ErrorResponse<422>;
model SuccessResponse<S extends HTTPSuccessStatusCode, T> {
@statusCode _: S;
@body body: T;
}
model OK<T> is SuccessResponse<200, T>;
model Created<T> is SuccessResponse<201, T>;
model Post {
id: ID;
title: string;
body: string;
published_at: utcDateTime | null;
}
model PostCreate {
title: string;
body: string;
}
model PostUpdate {
title?: string;
body?: string;
}
model Posts {
total: int32;
posts: Array<Post>;
}
@service({
title: "Blog API",
version: "2024-02-22",
})
@info({
contact: {
email: "user@host.example",
},
})
@server("http://localhost:3000", "Localhost")
@doc("API service to manage blog posts")
@useAuth(BasicAuth | BearerAuth)
namespace BlogService {
@route("posts")
@tag("post")
namespace PostEndpoints {
@get
@route("{id}")
@operationId("post-fetch")
@doc("Fetch a single post")
op fetch(@path id: ID): OK<Post> | NotFound;
@get
@operationId("post-list")
@doc("List all posts available")
op list(): OK<Posts> | Unauthorized;
@post
@operationId("post-create")
@doc("Create a new post")
op create(post: PostCreate):
| Created<Post>
| Unauthorized
| BadRequest
| UnprocessableEntity;
@patch
@route("{id}")
@operationId("post-update")
@doc("Update existing post")
op update(@path id: ID, post: PostUpdate):
| OK<Post>
| NotFound
| Unauthorized
| BadRequest
| UnprocessableEntity;
@delete
@route("{id}")
@operationId("post-destroy")
@doc("Destroy existing post")
op destroy(@path id: ID): OK<Post> | NotFound | Unauthorized;
@patch
@route("{id}/publish")
@operationId("post-publish")
@doc("Set publish date of post to now")
op publish(@path id: ID): OK<Post> | NotFound | Unauthorized;
@patch
@route("{id}/unpublish")
@operationId("post-unpublish")
@doc("Unset publish date of post")
op unpublish(@path id: ID): OK<Post> | NotFound | Unauthorized;
}
}