2019-08-22 01:22:26 +03:00
|
|
|
import express from "express";
|
|
|
|
import { availablePlugins } from "../plugins/availablePlugins";
|
|
|
|
import { ZeppelinPlugin } from "../plugins/ZeppelinPlugin";
|
|
|
|
import { notFound } from "./responses";
|
2019-08-22 02:58:32 +03:00
|
|
|
import { dropPropertiesByName, indentLines } from "../utils";
|
2019-09-22 17:06:22 +03:00
|
|
|
import { IPluginCommandConfig, Plugin, pluginUtils } from "knub";
|
|
|
|
import { parseParameters } from "knub-command-manager";
|
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}";
|
2019-09-29 15:54:19 +03:00
|
|
|
} 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) {
|
|
|
|
const docsPlugins = availablePlugins.filter(pluginClass => pluginClass.showInDocs);
|
|
|
|
|
|
|
|
app.get("/docs/plugins", (req: express.Request, res: express.Response) => {
|
|
|
|
res.json(
|
|
|
|
docsPlugins.map(pluginClass => {
|
|
|
|
const thinInfo = pluginClass.pluginInfo ? { prettyName: pluginClass.pluginInfo.prettyName } : {};
|
|
|
|
return {
|
|
|
|
name: pluginClass.pluginName,
|
|
|
|
info: thinInfo,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/docs/plugins/:pluginName", (req: express.Request, res: express.Response) => {
|
|
|
|
const pluginClass = docsPlugins.find(obj => obj.pluginName === req.params.pluginName);
|
|
|
|
if (!pluginClass) {
|
|
|
|
return notFound(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
const props = Reflect.ownKeys(pluginClass.prototype);
|
|
|
|
const commands = props.reduce((arr, prop) => {
|
|
|
|
if (typeof prop !== "string") return arr;
|
2019-09-22 17:06:22 +03:00
|
|
|
const decoratorCommands = pluginUtils.getPluginDecoratorCommands(pluginClass as typeof Plugin);
|
|
|
|
if (decoratorCommands) {
|
2019-08-22 01:22:26 +03:00
|
|
|
arr.push(
|
2019-09-22 17:06:22 +03:00
|
|
|
...decoratorCommands.map(cmd => {
|
|
|
|
const trigger = typeof cmd.trigger === "string" ? cmd.trigger : cmd.trigger.source;
|
2019-08-22 01:22:26 +03:00
|
|
|
const parameters = cmd.parameters
|
|
|
|
? typeof cmd.parameters === "string"
|
2019-09-22 17:06:22 +03:00
|
|
|
? parseParameters(cmd.parameters)
|
2019-08-22 01:22:26 +03:00
|
|
|
: cmd.parameters
|
|
|
|
: [];
|
2019-09-22 17:06:22 +03:00
|
|
|
const config: IPluginCommandConfig = cmd.config || {};
|
2019-08-22 01:22:26 +03:00
|
|
|
if (config.overloads) {
|
|
|
|
config.overloads = config.overloads.map(overload => {
|
2019-09-22 17:06:22 +03:00
|
|
|
return typeof overload === "string" ? parseParameters(overload) : overload;
|
2019-08-22 01:22:26 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
trigger,
|
|
|
|
parameters,
|
|
|
|
config,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
|
2019-09-29 15:54:19 +03:00
|
|
|
const defaultOptions = (pluginClass as typeof ZeppelinPlugin).getStaticDefaultOptions();
|
2019-08-22 01:22:26 +03:00
|
|
|
|
2019-08-22 02:58:32 +03:00
|
|
|
const configSchema = pluginClass.configSchema && formatConfigSchema(pluginClass.configSchema);
|
|
|
|
|
2019-08-22 01:22:26 +03:00
|
|
|
res.json({
|
|
|
|
name: pluginClass.pluginName,
|
|
|
|
info: pluginClass.pluginInfo || {},
|
2019-08-22 02:58:32 +03:00
|
|
|
configSchema,
|
2019-09-29 15:54:19 +03:00
|
|
|
defaultOptions,
|
2019-08-22 01:22:26 +03:00
|
|
|
commands,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|