import {z} from 'zod' export type RatingConfig = {rating_question?: (string | null) | undefined, additional_feedback_question?: (string | null) | undefined, include_additional_feedback?: (boolean | null) | undefined, rating_scale_low_label?: (string | null) | undefined, rating_scale_high_label?: (string | null) | undefined, rating_scale?: (5 | 10 | null) | undefined}; export type FlashcardCard = {id?: (string | null) | undefined, term: string, definition: string}; export type FlashcardConfig = {id?: (string | null) | undefined, header?: string | undefined, subheader?: string | undefined, cards?: Array | undefined}; export type ContentItemRequest = {id?: (string | null) | undefined, type: 'HTML' | 'VIDEO' | 'AUDIO' | 'QUIZ' | 'RATING' | 'FLASHCARD', header?: string | undefined, content_html?: (string | null) | undefined, asset_id?: (string | null) | undefined, quiz_id?: (string | null) | undefined, rating?: (RatingConfig | null) | undefined, flashcard?: (FlashcardConfig | null) | undefined}; export type LessonUpdateRequest = {title?: string | undefined, description_html?: string | undefined, content_html?: (string | null) | undefined, order?: number | undefined, content_items?: (Array | null) | undefined, quiz_id?: string | undefined, web_package_id?: string | undefined, height_px?: (number | null) | undefined, width_px?: (number | null) | undefined}; export type LessonBatchUpdateData = {type: 'lessons', id: string, attributes: LessonUpdateRequest}; export type LessonBatchUpdateRequestEnvelope = {data: Array}; export type PatchApiV2LessonsArgs = {body: LessonBatchUpdateRequestEnvelope}; export type PatchApiV2LessonsOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ratingConfig = z.object({rating_question: z.string().nullable().optional(), additional_feedback_question: z.string().nullable().optional(), include_additional_feedback: z.boolean().nullable().optional(), rating_scale_low_label: z.string().nullable().optional(), rating_scale_high_label: z.string().nullable().optional(), rating_scale: z.union([z.literal(5), z.literal(10)]).nullable().optional()}); export const flashcardCard = z.object({id: z.string().nullable().optional(), term: z.string().max(50), definition: z.string().max(350)}); export const flashcardConfig = z.object({id: z.string().nullable().optional(), header: z.string().max(200).optional(), subheader: z.string().max(1000).optional(), cards: z.array(flashcardCard).optional()}); export const contentItemResource = z.object({id: z.string(), type: z.string(), header: z.string().optional(), content_html: z.string().nullable().optional(), asset_id: z.string().nullable().optional(), quiz_id: z.string().nullable().optional(), rating: ratingConfig.nullable().optional(), flashcard: flashcardConfig.nullable().optional()}); export const lessonDetailAttributes = 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(), content_items: z.array(contentItemResource).nullable().optional()}); export const lessonDetailResource = z.object({type: z.literal("lessons").optional(), id: z.string(), attributes: lessonDetailAttributes, relationships: z.record(z.string(), z.unknown()).nullable().optional(), links: z.record(z.string(), z.unknown()).nullable().optional()}); export const batchSucceededItemLessonDetailResource = z.object({status: z.literal("succeeded").optional(), id: z.string().nullable().optional(), result: lessonDetailResource}); export const batchItemError = z.object({code: z.enum(["duplicate_email", "duplicate_in_batch", "duplicate_name", "validation_error", "not_found", "internal_error", "not_in_domain", "already_enrolled", "already_published", "already_in_path"]), source: z.record(z.string(), z.unknown()).nullable().optional(), detail: z.string().nullable().optional()}); export const batchFailedItem = z.object({status: z.literal("failed").optional(), id: z.string().nullable().optional(), error: batchItemError}); export const batchSummary = z.object({total: z.number().int().gte(0), succeeded: z.number().int().gte(0), failed: z.number().int().gte(0)}); export const batchResultEnvelopeLessonDetailResource = z.object({data: z.array(z.union([batchSucceededItemLessonDetailResource, batchFailedItem])), summary: batchSummary}); 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 }; /** * Update lessons * * PATCH /v2/lessons/ */ export const patchApiV2Lessons = async (args: PatchApiV2LessonsArgs, options: PatchApiV2LessonsOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/lessons/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(batchResultEnvelopeLessonDetailResource, await res.json()) };