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
|
// https://mermade.github.io/openapi-gui/#
// https://editor.swagger.io/
import path from "path";
import fs from "fs";
import * as TJS from "typescript-json-schema";
import "missing-native-js-functions";
const settings: TJS.PartialArgs = {
required: true,
ignoreErrors: true,
excludePrivate: true,
defaultNumberType: "integer",
noExtraProps: true,
defaultProps: false
};
const compilerOptions: TJS.CompilerOptions = {
strictNullChecks: false
};
const openapiPath = path.join(__dirname, "..", "assets", "openapi.json");
var specification = JSON.parse(fs.readFileSync(openapiPath, { encoding: "utf8" }));
async function generateSchemas() {
const program = TJS.getProgramFromFiles([path.join(__dirname, "..", "..", "util", "src", "index.ts")], compilerOptions);
const generator = TJS.buildGenerator(program, settings);
const schemas = [
"Application",
"Attachment",
"Message",
"AuditLog",
"Ban",
"Channel",
"Emoji",
"Guild",
"Invite",
"ReadState",
"Recipient",
"Relationship",
"Role",
"Sticker",
"Team",
"TeamMember",
"Template",
"VoiceState",
"Webhook",
"User",
"UserPublic"
];
// @ts-ignore
const definitions = combineSchemas({ schemas, generator, program });
for (const key in definitions) {
specification.components.schemas[key] = definitions[key];
delete definitions[key].additionalProperties;
}
}
function combineSchemas(opts: { program: TJS.Program; generator: TJS.JsonSchemaGenerator; schemas: string[] }) {
var definitions: any = {};
for (const name of opts.schemas) {
const part = TJS.generateSchema(opts.program, name, settings, [], opts.generator as TJS.JsonSchemaGenerator);
if (!part) continue;
definitions = { ...definitions, ...part.definitions, [name]: { ...part, definitions: undefined, $schema: undefined } };
}
return definitions;
}
function generateBodies() {
const program = TJS.getProgramFromFiles([path.join(__dirname, "..", "src", "schema", "index.ts")], compilerOptions);
const generator = TJS.buildGenerator(program, settings);
const schemas = [
"BanCreateSchema",
"DmChannelCreateSchema",
"ChannelModifySchema",
"ChannelGuildPositionUpdateSchema",
"ChannelGuildPositionUpdateSchema",
"EmojiCreateSchema",
"GuildCreateSchema",
"GuildUpdateSchema",
"GuildTemplateCreateSchema",
"GuildUpdateWelcomeScreenSchema",
"InviteCreateSchema",
"MemberCreateSchema",
"MemberNickChangeSchema",
"MemberChangeSchema",
"MessageCreateSchema",
"RoleModifySchema",
"TemplateCreateSchema",
"TemplateModifySchema",
"UserModifySchema",
"UserSettingsSchema",
"WidgetModifySchema"
];
// @ts-ignore
const definitions = combineSchemas({ schemas, generator, program });
for (const key in definitions) {
specification.components.requestBodies[key] = {
content: {
"application/json": { schema: definitions[key] }
},
description: ""
};
delete definitions[key].additionalProperties;
delete definitions[key].$schema;
}
}
function addDefaultResponses() {
Object.values(specification.paths).forEach((path: any) =>
Object.values(path).forEach((request: any) => {
if (!request.responses?.["401"]) {
request.responses["401"] = {
description: "Unauthorized",
content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } }
};
}
if (!request.responses?.["429"]) {
request.responses["429"] = {
description: "Rate limit exceeded",
content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } },
headers: {
"X-RateLimit-Bucket": {
description:
"A unique string denoting the rate limit being encountered (non-inclusive of major parameters in the route path)",
schema: { type: "string" }
},
"X-Rate-Limit-Limit": {
description: "The number of allowed requests in the current period",
schema: {
type: "integer"
}
},
"X-Rate-Limit-Remaining": {
description: "The number of remaining requests in the current period",
schema: {
type: "integer"
}
},
"X-Rate-Limit-Reset": {
description: "Date when current period is over in seconds since the Unix epoch",
schema: {
type: "integer"
}
},
"X-Rate-Limit-Reset-After": {
description: "Number of seconds when current period will reset (can have decimal)",
schema: {
type: "number"
}
},
"Retry-After": {
description: "Same as X-Rate-Limit-Reset-After but an integer",
schema: {
type: "integer"
}
},
"X-RateLimit-Global": {
description: "Indicates whether or not all requests from your ip are rate limited",
schema: {
type: "boolean"
}
}
}
};
}
})
);
}
function main() {
addDefaultResponses();
generateSchemas();
specification = JSON.parse(JSON.stringify(specification).replaceAll("#/definitions", "#/components/schemas"));
generateBodies();
fs.writeFileSync(
openapiPath,
JSON.stringify(specification, null, 4).replaceAll("#/definitions", "#/components/requestBodies").replaceAll("bigint", "number")
);
}
main();
|