diff options
-rw-r--r-- | api/assets/responses.json | 90 | ||||
-rw-r--r-- | api/scripts/generate_openapi_schema.ts | 41 | ||||
-rw-r--r-- | api/scripts/generate_test_schema.ts | 68 | ||||
-rw-r--r-- | api/scripts/globalSetup.js | 15 | ||||
-rw-r--r-- | api/scripts/tsconfig-paths-bootstrap.js | 10 |
5 files changed, 175 insertions, 49 deletions
diff --git a/api/assets/responses.json b/api/assets/responses.json new file mode 100644 index 00000000..35645d73 --- /dev/null +++ b/api/assets/responses.json @@ -0,0 +1,90 @@ +{ + "UserProfileResponse": { + "type": "object", + "properties": { + "user": { + "$ref": "#/definitions/UserPublic" + }, + "connected_accounts": { + "$ref": "#/definitions/PublicConnectedAccount" + }, + "premium_guild_since": { + "type": "string", + "format": "date-time" + }, + "premium_since": { + "type": "string", + "format": "date-time" + } + }, + "additionalProperties": false, + "required": [ + "connected_accounts", + "user" + ], + "definitions": { + "UserPublic": { + "type": "object", + "properties": { + "username": { + "type": "string" + }, + "discriminator": { + "type": "string" + }, + "id": { + "type": "string" + }, + "public_flags": { + "type": "string" + }, + "avatar": { + "type": "string" + }, + "accent_color": { + "type": "integer" + }, + "banner": { + "type": "string" + }, + "bio": { + "type": "string" + }, + "bot": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "bio", + "bot", + "discriminator", + "id", + "public_flags", + "username" + ] + }, + "PublicConnectedAccount": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "type": { + "type": "string" + }, + "verifie": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "name", + "type", + "verifie" + ] + } + }, + "$schema": "http://json-schema.org/draft-07/schema#" + } +} \ No newline at end of file diff --git a/api/scripts/generate_openapi_schema.ts b/api/scripts/generate_openapi_schema.ts index 329aeaf4..c0995b6c 100644 --- a/api/scripts/generate_openapi_schema.ts +++ b/api/scripts/generate_openapi_schema.ts @@ -48,34 +48,27 @@ function combineSchemas(opts: { program: TJS.Program; generator: TJS.JsonSchemaG return definitions; } +const ExcludedSchemas = [ + "DefaultSchema", + "Schema", + "EntitySchema", + "ServerResponse", + "Http2ServerResponse", + "global.Express.Response", + "Response", + "e.Response", + "request.Response", + "supertest.Response" +]; + function apiSchemas() { 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", - "" - ]; + const schemas = generator + .getUserSymbols() + .filter((x) => x.endsWith("Response") && !ExcludedSchemas.includes(x)) + .concat(generator.getUserSymbols().filter((x) => x.endsWith("Schema") && !ExcludedSchemas.includes(x))); // @ts-ignore combineSchemas({ schemas, generator, program }); diff --git a/api/scripts/generate_test_schema.ts b/api/scripts/generate_test_schema.ts new file mode 100644 index 00000000..eed77738 --- /dev/null +++ b/api/scripts/generate_test_schema.ts @@ -0,0 +1,68 @@ +// 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 schemaPath = path.join(__dirname, "..", "assets", "responses.json"); + +const settings: TJS.PartialArgs = { + required: true, + ignoreErrors: true, + excludePrivate: true, + defaultNumberType: "integer", + noExtraProps: true, + defaultProps: false +}; +const compilerOptions: TJS.CompilerOptions = { + strictNullChecks: true +}; +const ExcludedSchemas = [ + "ServerResponse", + "Http2ServerResponse", + "global.Express.Response", + "Response", + "e.Response", + "request.Response", + "supertest.Response" +]; + +function main() { + const program = TJS.getProgramFromFiles(walk(path.join(__dirname, "..", "src", "routes")), compilerOptions); + const generator = TJS.buildGenerator(program, settings); + if (!generator || !program) return; + + const schemas = generator.getUserSymbols().filter((x) => x.endsWith("Response") && !ExcludedSchemas.includes(x)); + console.log(schemas); + + var definitions: any = {}; + + for (const name of schemas) { + const part = TJS.generateSchema(program, name, settings, [], generator as TJS.JsonSchemaGenerator); + if (!part) continue; + + definitions = { ...definitions, [name]: { ...part } }; + } + + fs.writeFileSync(schemaPath, JSON.stringify(definitions, null, 4)); +} + +// #/definitions/ +main(); + +function walk(dir: string) { + var results = [] as string[]; + var list = fs.readdirSync(dir); + list.forEach(function (file) { + file = dir + "/" + file; + var stat = fs.statSync(file); + if (stat && stat.isDirectory()) { + /* Recurse into a subdirectory */ + results = results.concat(walk(file)); + } else { + if (!file.endsWith(".ts")) return; + results.push(file); + } + }); + return results; +} diff --git a/api/scripts/globalSetup.js b/api/scripts/globalSetup.js deleted file mode 100644 index 98e70fb9..00000000 --- a/api/scripts/globalSetup.js +++ /dev/null @@ -1,15 +0,0 @@ -const fs = require("fs"); -const path = require("path"); -const { FosscordServer } = require("../dist/Server"); -const Server = new FosscordServer({ port: 3001 }); -global.server = Server; -module.exports = async () => { - try { - fs.unlinkSync(path.join(__dirname, "..", "database.db")); - } catch {} - return await Server.start(); -}; - -// afterAll(async () => { -// return await Server.stop(); -// }); diff --git a/api/scripts/tsconfig-paths-bootstrap.js b/api/scripts/tsconfig-paths-bootstrap.js deleted file mode 100644 index d6ad3c57..00000000 --- a/api/scripts/tsconfig-paths-bootstrap.js +++ /dev/null @@ -1,10 +0,0 @@ -const tsConfigPaths = require("tsconfig-paths"); -const path = require("path"); - -const cleanup = tsConfigPaths.register({ - baseUrl: path.join(__dirname, ".."), - paths: { - "@fosscord/api": ["dist/index.js"], - "@fosscord/api/*": ["dist/*"] - } -}); |