import {z} from 'zod' export type CreateFlashcardRequest = {front: string, back: string, revision_type?: ('unit' | 'course_global' | null) | undefined}; export type CreateApiTeacherCoursesCourseIdFlashcardsArgs = {course_id: string, body: CreateFlashcardRequest}; export type CreateApiTeacherCoursesCourseIdFlashcardsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const flashcard = z.object({_id: z.string().optional(), course_id: z.string(), unit_id: z.string().nullable().optional(), user_id: z.string(), front: z.string(), back: z.string(), revision_type: z.enum(["unit", "course_global"]).nullable().optional(), anchor_unit_id: z.string().nullable().optional(), anchor_block_id: z.string().nullable().optional(), anchored_at: z.string().nullable().optional(), created_at: z.string().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 }; /** * Create Flashcard * * POST /teacher/courses/{course_id}/flashcards */ export const createApiTeacherCoursesCourseIdFlashcards = async (args: CreateApiTeacherCoursesCourseIdFlashcardsArgs, options: CreateApiTeacherCoursesCourseIdFlashcardsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/teacher/courses/${encodeURIComponent(String(args.course_id))}/flashcards${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(flashcard, await res.json()) };