hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | export type PageParams = { |
| 2 | limit?: number; |
| 3 | offset?: number; |
| 4 | }; |
| 5 | |
| 6 | export type Page<T> = { |
| 7 | items: T[]; |
| 8 | total: number; |
| 9 | limit: number | null; |
| 10 | offset: number; |
| 11 | }; |
| 12 | |
| 13 | /** |
| 14 | * A list route returned a successful response, but not the list shape the UI |
| 15 | * can safely render. Keeping this distinct from an empty list prevents a |
| 16 | * backend DTO regression from looking like "there is no data". |
| 17 | */ |
| 18 | export class PageShapeError extends Error { |
| 19 | constructor() { |
| 20 | super("Unexpected paginated response from the server"); |
| 21 | this.name = "PageShapeError"; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Schedule-window filters, UTC unix ms. Only `/events` and `/exams` honour |
| 27 | * them; all are optional and AND-ed when several are sent, and a row with no |
| 28 | * schedule at all is excluded by any of them. Sending any flips the server |
| 29 | * order to ASCENDING by schedule (soonest first) instead of newest-created. |
| 30 | */ |
| 31 | export type ScheduleWindowParams = { |
| 32 | /** Keeps rows that have not begun: `starts_at > T`. */ |
| 33 | starts_after?: number; |
| 34 | /** Keeps rows whose window has not finished: `ends_at > T`, else `starts_at > T`. */ |
| 35 | ends_after?: number; |
| 36 | /** Keeps rows that have already begun or begin earlier: `starts_at < T`. */ |
| 37 | starts_before?: number; |
| 38 | /** Keeps rows whose window has already finished: `ends_at < T`. */ |
| 39 | ends_before?: number; |
| 40 | }; |
| 41 | |
| 42 | export function pageQuery(params?: PageParams & ScheduleWindowParams): string { |
| 43 | if (!params) return ""; |
| 44 | const query = new URLSearchParams(); |
| 45 | // `!= null` is load-bearing: the API 400s on a present-but-empty value |
| 46 | // (`?limit=`), so an undefined param must drop the key, never emit `key=`. |
| 47 | if (params.limit != null) query.set("limit", String(params.limit)); |
| 48 | if (params.offset != null) query.set("offset", String(params.offset)); |
| 49 | if (params.starts_after != null) query.set("starts_after", String(params.starts_after)); |
| 50 | if (params.ends_after != null) query.set("ends_after", String(params.ends_after)); |
| 51 | if (params.starts_before != null) query.set("starts_before", String(params.starts_before)); |
| 52 | if (params.ends_before != null) query.set("ends_before", String(params.ends_before)); |
| 53 | const value = query.toString(); |
| 54 | return value ? `?${value}` : ""; |
| 55 | } |
| 56 | |
| 57 | export function appendPageParams(base: URLSearchParams, params?: PageParams): void { |
| 58 | if (!params) return; |
| 59 | if (params.limit != null) base.set("limit", String(params.limit)); |
| 60 | if (params.offset != null) base.set("offset", String(params.offset)); |
| 61 | } |
| 62 | |
| 63 | /** Accept the documented page envelope or a legacy bare array during backend rollouts. */ |
| 64 | export function normalizePage<T>(data: unknown): Page<T> { |
| 65 | if (Array.isArray(data)) { |
| 66 | return { items: data as T[], total: data.length, limit: null, offset: 0 }; |
| 67 | } |
| 68 | if (data && typeof data === "object") { |
| 69 | const record = data as Record<string, unknown>; |
| 70 | if (Array.isArray(record.items)) { |
| 71 | const items = record.items as T[]; |
| 72 | const total = typeof record.total === "number" ? record.total : items.length; |
| 73 | const limit = typeof record.limit === "number" ? record.limit : null; |
| 74 | const offset = typeof record.offset === "number" ? record.offset : 0; |
| 75 | return { items, total, limit, offset }; |
| 76 | } |
| 77 | } |
| 78 | throw new PageShapeError(); |
| 79 | } |