From 9c136e834a3d21ad792095eee6cc73dcc6036bfa Mon Sep 17 00:00:00 2001 From: maamokun/MikanDev Date: Fri, 13 Dec 2024 20:52:51 +0900 Subject: [PATCH] feat: much better API handling --- bun.lockb | Bin 64994 -> 67538 bytes package.json | 1 + src/api/routes/account-link.ts | 50 ++++++++++++++++++ src/api/routes/dm.ts | 30 +++++++++++ src/api/server.ts | 89 ++++++++------------------------- 5 files changed, 102 insertions(+), 68 deletions(-) create mode 100644 src/api/routes/account-link.ts create mode 100644 src/api/routes/dm.ts diff --git a/bun.lockb b/bun.lockb index 705081fea268852b4ae8d01823d67eec76b08dfb..62680ddad9c8c4ac682a23101d0fdecf8faabf99 100755 GIT binary patch delta 10700 zcmeHNX;@T8*1or)g)2KM*fuEQ3dquIjiR)uqejIgMsZ1ut#+Ul1Zhwq<3JqOETiUV z)EGC^M8pj-62S#A?nXx)W7KHk5;M4rVT>l4sPmq?j5wcpzWM%rKT;2GojP@@PMtb+ zs_vz4UH!%B^gQSJ5h0^neLuXkIPlkp$zo{oz}vXkzse_3cjlRA^LFr z9hF`Mbpvn9$amPSg7AftqGvm(0rg|EvL@IIZGx}{6+Wo=7#$jd?$ZlG6VNiy$-xKNP$dXhzmJt0P?ydZ~P_BRtbS)s~a%$gq!rZX4=(0tu>om?Q{= zEl|-N1s+fvC=R?Q_`HeZ>|^bM5QYxy0Ut~UqN4twJTpSZGoHfkLRtv0$7X?Y{~S;@ zPplt9g(59dcP;O@h zWdri^?BiJPLk!4<_e3Sr9V&I?=8a*SPk0n6mN?LxE!qjn3UYH|<5|HvFg)-AOoQd) zY-94+;!N(1vX9bcDAXxwuu-z0rjw@XsDlJST9gsq9joEpv7O|7kZ<>4P!ys zrJ*D``4?V;v1nQJ6)4wMfx@z)@v5iCPeD8(RU(eKKCYP{;IAkxH*11zq9CMMr`oe% z;(wYy#;6Sc1^V&AO$bo*ZwF<*4D~RtuxJ|wWIN{vDuTB_+5V{Jin+lmwNFg9<=Ar_ zxsanvQP#w$^N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#pQA&ERQVFUkGelyJ7kmDqE4QOQ!x~Q{a}Mlj-eF| zs{9yjZ%{49i`jG2CYNm*k@sgzh77=F)X|5~s-f!*DRL$aJ)DBC{p!Ou*-epe*y!iO zffnNta7w=$E!M6pT4R!7gD67BVm#k0s~kGxJIl6k%pIlW7ZDs5Zx$wyS#N*QN5dB!63P|Oiq%w5j_lOqqSTGXbFeMY+V^-srz zcSLI1O6!i;aL>;k$;PA`kE#~q8F7wd<-DZpqdhrmi2NpPrQD;uG_*whh{bqHd}rj4 z&0j3L7%et)+fjFOBw~tFgqvnJrr^Kh2OCw3@$gtWW#9e7lBSKvEb*GVr_%S0HjDA@ zc;bqA!ho=Yv5?!eMB}0X$7~kk9diG|C+(=~4z(&V-nZ;dx|8qqe-Qnb{S$%%q&OH31}H_oFl^ zWJi}9X4xw-UKL)glm z(ASS$rYk2^i}7xMEp0|c>*?=hK(2d2+*Q+sp5%2ZptJFMdN#Ip;K5VhP4Yy%ijR!s z)r*o(1;iT9t#vzwIXb7tt-q~cJji}>-S*JO-8@XUC1ZQhQp|0<;O?u+3K+U-=Hc54 zkN2X6Q&Fb;KJ>$>sJlg|ejF=k(CKWs9c@3Y;;Ve*v@O`V(vmu^MBI zmF|)n^jY$(WIYol?z^gG08@E5J$)w1)Fy&PoDJw~JnH(m_5?h3Z)uK6)_;^peIn@A znSjp5YjFIFg9iDUo?2#-lMx*_OuP&84EjVSO?}kAR_6fV58(VSv)^3o_eF^IqaqS1 z{P&;61^TD4KfQTYZQbR#mdK_Y#~(4tCj6^0;#`EK0Q2$hp~vE4PRb=dx0dT#_urIg T{<-|xpU;izSsQrswbuUxV-hQR 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, () => {