forked from pinks/eris
1
0
Fork 0
nyx/api/sessionsRoute.ts

44 lines
988 B
TypeScript
Raw 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()),
}),
},
);