/** * Backwards-compatible list pagination: without limit/offset the response is * the full list (total === items.length); with them, a page plus the total. */ export function applyPagination( items: T[], query: { limit?: string; offset?: string }, ): { items: T[]; total: number } { const total = items.length const limitRaw = query.limit !== undefined ? Number(query.limit) : NaN const offsetRaw = query.offset !== undefined ? Number(query.offset) : NaN if (!Number.isFinite(limitRaw) && !Number.isFinite(offsetRaw)) { return { items, total } } const limit = Number.isFinite(limitRaw) ? Math.max(1, Math.min(1000, Math.floor(limitRaw))) : items.length const offset = Number.isFinite(offsetRaw) ? Math.max(0, Math.floor(offsetRaw)) : 0 return { items: items.slice(offset, offset + limit), total } }