import { z } from 'zod'; import type { Request } from 'express'; /** Shared pieces of v2 schemas. Dates go out as ISO 8601 strings (UTC). */ export const isoDate = z.iso.datetime(); export const id = z.string().min(1).max(64); export const idParams = z.object({ id: id.describe('The id') }); /** A Date (or date string) as ISO 8601, for building responses. */ export function iso(d: Date | string): string; export function iso(d: Date | string | null | undefined): string | null; export function iso(d: Date | string | null | undefined): string | null { return d == null ? null : new Date(d).toISOString(); } /** * Where a stored upload path is served: "/uploads/…" (as stored) → "/api/uploads/…" (2.2.0), so it * reaches the backend when the site and its API share one address. Anything else is unchanged. */ export function uploadPath(raw: string): string { return /^\/uploads\//.test(raw) ? `/api${raw}` : raw; } /** A stored picture path ("/uploads/…") as an absolute URL on the host the request came to. */ export function absoluteUrl(req: Request, url: string | null | undefined): string | null { const raw = String(url || '').trim(); if (!raw) return null; if (/^https?:\/\//.test(raw)) return raw; return `${req.protocol}://${req.get('host')}${uploadPath(raw.startsWith('/') ? raw : `/${raw}`)}`; } export const campusRef = z.object({ id, name: z.string(), slug: z.string() }); export const seriesRef = z.object({ id, name: z.string(), slug: z.string() }); export const eventRef = z.object({ id, title: z.string(), startDate: isoDate, endDate: isoDate });