@@ -7,25 +7,53 @@ An experimental AWS Constructs based Open API schema builder
77``` typescript
88import {
99 Api ,
10- Info ,
1110 OpenApiVersion ,
11+ Parameter ,
1212 Path ,
13+ Reference ,
14+ Response ,
1315 Schema ,
16+ SecurityRequirement ,
17+ SecurityScheme ,
18+ Server ,
19+ Tag ,
20+ HttpMethods ,
1421} from ' @block65/openapi-constructs' ;
1522
1623const api = new Api ({
1724 openapi: OpenApiVersion .V3_1 ,
1825 info: {
19- title: ' Test ' ,
26+ title: ' Example REST API ' ,
2027 version: ' 1.0.0' ,
2128 },
2229});
2330
24- new Info (api , ' info' , {
25- title: ' Sites REST API' ,
26- version: ' 1.0.0' ,
31+ new Server (api , ' ExampleServer' , {
32+ url: new URL (' https://api.example.com' ),
2733});
2834
35+ const httpBearerJwtScheme = new SecurityScheme (api , ' HttpBearerJwtScheme' , {
36+ type: ' http' ,
37+ scheme: ' bearer' ,
38+ bearerFormat: ' JWT' ,
39+ });
40+
41+ new SecurityRequirement (api , ' AllScopes' , {
42+ securityScheme: httpBearerJwtScheme ,
43+ scopes: [],
44+ });
45+
46+ const userTag = new Tag (api , ' UserTag' , {
47+ name: ' user' ,
48+ });
49+
50+ const userDeleteScopeReq = new SecurityRequirement (api , ' UserDeleteScope' , {
51+ securityScheme: httpBearerJwtScheme ,
52+ scopes: [' users.delete' ],
53+ });
54+
55+ const noSecurityRequirement = new SecurityRequirement (api , ' NoSecurity' );
56+
2957const addressSchema = new Schema (api , ' Address' , {
3058 schema: {
3159 type: ' object' ,
@@ -42,16 +70,25 @@ const addressSchema = new Schema(api, 'Address', {
4270 },
4371});
4472
45- new Schema (api , ' User' , {
73+ const idSchema = new Schema (api , ' Id' , {
74+ schema: {
75+ type: ' string' ,
76+ minLength: 6 ,
77+ maxLength: 6 ,
78+ },
79+ });
80+
81+ const user = new Schema (api , ' User' , {
4682 schema: {
4783 type: ' object' ,
4884 required: [' name' ],
4985 additionalProperties: false ,
5086 properties: {
87+ userId: idSchema .referenceObject (),
5188 name: {
5289 type: ' string' ,
5390 },
54- address: addressSchema .toJSON (),
91+ address: addressSchema .referenceObject (),
5592 age: {
5693 type: ' integer' ,
5794 format: ' int32' ,
@@ -61,32 +98,115 @@ new Schema(api, 'User', {
6198 },
6299});
63100
64- const userIdentifiersSchema = new Schema (api , ' UserIdentifiers ' , {
101+ const updateUserRequest = new Schema (api , ' UpdateUserRequest ' , {
65102 schema: {
66103 type: ' object' ,
104+ minProperties: 1 ,
67105 additionalProperties: false ,
68- required: [' userId' ],
69106 properties: {
70- userId: {
71- type: ' string' ,
107+ address: addressSchema .referenceObject (),
108+ age: {
109+ type: ' integer' ,
110+ format: ' int32' ,
111+ minimum: 0 ,
72112 },
73113 },
74114 },
75115});
76116
117+ const createUserRequest = new Reference (user , ' CreateUserRequest' );
118+
119+ const users = new Schema (api , ' Users' , {
120+ schema: {
121+ type: ' array' ,
122+ uniqueItems: true ,
123+ items: user .referenceObject (),
124+ },
125+ });
126+
127+ const userIdParameter = new Parameter (api , ' UserId' , {
128+ name: ' userId' ,
129+ in: ' path' ,
130+ required: true ,
131+ schema: idSchema ,
132+ });
133+
134+ new Path (api , {
135+ path: ' /users' ,
136+ tags: new Set ([userTag ]),
137+ })
138+ .addOperation (HttpMethods .GET , {
139+ operationId: ' listUsersCommand' ,
140+ responses: {
141+ 200 : new Response (api , ' ListUsersResponse' , {
142+ description: ' User 200 response' ,
143+ content: {
144+ contentType: ' application/json' ,
145+ schema: users ,
146+ },
147+ }),
148+ },
149+ })
150+ .addOperation (HttpMethods .POST , {
151+ operationId: ' createUserCommand' ,
152+ requestBody: {
153+ content: {
154+ contentType: ' application/json' ,
155+ schema: createUserRequest ,
156+ },
157+ },
158+ responses: {
159+ 200 : new Response (api , ' CreateUserResponse' , {
160+ description: ' User 200 response' ,
161+ content: {
162+ contentType: ' application/json' ,
163+ schema: users ,
164+ },
165+ }),
166+ },
167+ });
168+
77169new Path (api , {
78170 path: ' /users/{userId}' ,
79- }).addOperation (OpenAPIV3 .HttpMethods .GET , {
80- operationId: ' getUserById' ,
81- parameters: [
82- {
83- name: ' userId' ,
84- in: ' path' ,
85- required: true ,
86- schema: userIdentifiersSchema .toJSON (),
171+ parameters: [userIdParameter ],
172+ })
173+ .addOperation (HttpMethods .GET , {
174+ operationId: ' getUserByIdCommand' ,
175+ responses: {
176+ 200 : new Response (api , ' GetUserById' , {
177+ description: ' User 200 response' ,
178+ content: {
179+ contentType: ' application/json' ,
180+ schema: user ,
181+ },
182+ }),
87183 },
88- ],
89- });
184+ })
185+ .addOperation (HttpMethods .DELETE , {
186+ operationId: ' deleteUserByIdCommand' ,
187+ security: userDeleteScopeReq ,
188+ })
189+ .addOperation (HttpMethods .HEAD , {
190+ operationId: ' checkUserIdAvailableCommand' ,
191+ security: noSecurityRequirement ,
192+ })
193+ .addOperation (HttpMethods .POST , {
194+ operationId: ' updateUserCommand' ,
195+ requestBody: {
196+ content: {
197+ contentType: ' application/json' ,
198+ schema: updateUserRequest ,
199+ },
200+ },
201+ responses: {
202+ 200 : new Response (api , ' UpdateUserResponse' , {
203+ content: {
204+ contentType: ' application/json' ,
205+ schema: user ,
206+ },
207+ }),
208+ },
209+ });
90210
91211process .stdout .write (JSON .stringify (api .synth (), null , 2 ));
92212```
0 commit comments