zappyzep/backend/src/api/docs.ts

77 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-08-22 01:22:26 +03:00
import express from "express";
2020-07-06 02:01:16 +03:00
import { guildPlugins } from "../plugins/availablePlugins";
2019-08-22 01:22:26 +03:00
import { notFound } from "./responses";
2020-07-06 02:01:16 +03:00
import { indentLines } from "../utils";
import { getPluginName } from "knub/dist/plugins/pluginUtils";
import { ZeppelinPluginBlueprint } from "src/plugins/ZeppelinPluginBlueprint";
2019-08-22 01:22:26 +03:00
2019-08-22 02:58:32 +03:00
function formatConfigSchema(schema) {
if (schema._tag === "InterfaceType" || schema._tag === "PartialType") {
return (
`{\n` +
Object.entries(schema.props)
.map(([k, value]) => indentLines(`${k}: ${formatConfigSchema(value)}`, 2))
.join("\n") +
"\n}"
);
} else if (schema._tag === "DictionaryType") {
return "{\n" + indentLines(`[string]: ${formatConfigSchema(schema.codomain)}`, 2) + "\n}";
} else if (schema._tag === "ArrayType") {
return `Array<${formatConfigSchema(schema.type)}>`;
} else if (schema._tag === "UnionType") {
if (schema.name.startsWith("Nullable<")) {
return `Nullable<${formatConfigSchema(schema.types[0])}>`;
} else {
return schema.types.map(t => formatConfigSchema(t)).join(" | ");
}
} else if (schema._tag === "IntersectionType") {
return schema.types.map(t => formatConfigSchema(t)).join(" & ");
2019-08-22 02:58:32 +03:00
} else {
return schema.name;
}
}
2019-08-22 01:22:26 +03:00
export function initDocs(app: express.Express) {
2020-07-06 02:01:16 +03:00
const docsPlugins = guildPlugins.filter(pluginClass => pluginClass.showInDocs);
2019-08-22 01:22:26 +03:00
app.get("/docs/plugins", (req: express.Request, res: express.Response) => {
res.json(
docsPlugins.map(pluginClass => {
2020-07-06 02:01:16 +03:00
const thinInfo = pluginClass.info ? { prettyName: pluginClass.info.prettyName } : {};
2019-08-22 01:22:26 +03:00
return {
2020-07-06 02:01:16 +03:00
name: pluginClass.info,
2019-08-22 01:22:26 +03:00
info: thinInfo,
};
}),
);
});
app.get("/docs/plugins/:pluginName", (req: express.Request, res: express.Response) => {
// prettier-ignore
2020-07-10 21:03:40 +03:00
const plugin = docsPlugins.find(obj => getPluginName(obj) === req.params.pluginName);
2020-07-06 02:01:16 +03:00
if (!plugin) {
2019-08-22 01:22:26 +03:00
return notFound(res);
}
2020-07-06 02:01:16 +03:00
const name = getPluginName(plugin);
const info = plugin.info || {};
2019-10-05 14:46:00 +03:00
2020-07-10 21:03:40 +03:00
const commands = (plugin.commands || []).map(cmd => ({
2020-07-06 02:01:16 +03:00
trigger: cmd.trigger,
signature: cmd.signature,
config: cmd.config,
}));
2019-08-22 01:22:26 +03:00
2020-07-10 21:03:40 +03:00
const defaultOptions = plugin.defaultOptions || {};
2020-07-06 02:01:16 +03:00
const configSchema = plugin.configSchema && formatConfigSchema(plugin.configSchema);
2019-08-22 02:58:32 +03:00
2019-08-22 01:22:26 +03:00
res.json({
2020-07-06 02:01:16 +03:00
name,
info,
2019-08-22 02:58:32 +03:00
configSchema,
defaultOptions,
2019-08-22 01:22:26 +03:00
commands,
});
});
}