2021-09-01 23:33:14 +02:00
|
|
|
// 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" }));
|
|
|
|
|
2021-09-03 03:38:40 +02:00
|
|
|
async function utilSchemas() {
|
2021-09-01 23:33:14 +02:00
|
|
|
const program = TJS.getProgramFromFiles([path.join(__dirname, "..", "..", "util", "src", "index.ts")], compilerOptions);
|
|
|
|
const generator = TJS.buildGenerator(program, settings);
|
|
|
|
|
2021-09-03 03:38:40 +02:00
|
|
|
const schemas = ["UserPublic", "UserPrivate", "PublicConnectedAccount"];
|
2021-09-01 23:33:14 +02:00
|
|
|
|
|
|
|
// @ts-ignore
|
2021-09-03 03:38:40 +02:00
|
|
|
combineSchemas({ schemas, generator, program });
|
2021-09-01 23:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-09-03 03:38:40 +02:00
|
|
|
definitions = { ...definitions, [name]: { ...part, definitions: undefined, $schema: undefined } };
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key in definitions) {
|
|
|
|
specification.components.schemas[key] = definitions[key];
|
|
|
|
delete definitions[key].additionalProperties;
|
|
|
|
delete definitions[key].$schema;
|
2021-09-01 23:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return definitions;
|
|
|
|
}
|
|
|
|
|
2021-09-03 03:38:40 +02:00
|
|
|
function apiSchemas() {
|
2021-09-01 23:33:14 +02:00
|
|
|
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",
|
2021-09-03 03:38:40 +02:00
|
|
|
"WidgetModifySchema",
|
|
|
|
""
|
2021-09-01 23:33:14 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// @ts-ignore
|
2021-09-03 03:38:40 +02:00
|
|
|
combineSchemas({ schemas, generator, program });
|
2021-09-01 23:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function addDefaultResponses() {
|
2021-09-03 03:38:40 +02:00
|
|
|
Object.values(specification.paths).forEach((path: any) => Object.values(path).forEach((request: any) => {}));
|
2021-09-01 23:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
addDefaultResponses();
|
2021-09-03 03:38:40 +02:00
|
|
|
utilSchemas();
|
|
|
|
apiSchemas();
|
2021-09-01 23:33:14 +02:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
openapiPath,
|
2021-09-03 03:38:40 +02:00
|
|
|
JSON.stringify(specification, null, 4).replaceAll("#/definitions", "#/components/schemas").replaceAll("bigint", "number")
|
2021-09-01 23:33:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|