import {z} from 'zod' export type DuplicateAssignmentsBatchRequest = {folder_ids?: Array | undefined, school_year: string, dry_run?: boolean | undefined, include_already_duplicated?: boolean | undefined, assignment_ids?: Array | undefined}; export type CreateApiTeacherAssignmentsDuplicateBatchArgs = {body: DuplicateAssignmentsBatchRequest}; export type CreateApiTeacherAssignmentsDuplicateBatchOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const duplicationItemResponse = z.object({id: z.string(), title: z.string(), folder_id: z.string().nullable().optional()}); export const duplicationCreatedResponse = z.object({source_id: z.string(), id: z.string()}); export const duplicationFailureResponse = z.object({id: z.string(), title: z.string(), error: z.string()}); export const bulkDuplicationResponse = z.object({target_school_year: z.string(), to_copy: z.array(duplicationItemResponse), already_duplicated: z.array(duplicationItemResponse), created: z.array(duplicationCreatedResponse).optional(), failures: z.array(duplicationFailureResponse).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 }; /** * Duplicate Teacher Assignments Batch * * POST /teacher/assignments/duplicate-batch */ export const createApiTeacherAssignmentsDuplicateBatch = async (args: CreateApiTeacherAssignmentsDuplicateBatchArgs, options: CreateApiTeacherAssignmentsDuplicateBatchOptions) => { 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/assignments/duplicate-batch${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(bulkDuplicationResponse, await res.json()) };