forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

page.ts73 lines, 2.7 KiBHistoryRawDownload
1export type PageParams = {
2 limit?: number;
3 offset?: number;
4};
5
6export 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 */
18export 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; both are optional and AND-ed when both sent, and a row with no
28 * schedule at all is excluded by either. Sending either flips the server
29 * order to ASCENDING by schedule (soonest first) instead of newest-created.
30 */
31export 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};
37
38export function pageQuery(params?: PageParams & ScheduleWindowParams): string {
39 if (!params) return "";
40 const query = new URLSearchParams();
41 // `!= null` is load-bearing: the API 400s on a present-but-empty value
42 // (`?limit=`), so an undefined param must drop the key, never emit `key=`.
43 if (params.limit != null) query.set("limit", String(params.limit));
44 if (params.offset != null) query.set("offset", String(params.offset));
45 if (params.starts_after != null) query.set("starts_after", String(params.starts_after));
46 if (params.ends_after != null) query.set("ends_after", String(params.ends_after));
47 const value = query.toString();
48 return value ? `?${value}` : "";
49}
50
51export function appendPageParams(base: URLSearchParams, params?: PageParams): void {
52 if (!params) return;
53 if (params.limit != null) base.set("limit", String(params.limit));
54 if (params.offset != null) base.set("offset", String(params.offset));
55}
56
57/** Accept the documented page envelope or a legacy bare array during backend rollouts. */
58export function normalizePage<T>(data: unknown): Page<T> {
59 if (Array.isArray(data)) {
60 return { items: data as T[], total: data.length, limit: null, offset: 0 };
61 }
62 if (data && typeof data === "object") {
63 const record = data as Record<string, unknown>;
64 if (Array.isArray(record.items)) {
65 const items = record.items as T[];
66 const total = typeof record.total === "number" ? record.total : items.length;
67 const limit = typeof record.limit === "number" ? record.limit : null;
68 const offset = typeof record.offset === "number" ? record.offset : 0;
69 return { items, total, limit, offset };
70 }
71 }
72 throw new PageShapeError();
73}