import {z} from 'zod' export type QuestionAnswerWithoutLessonQuestion = {id?: string | undefined, answer: string, position?: number | undefined, correct?: boolean | undefined, next_lesson?: (string | null) | undefined, explanation?: (string | null) | undefined}; export type QuestionTitleAnswerOnly = {id?: (string | null) | undefined, title: string, answers?: Array | undefined, type?: ('multi_select' | 'multi_choice') | undefined} | null; export type EditorState = {id?: string | undefined, note_editor_state?: (string | null) | undefined, summary_editor_state?: (string | null) | undefined, question_editor_state?: (string | null) | undefined, quiz_editor_state?: (string | null) | undefined, quiz_message_state?: (string | null) | undefined}; export type LessonWrite = {question?: QuestionTitleAnswerOnly | undefined, title?: string | undefined, subtitle?: string | undefined, note?: (string | null) | undefined, summary?: string | undefined, written_content?: (string | null) | undefined, includes_video?: boolean | undefined, show_to_browser_link_in_slack?: boolean | undefined, video_url?: (string | null) | undefined, video_position?: (string | null) | undefined, curriculum?: string | undefined, position?: number | undefined, editor_content?: EditorState | undefined, is_simple_form?: boolean | undefined}; export type PatchApiApiLessonsIdArgs = {body: LessonWrite}; export type PatchApiApiLessonsIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const questionAnswerWithoutLessonQuestion = z.object({id: z.string().optional(), answer: z.string().min(1).max(150), position: z.number().int().gte(-32768).lte(32767).optional(), correct: z.boolean().optional(), next_lesson: z.string().nullable().optional(), explanation: z.string().nullable().optional()}); export const questionTitleAnswerOnly = z.object({id: z.string().nullable().optional(), title: z.string().min(1), answers: z.array(questionAnswerWithoutLessonQuestion).optional(), type: z.enum(["multi_select", "multi_choice"]).optional()}).nullable(); export const editorState = z.object({id: z.string().optional(), note_editor_state: z.string().nullable().optional(), summary_editor_state: z.string().nullable().optional(), question_editor_state: z.string().nullable().optional(), quiz_editor_state: z.string().nullable().optional(), quiz_message_state: z.string().nullable().optional()}); export const lessonWrite = z.object({question: questionTitleAnswerOnly.optional(), title: z.string().optional(), subtitle: z.string().optional(), note: z.string().nullable().optional(), summary: z.string().optional(), written_content: z.string().nullable().optional(), includes_video: z.boolean().optional(), show_to_browser_link_in_slack: z.boolean().optional(), video_url: z.string().nullable().optional(), video_position: z.string().nullable().optional(), curriculum: z.string().optional(), position: z.number().int().optional(), editor_content: editorState.optional(), is_simple_form: z.boolean().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 }; /** * PATCH /api/lessons/{id} */ export const patchApiApiLessonsId = async (args: PatchApiApiLessonsIdArgs, options?: PatchApiApiLessonsIdOptions) => { const {baseUrl = "https://app.haekka.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/api/lessons/${encodeURIComponent(String(args.id))}${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(lessonWrite, await res.json()) };