Skip to content

Commit 594cfd6

Browse files
committed
chore: random improvements
1 parent 0d33ada commit 594cfd6

14 files changed

Lines changed: 509 additions & 161 deletions

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,3 @@ dist: node_modules tsconfig.json $(SRCS)
2020
.PHONY: dev
2121
dev:
2222
yarn tsc -w
23-
# "build": "jsii",
24-
# "watch": "jsii -w",
25-
# "package": "jsii-pacmak",

README.md

Lines changed: 141 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,53 @@ An experimental AWS Constructs based Open API schema builder
77
```typescript
88
import {
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

1623
const 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+
2957
const 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+
77169
new 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

91211
process.stdout.write(JSON.stringify(api.synth(), null, 2));
92212
```

__tests__/example.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
],
3232
"additionalProperties": false,
3333
"properties": {
34+
"userId": {
35+
"$ref": "#/components/schemas/Id"
36+
},
3437
"name": {
3538
"type": "string"
3639
},
@@ -66,6 +69,9 @@
6669
],
6770
"additionalProperties": false,
6871
"properties": {
72+
"userId": {
73+
"$ref": "#/components/schemas/Id"
74+
},
6975
"name": {
7076
"type": "string"
7177
},
@@ -128,7 +134,8 @@
128134
"post": {
129135
"operationId": "createUserCommand",
130136
"tags": [
131-
"user"
137+
"user",
138+
"random"
132139
],
133140
"requestBody": {
134141
"description": "",
@@ -157,9 +164,7 @@
157164
"/users/{userId}": {
158165
"get": {
159166
"operationId": "getUserByIdCommand",
160-
"tags": [
161-
"user"
162-
],
167+
"tags": [],
163168
"responses": {
164169
"200": {
165170
"description": "User 200 response",
@@ -183,23 +188,20 @@
183188
}
184189
],
185190
"tags": [
186-
"user"
191+
"user",
192+
"random"
187193
]
188194
},
189195
"head": {
190196
"operationId": "checkUserIdAvailableCommand",
191197
"security": [
192198
{}
193199
],
194-
"tags": [
195-
"user"
196-
]
200+
"tags": []
197201
},
198202
"post": {
199203
"operationId": "updateUserCommand",
200-
"tags": [
201-
"user"
202-
],
204+
"tags": [],
203205
"requestBody": {
204206
"description": "",
205207
"content": {

0 commit comments

Comments
 (0)