✨ 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;
|
return definitions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ExcludedSchemas = [
|
||||||
|
"DefaultSchema",
|
||||||
|
"Schema",
|
||||||
|
"EntitySchema",
|
||||||
|
"ServerResponse",
|
||||||
|
"Http2ServerResponse",
|
||||||
|
"global.Express.Response",
|
||||||
|
"Response",
|
||||||
|
"e.Response",
|
||||||
|
"request.Response",
|
||||||
|
"supertest.Response"
|
||||||
|
];
|
||||||
|
|
||||||
function apiSchemas() {
|
function apiSchemas() {
|
||||||
const program = TJS.getProgramFromFiles([path.join(__dirname, "..", "src", "schema", "index.ts")], compilerOptions);
|
const program = TJS.getProgramFromFiles([path.join(__dirname, "..", "src", "schema", "index.ts")], compilerOptions);
|
||||||
const generator = TJS.buildGenerator(program, settings);
|
const generator = TJS.buildGenerator(program, settings);
|
||||||
|
|
||||||
const schemas = [
|
const schemas = generator
|
||||||
"BanCreateSchema",
|
.getUserSymbols()
|
||||||
"DmChannelCreateSchema",
|
.filter((x) => x.endsWith("Response") && !ExcludedSchemas.includes(x))
|
||||||
"ChannelModifySchema",
|
.concat(generator.getUserSymbols().filter((x) => x.endsWith("Schema") && !ExcludedSchemas.includes(x)));
|
||||||
"ChannelGuildPositionUpdateSchema",
|
|
||||||
"ChannelGuildPositionUpdateSchema",
|
|
||||||
"EmojiCreateSchema",
|
|
||||||
"GuildCreateSchema",
|
|
||||||
"GuildUpdateSchema",
|
|
||||||
"GuildTemplateCreateSchema",
|
|
||||||
"GuildUpdateWelcomeScreenSchema",
|
|
||||||
"InviteCreateSchema",
|
|
||||||
"MemberCreateSchema",
|
|
||||||
"MemberNickChangeSchema",
|
|
||||||
"MemberChangeSchema",
|
|
||||||
"MessageCreateSchema",
|
|
||||||
"RoleModifySchema",
|
|
||||||
"TemplateCreateSchema",
|
|
||||||
"TemplateModifySchema",
|
|
||||||
"UserModifySchema",
|
|
||||||
"UserSettingsSchema",
|
|
||||||
"WidgetModifySchema",
|
|
||||||
""
|
|
||||||
];
|
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
combineSchemas({ schemas, generator, program });
|
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…
x
Reference in New Issue
Block a user