eris/api/sessionsRoute.ts

44 lines
988 B
TypeScript
Raw Permalink Normal View History

import { Elysia, NotFoundError, t } from "elysia";
2023-10-05 09:00:51 +00:00
import { ulid } from "ulid";
export const sessions = new Map<string, Session>();
export interface Session {
2023-10-19 21:37:03 +00:00
userId?: number | undefined;
2023-10-05 09:00:51 +00:00
}
export const sessionsRoute = new Elysia()
.post(
"",
async () => {
const id = ulid();
const session: Session = {};
sessions.set(id, session);
return { id, userId: session.userId ?? null };
},
{
response: t.Object({
id: t.String(),
userId: t.Nullable(t.Number()),
}),
},
)
.get(
"/:sessionId",
async ({ params }) => {
const id = params.sessionId!;
const session = sessions.get(id);
if (!session) {
throw new NotFoundError("Session not found");
}
return { id, userId: session.userId ?? null };
},
{
params: t.Object({ sessionId: t.String() }),
response: t.Object({
id: t.String(),
userId: t.Nullable(t.Number()),
}),
},
);