import {z} from 'zod' export type GetApiCoursesSectionsArgs = {sort?: string | undefined, page?: number | undefined, per_page?: number | undefined, course_id?: string | undefined, seats?: string | undefined, open_seats?: string | undefined, waitlist?: string | undefined, semester?: string | undefined}; export type GetApiCoursesSectionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const meeting = z.object({days: z.string().optional(), room: z.string().optional(), building: z.string().optional(), classtype: z.string().optional(), start_time: z.string().optional(), end_time: z.string().optional()}); export const section = z.object({course: z.string().optional(), section_id: z.string().optional(), semester: z.number().optional(), number: z.string().optional(), seats: z.string().optional(), meetings: z.array(meeting).optional(), open_seats: z.string().optional(), waitlist: z.string().optional(), instructors: z.array(z.string()).optional()}); export const getApiCoursesSectionsResponse = z.array(section); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List of sections * * GET /courses/sections */ export const getApiCoursesSections = async (args: GetApiCoursesSectionsArgs, options?: GetApiCoursesSectionsOptions) => { const {baseUrl = "https://api.umd.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort != null) query.append("sort", String(args.sort)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.course_id != null) query.append("course_id", String(args.course_id)) if (args.seats != null) query.append("seats", String(args.seats)) if (args.open_seats != null) query.append("open_seats", String(args.open_seats)) if (args.waitlist != null) query.append("waitlist", String(args.waitlist)) if (args.semester != null) query.append("semester", String(args.semester)) const url = `${baseUrl.replace(/\/+$/, '')}/courses/sections${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiCoursesSectionsResponse, await res.json()) };