import {z} from 'zod' export type GetApiV2PublishedCoursesArgs = {'filter[domain]'?: (string | null) | undefined, 'filter[course]'?: (string | null) | undefined, 'filter[live]'?: (boolean | null) | undefined, include?: (string | null) | undefined, 'page[size]'?: number | undefined, 'page[cursor]'?: (string | null) | undefined}; export type GetApiV2PublishedCoursesOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const publishedCourseAttributes = z.object({external_id: z.string(), slug: z.string(), live: z.boolean(), is_hidden: z.boolean(), visible_on_catalog: z.boolean(), open_access: z.boolean(), strict_enforce_group_visibility: z.boolean(), visibility_override_type: z.enum(["GROUP", "CATEGORY"]), access_period_starts_at: z.string().nullable().optional(), access_period_ends_at: z.string().nullable().optional(), restrict_access_start_end_dates: z.boolean(), allow_self_service_reenroll: z.boolean(), unique_progress_per_enrollment: z.boolean(), require_all_prerequisites: z.boolean(), created_at: z.string(), modified_at: z.string()}); export const publishedCourseResource = z.object({type: z.string().optional(), id: z.string(), attributes: publishedCourseAttributes, relationships: z.record(z.string(), z.unknown()).nullable().optional(), links: z.record(z.string(), z.unknown()).nullable().optional()}); export const paginationLinks = z.object({self: z.string().nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional()}); export const paginatedListResponsePublishedCourseResource = z.object({data: z.array(publishedCourseResource), meta: z.record(z.string(), z.unknown()).nullable().optional(), links: paginationLinks.nullable().optional(), included: z.array(z.record(z.string(), z.unknown())).nullable().optional()}); 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 published courses * * GET /v2/published-courses/ */ export const getApiV2PublishedCourses = async (args: GetApiV2PublishedCoursesArgs, options: GetApiV2PublishedCoursesOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['filter[domain]'] != null) query.append("filter[domain]", String(args['filter[domain]'])) if (args['filter[course]'] != null) query.append("filter[course]", String(args['filter[course]'])) if (args['filter[live]'] != null) query.append("filter[live]", String(args['filter[live]'])) if (args.include != null) query.append("include", String(args.include)) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[cursor]'] != null) query.append("page[cursor]", String(args['page[cursor]'])) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/published-courses/${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(paginatedListResponsePublishedCourseResource, await res.json()) };