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

37 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-10-08 21:23:54 +00:00
import { createEndpoint, createMethodFilter, createPathFilter } from "t_rest/server";
2023-10-05 09:00:51 +00:00
import { ulid } from "ulid";
export const sessions = new Map<string, Session>();
export interface Session {
userId?: number;
}
2023-10-08 21:23:54 +00:00
export const sessionsRoute = createPathFilter({
"": createMethodFilter({
POST: createEndpoint(
{ query: null, body: null },
async () => {
const id = ulid();
const session: Session = {};
sessions.set(id, session);
return { status: 200, body: { type: "application/json", data: { id, ...session } } };
},
),
}),
"{sessionId}": createMethodFilter({
GET: createEndpoint(
{ query: null, body: null },
async ({ params }) => {
const id = params.sessionId;
const session = sessions.get(id);
if (!session) {
return { status: 401, body: { type: "text/plain", data: "Session not found" } };
}
return { status: 200, body: { type: "application/json", data: { id, ...session } } };
},
),
}),
});