// Express request helpers — shared query/param extraction. // // Centralizes patterns that were duplicated across route handlers // (3+ different ways to read `req.query.session`). import { isRecord } from "./types.js"; // `query: object` so the helpers work with any Express Request // generic — `Request`. A narrow // `Query` generic like `{ path?: string }` isn't assignable to // `Record` (no index signature), so we widen to // `object` and narrow again when reading a key. interface HasQuery { query: object; } function readQueryKey(queryObj: object, key: string): unknown { return isRecord(queryObj) ? queryObj[key] : undefined; } /** * Extract the session ID from `req.query.session`. * Returns the string value, or "" if missing/non-string. */ export function getSessionQuery(req: HasQuery): string { const raw = readQueryKey(req.query, "session"); return typeof raw === "string" ? raw : ""; } /** * Extract an optional string query parameter. * Returns the string value, or undefined if missing/non-string. */ export function getOptionalStringQuery(req: HasQuery, key: string): string | undefined { const raw = readQueryKey(req.query, key); return typeof raw === "string" ? raw : undefined; }