-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathdocs.ts
More file actions
151 lines (133 loc) · 4.34 KB
/
docs.ts
File metadata and controls
151 lines (133 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import express from "express";
import { z } from "zod";
import { $ZodPipeDef } from "zod/v4/core";
import { availableGuildPlugins } from "../plugins/availablePlugins.js";
import { ZeppelinGuildPluginInfo } from "../types.js";
import { indentLines } from "../utils.js";
import { notFound } from "./responses.js";
function isZodObject(schema: z.ZodType): schema is z.ZodObject<any> {
return schema.def.type === "object";
}
function isZodRecord(schema: z.ZodType): schema is z.ZodRecord<any> {
return schema.def.type === "record";
}
function isZodOptional(schema: z.ZodType): schema is z.ZodOptional<any> {
return schema.def.type === "optional";
}
function isZodArray(schema: z.ZodType): schema is z.ZodArray<any> {
return schema.def.type === "array";
}
function isZodUnion(schema: z.ZodType): schema is z.ZodUnion<any> {
return schema.def.type === "union";
}
function isZodNullable(schema: z.ZodType): schema is z.ZodNullable<any> {
return schema.def.type === "nullable";
}
function isZodDefault(schema: z.ZodType): schema is z.ZodDefault<any> {
return schema.def.type === "default";
}
function isZodLiteral(schema: z.ZodType): schema is z.ZodLiteral<any> {
return schema.def.type === "literal";
}
function isZodIntersection(schema: z.ZodType): schema is z.ZodIntersection<any, any> {
return schema.def.type === "intersection";
}
function formatZodConfigSchema(schema: z.ZodType) {
if (isZodObject(schema)) {
return (
`{\n` +
Object.entries(schema.def.shape)
.map(([k, value]) => indentLines(`${k}: ${formatZodConfigSchema(value as z.ZodType)}`, 2))
.join("\n") +
"\n}"
);
}
if (isZodRecord(schema)) {
return "{\n" + indentLines(`[string]: ${formatZodConfigSchema(schema.valueType as z.ZodType)}`, 2) + "\n}";
}
if (isZodOptional(schema)) {
return `Optional<${formatZodConfigSchema(schema.def.innerType)}>`;
}
if (isZodArray(schema)) {
return `Array<${formatZodConfigSchema(schema.def.element)}>`;
}
if (isZodUnion(schema)) {
return schema.def.options.map((t) => formatZodConfigSchema(t)).join(" | ");
}
if (isZodNullable(schema)) {
return `Nullable<${formatZodConfigSchema(schema.def.innerType)}>`;
}
if (isZodDefault(schema)) {
return formatZodConfigSchema(schema.def.innerType);
}
if (isZodLiteral(schema)) {
return schema.def.values;
}
if (isZodIntersection(schema)) {
return [
formatZodConfigSchema(schema.def.left as z.ZodType),
formatZodConfigSchema(schema.def.right as z.ZodType),
].join(" & ");
}
if (schema.def.type === "string") {
return "string";
}
if (schema.def.type === "number") {
return "number";
}
if (schema.def.type === "boolean") {
return "boolean";
}
if (schema.def.type === "never") {
return "never";
}
if (schema.def.type === "pipe") {
return formatZodConfigSchema((schema.def as $ZodPipeDef).in as z.ZodType);
}
return "unknown";
}
const availableGuildPluginsByName = availableGuildPlugins.reduce<Record<string, ZeppelinGuildPluginInfo>>(
(map, obj) => {
map[obj.plugin.name] = obj;
return map;
},
{},
);
export function initDocs(router: express.Router) {
const docsPlugins = availableGuildPlugins.filter((obj) => obj.docs.type !== "internal");
router.get("/docs/plugins", (req: express.Request, res: express.Response) => {
res.json(
docsPlugins.map((obj) => ({
name: obj.plugin.name,
info: {
prettyName: obj.docs.prettyName,
type: obj.docs.type,
},
})),
);
});
router.get("/docs/plugins/:pluginName", (req: express.Request, res: express.Response) => {
const pluginInfo = availableGuildPluginsByName[req.params.pluginName];
if (!pluginInfo) {
return notFound(res);
}
const { configSchema, ...info } = pluginInfo.docs;
const formattedConfigSchema = formatZodConfigSchema(configSchema);
const messageCommands = (pluginInfo.plugin.messageCommands || []).map((cmd) => ({
trigger: cmd.trigger,
permission: cmd.permission,
signature: cmd.signature,
description: cmd.description,
usage: cmd.usage,
config: cmd.config,
}));
const defaultOptions = pluginInfo.docs.configSchema.safeParse({}).data ?? {};
res.json({
name: pluginInfo.plugin.name,
info,
configSchema: formattedConfigSchema,
defaultOptions,
messageCommands,
});
});
}