import {z} from 'zod' export type GetApiV2LessonsArgs = {'page[size]'?: number | undefined, 'page[cursor]'?: (string | null) | undefined, 'filter[course_id]'?: (string | null) | undefined, 'filter[type]'?: ('ASSET' | 'HTML' | 'QUIZ' | 'WEB_PACKAGE' | 'VILT' | 'IE_EXAM' | 'WIDGET' | 'MODULAR' | null) | undefined, 'filter[title]'?: (string | null) | undefined, 'filter[updated_since]'?: (string | null) | undefined}; export type GetApiV2LessonsOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const lessonAttributes = z.object({title: z.string(), type: z.string(), order: z.number().int(), course_id: z.string(), description_html: z.string().optional(), content_html: z.string().nullable().optional(), quiz_id: z.string().nullable().optional(), web_package_id: z.string().nullable().optional(), height_px: z.number().int().nullable().optional(), width_px: z.number().int().nullable().optional(), external_id: z.string(), created_at: z.string(), modified_at: z.string()}); export const lessonResource = z.object({type: z.literal("lessons").optional(), id: z.string(), attributes: lessonAttributes, 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 paginatedListResponseLessonResource = z.object({data: z.array(lessonResource), 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 lessons * * GET /v2/lessons/ */ export const getApiV2Lessons = async (args: GetApiV2LessonsArgs, options: GetApiV2LessonsOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") 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 (args['filter[course_id]'] != null) query.append("filter[course_id]", String(args['filter[course_id]'])) if (args['filter[type]'] != null) query.append("filter[type]", String(args['filter[type]'])) if (args['filter[title]'] != null) query.append("filter[title]", String(args['filter[title]'])) if (args['filter[updated_since]'] != null) query.append("filter[updated_since]", String(args['filter[updated_since]'])) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/lessons/${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(paginatedListResponseLessonResource, await res.json()) };