Files
games/packages/server/src/api.ts
2025-08-16 00:15:07 -05:00

56 lines
1.1 KiB
TypeScript

import { prisma } from "./db/db";
import { Elysia, t } from "elysia";
import { Prisma } from "@prisma/client";
import { simpleApi } from "./games/simple";
const api = new Elysia({ prefix: "/api" })
.onBeforeHandle(async ({ cookie: { token } }) => {
if (token.value == null) {
console.log("CREATING NEW USER");
const newHuman = await prisma.human.create({
data: {},
});
token.value = newHuman.key;
}
})
.guard({ cookie: t.Object({ token: t.String() }) })
.post(
"/setName",
({ cookie: { token: humanKey }, body: { name } }) =>
prisma.human.update({
where: {
key: humanKey.value,
},
data: {
name,
},
}),
{
body: t.Object({
name: t.String(),
}),
}
)
.get("/profile", ({ cookie: { token: humanKey } }) =>
prisma.human.findFirst({ where: { key: humanKey.value } })
)
.get("/games", () => prisma.game.findMany())
.get("/instances", ({ query: { game } }) =>
prisma.instance.findMany({
where: {
game: {
name: game,
},
},
select: {
id: true,
},
})
)
.use(simpleApi);
export default api;
export type Api = typeof api;