diff --git a/bun.lockb b/bun.lockb index 705081f..62680dd 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5ad77fa..09b7ee1 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "dependencies": { "@baselime/pino-transport": "^0.1.5", "@biomejs/biome": "^1.8.2", + "@elysiajs/swagger": "^1.1.6", "@google-cloud/translate": "^8.5.0", "@google-cloud/vision": "^4.3.2", "@prisma/client": "^6.0.1", diff --git a/src/api/routes/account-link.ts b/src/api/routes/account-link.ts new file mode 100644 index 0000000..e911b83 --- /dev/null +++ b/src/api/routes/account-link.ts @@ -0,0 +1,50 @@ +import { Elysia } from "elysia"; +import { dmUser } from "../../index.ts"; +import { PrismaClient } from "@prisma/client"; + +const prisma = new PrismaClient(); + +export const AcclinkEndpoint = new Elysia({ prefix: "/admin/link" }).post( + "/", + async ({ query, body }) => { + const key = query.key; + const json = body as { uid: string; acc: string }; + + const uid = json.uid; + const acc = json.acc; + + if (!key) return new Response("No key provided", { status: 400 }); + if (!uid || !acc) + return new Response("Missing uid or account information", { + status: 400, + }); + + if (key !== process.env.API_SIGNING_KEY) + return new Response("Invalid key", { status: 401 }); + + const user = await prisma.user.findUnique({ + where: { + id: uid, + }, + }); + + if (!user) return new Response("User not found", { status: 404 }); + + await prisma.user.update({ + where: { + id: uid, + }, + data: { + mdUID: acc, + }, + }); + + await dmUser( + uid, + "MikanDev Accounts", + `Your account has been linked!\n\n**MikanDev UID:** ${acc}\n**Discord ID:**${uid}`, + ); + + return new Response("Account linked", { status: 200 }); + }, +); diff --git a/src/api/routes/dm.ts b/src/api/routes/dm.ts new file mode 100644 index 0000000..738177a --- /dev/null +++ b/src/api/routes/dm.ts @@ -0,0 +1,30 @@ +import { Elysia } from "elysia"; +import { dmUser } from "../../index.ts"; + +export const dmEndpoint = new Elysia({ prefix: "/admin/dm" }).post( + "/", + async ({ query, body }) => { + const key = query.key; + const json = body as { provider: string; uid: string; message: string }; + + if (!json) + return new Response("No JSON body provided", { status: 400 }); + + const provider = json.provider; + const uid = json.uid; + const message = json.message; + + if (!key) return new Response("No key provided", { status: 400 }); + if (key !== process.env.API_SIGNING_KEY) + return new Response("Invalid key", { status: 401 }); + if (!provider || !uid || !message) + return new Response("Missing provider, uid, or message", { + status: 400, + }); + + const response = await dmUser(uid, provider, message); + if (response instanceof Error) + return { status: 500, message: response.message }; + return new Response("Message sent", { status: 200 }); + }, +); diff --git a/src/api/server.ts b/src/api/server.ts index 6cf0103..4805c82 100644 --- a/src/api/server.ts +++ b/src/api/server.ts @@ -1,75 +1,28 @@ import { Elysia } from "elysia"; -import { dmUser } from ".."; -import { PrismaClient } from "@prisma/client"; - -const prisma = new PrismaClient(); -const app = new Elysia(); - -app.post("/accLink", async ({ query, body }) => { - const key = query.key; - const json = body as { uid: string; acc: string }; - - const uid = json.uid; - const acc = json.acc; - - if (!key) return new Response("No key provided", { status: 400 }); - if (!uid || !acc) - return new Response("Missing uid or account information", { - status: 400, - }); - - if (key !== process.env.API_SIGNING_KEY) - return new Response("Invalid key", { status: 401 }); - - const user = await prisma.user.findUnique({ - where: { - id: uid, - }, - }); - - if (!user) return new Response("User not found", { status: 404 }); - - await prisma.user.update({ - where: { - id: uid, - }, - data: { - mdUID: acc, - }, +import { swagger } from '@elysiajs/swagger' +import { AcclinkEndpoint } from "./routes/account-link.ts"; +import { dmEndpoint } from "./routes/dm.ts"; + +export const app = new Elysia({ aot: false }).onError(({ code, error }) => { + console.log(code); + return new Response(JSON.stringify({ error: error.toString() ?? code }), { + status: 500, }); - - await dmUser( - uid, - "MikanDev Accounts", - `Your account has been linked!\n\n**MikanDev UID:** ${acc}\n**Discord ID:**${uid}`, - ); - - return new Response("Account linked", { status: 200 }); }); -app.post("/dm", async ({ query, body }) => { - const key = query.key; - const json = body as { provider: string; uid: string; message: string }; - - if (!json) return new Response("No JSON body provided", { status: 400 }); - - const provider = json.provider; - const uid = json.uid; - const message = json.message; - - if (!key) return new Response("No key provided", { status: 400 }); - if (key !== process.env.API_SIGNING_KEY) - return new Response("Invalid key", { status: 401 }); - if (!provider || !uid || !message) - return new Response("Missing provider, uid, or message", { - status: 400, - }); - - const response = await dmUser(uid, provider, message); - if (response instanceof Error) - return { status: 500, message: response.message }; - return new Response("Message sent", { status: 200 }); -}); +app.use(swagger({ + path: '/', + documentation: { + info: { + title: 'MikanBot API', + version: '1.0.0' + } + }, + exclude: ['/admin/*'], +})); + +app.use(AcclinkEndpoint); +app.use(dmEndpoint); export function start() { app.listen(process.env.API_PORT || 3000, () => {