/** * Tiny helpers to parse + whitelist server-side sort parameters for the admin * list/export endpoints. Anything not on the per-endpoint allow-list falls * back to that endpoint's default column, so untrusted `sort`/`order` query * params can never reach PostgREST verbatim. */ export type SortOrder = 'asc' | 'desc'; export function parseOrder( v: string | null | undefined, fallback: SortOrder = 'desc', ): SortOrder { return v === 'asc' || v === 'desc' ? v : fallback; } export function parseSort( v: string | null | undefined, allowed: readonly T[], fallback: T, ): T { return v && (allowed as readonly string[]).includes(v) ? (v as T) : fallback; } export const USER_SORTS = [ 'email', 'created_at', 'last_sign_in_at', 'role', ] as const; export type UserSort = (typeof USER_SORTS)[number]; export const TUNNEL_SORTS = [ 'subdomain', 'bytes_used', 'quota_bytes', 'is_active', 'created_at', 'last_seen_at', 'usage_pct', ] as const; export type TunnelSort = (typeof TUNNEL_SORTS)[number]; export const AUDIT_SORTS = ['created_at', 'action', 'actor_email'] as const; export type AuditSort = (typeof AUDIT_SORTS)[number];