summary refs log tree commit diff
path: root/api/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'api/scripts')
-rw-r--r--api/scripts/generate_openapi_schema.ts41
-rw-r--r--api/scripts/generate_test_schema.ts68
-rw-r--r--api/scripts/globalSetup.js15
-rw-r--r--api/scripts/tsconfig-paths-bootstrap.js10
4 files changed, 85 insertions, 49 deletions
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/*"]
-	}
-});