// --------------------------------------------------------------------------- // Sort and listing type validation // --------------------------------------------------------------------------- export type CovesSortType = 'hot' | 'new' | 'top' export type CovesTimeframe = 'day' | 'week' | 'month' | 'all' export type CovesListingType = 'discover' | 'timeline' export type CovesSortParams = | { sort: 'hot'; timeframe?: undefined } | { sort: 'new'; timeframe?: undefined } | { sort: 'top'; timeframe: CovesTimeframe } const VALID_SORTS: ReadonlySet = new Set([ 'hot', 'new', 'top', ]) const VALID_TIMEFRAMES: ReadonlySet = new Set([ 'day', 'week', 'month', 'all', ]) function isValidSort(s: string): s is CovesSortType { return (VALID_SORTS as ReadonlySet).has(s) } function isValidTimeframe(t: string): t is CovesTimeframe { return (VALID_TIMEFRAMES as ReadonlySet).has(t) } /** * Validates and returns Coves API sort + timeframe parameters. * Falls back to `{ sort: 'hot' }` for invalid input. */ export function mapSort(sort: string, timeframe?: string): CovesSortParams { if (!isValidSort(sort)) { console.warn(`[sort] Invalid sort value "${sort}", falling back to "hot"`) return { sort: 'hot' } } if (sort === 'top') { if (timeframe && isValidTimeframe(timeframe)) { return { sort: 'top', timeframe } } if (timeframe) { console.warn( `[sort] Invalid timeframe "${timeframe}", falling back to "all"`, ) } return { sort: 'top', timeframe: 'all' } } return { sort } } /** * Validates and returns Coves listing type. * Falls back to `'discover'` for invalid input or unauthenticated timeline requests. */ export function mapListing( listing: string, isAuthenticated: boolean, ): CovesListingType { if (listing === 'timeline') return isAuthenticated ? 'timeline' : 'discover' if (listing !== 'discover') { console.warn( `[sort] Invalid listing type "${listing}", falling back to "discover"`, ) } return 'discover' }