import {z} from 'zod' export type QuizQuestionAnswer = {id?: string | undefined, created?: string | undefined, modified?: string | undefined, answer: string, position?: number | undefined, correct?: (boolean | null) | undefined, explanation?: (string | null) | undefined, question?: string | undefined}; export type QuestionEditorStateSerializer = {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, content?: Array | undefined}; export type QuizQuestionWrite = {id?: string | undefined, answers?: Array | undefined, editor_content?: QuestionEditorStateSerializer | undefined, image_ids?: (Array) | undefined, custom_video_id?: string | undefined, created?: string | undefined, modified?: string | undefined, position?: number | undefined, quiz_question_context?: (string | null) | undefined, answer_type?: ('no_action' | 'user_acknowledged' | 'multiple_choice' | 'neutral' | 'written_response' | 'external_url' | null) | undefined, external_url?: (string | null) | undefined, external_url_btn_text?: (string | null) | undefined, single_select?: boolean | undefined, quiz: string}; export type UpdateApiApiQuestionsQuizArgs = {body: QuizQuestionWrite}; export type UpdateApiApiQuestionsQuizOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const quizQuestionAnswer = z.object({id: z.string().optional(), created: z.string().optional(), modified: z.string().optional(), answer: z.string().min(1).max(150), position: z.number().int().gte(-32768).lte(32767).optional(), correct: z.boolean().nullable().optional(), explanation: z.string().nullable().optional(), question: z.string().optional()}); export const questionEditorStateSerializer = 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(), content: z.array(z.string()).optional()}); export const quizQuestionWrite = z.object({id: z.string().optional(), answers: z.array(quizQuestionAnswer).optional(), editor_content: questionEditorStateSerializer.optional(), image_ids: z.array(z.string().nullable()).optional(), custom_video_id: z.string().min(1).optional(), created: z.string().optional(), modified: z.string().optional(), position: z.number().int().gte(-32768).lte(32767).optional(), quiz_question_context: z.string().nullable().optional(), answer_type: z.enum(["no_action", "user_acknowledged", "multiple_choice", "neutral", "written_response", "external_url"]).nullable().optional(), external_url: z.string().max(200).nullable().optional(), external_url_btn_text: z.string().nullable().optional(), single_select: z.boolean().optional(), quiz: z.string()}); 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 }; /** * PUT /api/questions/quiz */ export const updateApiApiQuestionsQuiz = async (args: UpdateApiApiQuestionsQuizArgs, options?: UpdateApiApiQuestionsQuizOptions) => { 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/questions/quiz${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(quizQuestionWrite, await res.json()) };