Rename AcountSession to ServerSession
All checks were successful
/ build (push) Successful in 1m12s
/ deploy (push) Successful in 16s

Start the work of clearly distingushing client side types, server side
types and types shared over the API by renaming "AccountSession" and
"Session" names used on the server to "ServerSession".
This commit is contained in:
Hornwitser 2025-06-09 16:51:05 +02:00
parent 16191a8dd2
commit 251e83f640
15 changed files with 39 additions and 36 deletions

View file

@ -1,8 +1,7 @@
import { readAccounts } from "~/server/database"
import { requireAccountSession } from "~/server/utils/session";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const session = await requireServerSession(event);
const accounts = await readAccounts();
const account = accounts.find(a => a.id === session.accountId);
if (!account) {

View file

@ -1,7 +1,7 @@
import { deleteDatbase, readAccounts } from "~/server/database";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const session = await requireServerSession(event);
let accounts = await readAccounts();
const sessionAccount = accounts.find(
account => account.id === session.accountId

View file

@ -5,11 +5,11 @@ import {
import { cancelAccountStreams } from "~/server/streams";
export default defineEventHandler(async (event) => {
const accountSession = await requireAccountSession(event);
const serverSession = await requireServerSession(event);
let accounts = await readAccounts();
const sessionAccount = accounts.find(
account => account.id === accountSession.accountId
account => account.id === serverSession.accountId
);
if (!sessionAccount) {
throw Error("Account does not exist");
@ -19,13 +19,13 @@ export default defineEventHandler(async (event) => {
const removedSessionIds = new Set<number>();
let sessions = await readSessions();
sessions = sessions.filter(session => {
if (session.accountId === accountSession.accountId) {
if (session.accountId === serverSession.accountId) {
removedSessionIds.add(session.id);
return false;
}
return true;
});
cancelAccountStreams(accountSession.accountId);
cancelAccountStreams(serverSession.accountId);
await writeSessions(sessions);
await deleteCookie(event, "session");
@ -37,7 +37,7 @@ export default defineEventHandler(async (event) => {
await writeSubscriptions(subscriptions);
// Remove the account
accounts = accounts.filter(account => account.id !== accountSession.accountId);
accounts = accounts.filter(account => account.id !== serverSession.accountId);
await writeAccounts(accounts);
// Update Schedule counts.

View file

@ -3,7 +3,7 @@ import { readAccounts, writeAccounts } from "~/server/database";
import { DateTime } from "luxon";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const session = await requireServerSession(event);
const body: Pick<Account, "interestedIds" | "timezone"> = await readBody(event);
if (
body.interestedIds !== undefined

View file

@ -2,7 +2,7 @@ import { readAccounts, writeAccounts, nextAccountId } from "~/server/database";
import { Account } from "~/shared/types/account";
export default defineEventHandler(async (event) => {
let session = await getAccountSession(event);
let session = await getServerSession(event);
if (session) {
throw createError({
status: 409,
@ -49,5 +49,5 @@ export default defineEventHandler(async (event) => {
accounts.push(account);
await writeAccounts(accounts);
await setAccountSession(event, account.id);
await setServerSession(event, account.id);
})

View file

@ -14,5 +14,5 @@ export default defineEventHandler(async (event) => {
return new Response(undefined, { status: 403 })
}
await setAccountSession(event, account.id);
await setServerSession(event, account.id);
})

View file

@ -2,7 +2,7 @@ import { readAccounts } from "~/server/database";
import { cancelSessionStreams } from "~/server/streams";
export default defineEventHandler(async (event) => {
const session = await getAccountSession(event);
const session = await getServerSession(event);
if (session) {
const accounts = await readAccounts();
const account = accounts.find(
@ -19,5 +19,5 @@ export default defineEventHandler(async (event) => {
if (session) {
cancelSessionStreams(session.id);
}
await clearAccountSession(event);
await clearServerSession(event);
})

View file

@ -2,7 +2,7 @@ import { readAccounts, readSubscriptions } from "~/server/database";
import { AccountSession } from "~/shared/types/account";
export default defineEventHandler(async (event): Promise<AccountSession | undefined> => {
const session = await getAccountSession(event);
const session = await getServerSession(event);
if (!session)
return;
const accounts = await readAccounts();
@ -11,7 +11,7 @@ export default defineEventHandler(async (event): Promise<AccountSession | undefi
subscriptions.find(sub => sub.type === "push" && sub.sessionId === session.id)
);
await refreshAccountSession(event, session);
await refreshServerSession(event, session);
return {
id: session.id,

View file

@ -3,7 +3,7 @@ import { addStream, deleteStream } from "~/server/streams";
import { readAccounts } from "~/server/database";
export default defineEventHandler(async (event) => {
const session = await getAccountSession(event);
const session = await getServerSession(event);
let accountId: number | undefined;
if (session) {
const accounts = await readAccounts()

View file

@ -38,7 +38,7 @@ function isPatch(data: unknown): SchedulePatch {
}
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const session = await requireServerSession(event);
const accounts = await readAccounts();
const account = accounts.find(a => a.id === session.accountId);
if (!account) {

View file

@ -3,7 +3,7 @@ import { Account } from "~/shared/types/account";
import { canSeeCrew } from "../utils/schedule";
export default defineEventHandler(async (event) => {
const session = await getAccountSession(event);
const session = await getServerSession(event);
let account: Account | undefined;
if (session) {
const accounts = await readAccounts()

View file

@ -2,7 +2,7 @@ import { readSubscriptions, writeSubscriptions } from "~/server/database";
import { Subscription } from "~/shared/types/account";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const session = await requireServerSession(event);
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
const subscriptions = await readSubscriptions();
const existingIndex = subscriptions.findIndex(

View file

@ -1,7 +1,7 @@
import { readSubscriptions, writeSubscriptions } from "~/server/database";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const session = await requireServerSession(event);
const subscriptions = await readSubscriptions();
const existingIndex = subscriptions.findIndex(
sub => sub.type === "push" && sub.sessionId === session.id

View file

@ -1,8 +1,13 @@
import { readFile, unlink, writeFile } from "node:fs/promises";
import { Schedule } from "~/shared/types/schedule";
import { Account, Subscription, Session } from "~/shared/types/account";
import { Account, Subscription } from "~/shared/types/account";
import { generateDemoSchedule, generateDemoAccounts } from "./generate-demo-schedule";
export interface ServerSession {
id: number,
accountId: number,
};
// For this demo I'm just storing the runtime data in JSON files. When making
// this into proper application this should be replaced with an actual database.
@ -87,9 +92,9 @@ export async function nextSessionId() {
}
export async function readSessions() {
return await readJson<Session[]>(sessionsPath, []);
return await readJson<ServerSession[]>(sessionsPath, []);
}
export async function writeSessions(sessions: Session[]) {
export async function writeSessions(sessions: ServerSession[]) {
await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8");
}

View file

@ -1,6 +1,5 @@
import type { H3Event } from "h3";
import { nextSessionId, readSessions, readSubscriptions, writeSessions, writeSubscriptions } from "~/server/database";
import { Session } from "~/shared/types/account";
import { nextSessionId, readSessions, readSubscriptions, ServerSession, writeSessions, writeSubscriptions } from "~/server/database";
const oneYearSeconds = 365 * 24 * 60 * 60;
@ -13,7 +12,7 @@ async function removeSessionSubscription(sessionId: number) {
}
}
async function clearAccountSessionInternal(event: H3Event, sessions: Session[]) {
async function clearServerSessionInternal(event: H3Event, sessions: ServerSession[]) {
const existingSessionCookie = await getSignedCookie(event, "session");
if (existingSessionCookie) {
const sessionId = parseInt(existingSessionCookie, 10);
@ -27,19 +26,19 @@ async function clearAccountSessionInternal(event: H3Event, sessions: Session[])
return false;
}
export async function clearAccountSession(event: H3Event) {
export async function clearServerSession(event: H3Event) {
const sessions = await readSessions();
if (await clearAccountSessionInternal(event, sessions)) {
if (await clearServerSessionInternal(event, sessions)) {
await writeSessions(sessions);
}
deleteCookie(event, "session");
}
export async function setAccountSession(event: H3Event, accountId: number) {
export async function setServerSession(event: H3Event, accountId: number) {
const sessions = await readSessions();
await clearAccountSessionInternal(event, sessions);
await clearServerSessionInternal(event, sessions);
const newSession: Session = {
const newSession: ServerSession = {
accountId,
id: await nextSessionId(),
};
@ -49,11 +48,11 @@ export async function setAccountSession(event: H3Event, accountId: number) {
await setSignedCookie(event, "session", String(newSession.id), oneYearSeconds)
}
export async function refreshAccountSession(event: H3Event, session: Session) {
export async function refreshServerSession(event: H3Event, session: ServerSession) {
await setSignedCookie(event, "session", String(session.id), oneYearSeconds)
}
export async function getAccountSession(event: H3Event) {
export async function getServerSession(event: H3Event) {
const sessionCookie = await getSignedCookie(event, "session");
if (sessionCookie) {
const sessionId = parseInt(sessionCookie, 10);
@ -62,8 +61,8 @@ export async function getAccountSession(event: H3Event) {
}
}
export async function requireAccountSession(event: H3Event) {
const session = await getAccountSession(event);
export async function requireServerSession(event: H3Event) {
const session = await getServerSession(event);
if (!session)
throw createError({
status: 401,