summary refs log tree commit diff
path: root/src/api/routes/guilds/templates
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2023-03-24 21:21:21 -0400
committerPuyodead1 <puyodead@proton.me>2023-04-13 15:28:41 -0400
commitc2ce88dee726bb6c1993bf4615aa866d089ca494 (patch)
treef5b3fe7d7d1e23b5f2ce6540c7e78d34b4c1e43f /src/api/routes/guilds/templates
parentUpdate openapi.json (diff)
downloadserver-ts-c2ce88dee726bb6c1993bf4615aa866d089ca494.tar.xz
guilds
Diffstat (limited to 'src/api/routes/guilds/templates')
-rw-r--r--src/api/routes/guilds/templates/index.ts101
1 files changed, 60 insertions, 41 deletions
diff --git a/src/api/routes/guilds/templates/index.ts b/src/api/routes/guilds/templates/index.ts

index 8eff5563..32129cca 100644 --- a/src/api/routes/guilds/templates/index.ts +++ b/src/api/routes/guilds/templates/index.ts
@@ -31,53 +31,72 @@ import { Request, Response, Router } from "express"; import fetch from "node-fetch"; const router: Router = Router(); -router.get("/:code", route({}), async (req: Request, res: Response) => { - const { allowDiscordTemplates, allowRaws, enabled } = - Config.get().templates; - if (!enabled) - res.json({ - code: 403, - message: "Template creation & usage is disabled on this instance.", - }).sendStatus(403); +router.get( + "/:code", + route({ + responses: { + 200: { + body: "GuildTemplate", + }, + 403: { + body: "APIErrorResponse", + }, + 404: { + body: "APIErrorResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const { allowDiscordTemplates, allowRaws, enabled } = + Config.get().templates; + if (!enabled) + res.json({ + code: 403, + message: + "Template creation & usage is disabled on this instance.", + }).sendStatus(403); - const { code } = req.params; + const { code } = req.params; - if (code.startsWith("discord:")) { - if (!allowDiscordTemplates) - return res - .json({ - code: 403, - message: - "Discord templates cannot be used on this instance.", - }) - .sendStatus(403); - const discordTemplateID = code.split("discord:", 2)[1]; + if (code.startsWith("discord:")) { + if (!allowDiscordTemplates) + return res + .json({ + code: 403, + message: + "Discord templates cannot be used on this instance.", + }) + .sendStatus(403); + const discordTemplateID = code.split("discord:", 2)[1]; - const discordTemplateData = await fetch( - `https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, - { - method: "get", - headers: { "Content-Type": "application/json" }, - }, - ); - return res.json(await discordTemplateData.json()); - } + const discordTemplateData = await fetch( + `https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, + { + method: "get", + headers: { "Content-Type": "application/json" }, + }, + ); + return res.json(await discordTemplateData.json()); + } - if (code.startsWith("external:")) { - if (!allowRaws) - return res - .json({ - code: 403, - message: "Importing raws is disabled on this instance.", - }) - .sendStatus(403); + if (code.startsWith("external:")) { + if (!allowRaws) + return res + .json({ + code: 403, + message: "Importing raws is disabled on this instance.", + }) + .sendStatus(403); - return res.json(code.split("external:", 2)[1]); - } + return res.json(code.split("external:", 2)[1]); + } - const template = await Template.findOneOrFail({ where: { code: code } }); - res.json(template); -}); + const template = await Template.findOneOrFail({ + where: { code: code }, + }); + res.json(template); + }, +); router.post( "/:code",