From 3a40254ca542a29a211b181f1b6853370bffeb21 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 24 Mar 2023 21:36:50 -0400 Subject: oapi: invites --- assets/openapi.json | 31 +++++++++- src/api/routes/invites/index.ts | 122 ++++++++++++++++++++++++++++------------ 2 files changed, 116 insertions(+), 37 deletions(-) diff --git a/assets/openapi.json b/assets/openapi.json index 8062c7a6..5c29b13f 100644 --- a/assets/openapi.json +++ b/assets/openapi.json @@ -7779,8 +7779,35 @@ } ], "responses": { - "default": { - "description": "No description available" + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Invite" + } + } + } + }, + "401": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/APIErrorResponse" + } + } + } + }, + "404": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/APIErrorResponse" + } + } + } } }, "parameters": [ diff --git a/src/api/routes/invites/index.ts b/src/api/routes/invites/index.ts index 6680e375..5a9cd942 100644 --- a/src/api/routes/invites/index.ts +++ b/src/api/routes/invites/index.ts @@ -16,35 +16,64 @@ along with this program. If not, see . */ -import { Router, Request, Response } from "express"; +import { route } from "@spacebar/api"; import { emitEvent, getPermission, Guild, Invite, InviteDeleteEvent, - User, PublicInviteRelation, + User, } from "@spacebar/util"; -import { route } from "@spacebar/api"; +import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; const router: Router = Router(); -router.get("/:code", route({}), async (req: Request, res: Response) => { - const { code } = req.params; +router.get( + "/:code", + route({ + responses: { + "200": { + body: "Invite", + }, + 404: { + body: "APIErrorResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const { code } = req.params; - const invite = await Invite.findOneOrFail({ - where: { code }, - relations: PublicInviteRelation, - }); + const invite = await Invite.findOneOrFail({ + where: { code }, + relations: PublicInviteRelation, + }); - res.status(200).send(invite); -}); + res.status(200).send(invite); + }, +); router.post( "/:code", - route({ right: "USE_MASS_INVITES" }), + route({ + right: "USE_MASS_INVITES", + responses: { + "200": { + body: "Invite", + }, + 401: { + body: "APIErrorResponse", + }, + 403: { + body: "APIErrorResponse", + }, + 404: { + body: "APIErrorResponse", + }, + }, + }), async (req: Request, res: Response) => { const { code } = req.params; const { guild_id } = await Invite.findOneOrFail({ @@ -75,33 +104,56 @@ router.post( ); // * cant use permission of route() function because path doesn't have guild_id/channel_id -router.delete("/:code", route({}), async (req: Request, res: Response) => { - const { code } = req.params; - const invite = await Invite.findOneOrFail({ where: { code } }); - const { guild_id, channel_id } = invite; - - const permission = await getPermission(req.user_id, guild_id, channel_id); +router.delete( + "/:code", + route({ + responses: { + "200": { + body: "Invite", + }, + 401: { + body: "APIErrorResponse", + }, + 404: { + body: "APIErrorResponse", + }, + }, + }), + async (req: Request, res: Response) => { + const { code } = req.params; + const invite = await Invite.findOneOrFail({ where: { code } }); + const { guild_id, channel_id } = invite; - if (!permission.has("MANAGE_GUILD") && !permission.has("MANAGE_CHANNELS")) - throw new HTTPError( - "You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", - 401, + const permission = await getPermission( + req.user_id, + guild_id, + channel_id, ); - await Promise.all([ - Invite.delete({ code }), - emitEvent({ - event: "INVITE_DELETE", - guild_id: guild_id, - data: { - channel_id: channel_id, + if ( + !permission.has("MANAGE_GUILD") && + !permission.has("MANAGE_CHANNELS") + ) + throw new HTTPError( + "You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", + 401, + ); + + await Promise.all([ + Invite.delete({ code }), + emitEvent({ + event: "INVITE_DELETE", guild_id: guild_id, - code: code, - }, - } as InviteDeleteEvent), - ]); + data: { + channel_id: channel_id, + guild_id: guild_id, + code: code, + }, + } as InviteDeleteEvent), + ]); - res.json({ invite: invite }); -}); + res.json({ invite: invite }); + }, +); export default router; -- cgit 1.4.1