forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

getCourses.ts35 lines, 1.4 KiBHistoryRawDownload
1import { client } from "../client";
2import { appendPageParams, normalizePage, type Course, type Page, type PageParams } from "../client";
3import type { CourseKind } from "../client";
4
5/**
6 * `GET /courses` list filters. `kind` keeps one course kind, `q` is the
7 * server-side catalog search, `taught` keeps courses with (`true`) or without
8 * (`false`) an attached şube. All optional, AND-ed with each other and with
9 * `limit`/`offset`; an omitted key filters nothing.
10 */
11export type CourseListParams = PageParams & {
12 kind?: CourseKind;
13 q?: string;
14 taught?: boolean;
15};
16
17export function courseListQuery(params?: CourseListParams): string {
18 if (!params) return "";
19 const query = new URLSearchParams();
20 appendPageParams(query, params);
21 if (params.kind != null) query.set("kind", params.kind);
22 // Trimmed here so every caller sends the same thing; blank drops the key —
23 // the API 400s on a present-but-empty value (`?q=`).
24 const q = params.q?.trim();
25 if (q) query.set("q", q);
26 // Serializes as `taught=true` / `taught=false`; unset drops the key (both).
27 if (params.taught != null) query.set("taught", String(params.taught));
28 const value = query.toString();
29 return value ? `?${value}` : "";
30}
31
32export async function getCourses(params?: CourseListParams, signal?: AbortSignal): Promise<Page<Course>> {
33 const data = await client<unknown>(`/courses${courseListQuery(params)}`, { signal });
34 return normalizePage<Course>(data);
35}