import {z} from 'zod' export type GetApiOfferingCoursesCourseShortcodeStatisticsArgs = {courseShortcode: string, year?: string | undefined, teacherId?: string | undefined}; export type GetApiOfferingCoursesCourseShortcodeStatisticsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const teacher = z.object({id: z.number().int(), firstName: z.string(), lastName: z.string()}); export const gradeCount = z.object({grade: z.enum(["18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "30L"]), count: z.number().int()}); export const yearStatistics = z.object({succeeded: z.number().int(), failed: z.number().int(), grades: z.array(gradeCount), averageGrade: z.number().nullable()}); export const previousYearsToCompare = z.object({year: z.number().int(), succeeded: z.number().int(), failed: z.number().int()}); export const courseStatistics = z.object({shortcode: z.string(), year: z.number().int(), teacher: teacher, totalEnrolled: z.number().int(), totalSucceeded: z.number().int(), totalFailed: z.number().int(), firstYear: yearStatistics, otherYears: yearStatistics, previousYearsToCompare: z.array(previousYearsToCompare), years: z.array(z.number().int()), teachers: z.array(teacher)}); export const getApiOfferingCoursesCourseShortcodeStatisticsResponse = z.object({data: courseStatistics}); 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 }; /** * Retrieve course statistics | Recupera le statistiche dell'insegnamento * * GET /offering/courses/{courseShortcode}/statistics */ export const getApiOfferingCoursesCourseShortcodeStatistics = async (args: GetApiOfferingCoursesCourseShortcodeStatisticsArgs, options?: GetApiOfferingCoursesCourseShortcodeStatisticsOptions) => { const {baseUrl = "https://app.didattica.polito.it/api", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.year != null) query.append("year", String(args.year)) if (args.teacherId != null) query.append("teacherId", String(args.teacherId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/offering/courses/${encodeURIComponent(String(args.courseShortcode))}/statistics${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(getApiOfferingCoursesCourseShortcodeStatisticsResponse, await res.json()) };