mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-10 20:52:42 +01:00
✨ generate test responses
This commit is contained in:
parent
54412a3364
commit
69550e535e
90
api/assets/responses.json
Normal file
90
api/assets/responses.json
Normal file
@ -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#"
|
||||
}
|
||||
}
|
@ -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 });
|
||||
|
68
api/scripts/generate_test_schema.ts
Normal file
68
api/scripts/generate_test_schema.ts
Normal file
@ -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;
|
||||
}
|
@ -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();
|
||||
// });
|
@ -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/*"]
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue
Block a user