Skip to content

Commit

Permalink
feat: much better API handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maamokun committed Dec 13, 2024
1 parent eca7c5a commit 9c136e8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 68 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 50 additions & 0 deletions src/api/routes/account-link.ts
Original file line number Diff line number Diff line change
@@ -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 });
},
);
30 changes: 30 additions & 0 deletions src/api/routes/dm.ts
Original file line number Diff line number Diff line change
@@ -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 });
},
);
89 changes: 21 additions & 68 deletions src/api/server.ts
Original file line number Diff line number Diff line change
@@ -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, () => {
Expand Down

0 comments on commit 9c136e8

Please sign in to comment.