import {z} from 'zod' export type CustomObjectRecord = {created_at?: string | undefined, created_by_user_id?: string | undefined, custom_object_fields?: {[key: string]: unknown} | undefined, custom_object_key?: string | undefined, external_id?: (string | null) | undefined, id?: string | undefined, name: string, photo?: ({[key: string]: unknown} | null) | undefined, updated_at?: string | undefined, updated_by_user_id?: string | undefined, url?: string | undefined}; export type CustomObjectRecordsBulkCreateRequest = {job?: ({action?: string | undefined, items?: Array | undefined}) | undefined}; export type CreateApiApiV2CustomObjectsCustomObjectKeyJobsArgs = {custom_object_key: string, body: CustomObjectRecordsBulkCreateRequest}; export type CreateApiApiV2CustomObjectsCustomObjectKeyJobsOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const customObjectRecord = z.object({created_at: z.string().optional(), created_by_user_id: z.string().optional(), custom_object_fields: z.record(z.string(), z.unknown()).optional(), custom_object_key: z.string().optional(), external_id: z.string().nullable().optional(), id: z.string().optional(), name: z.string(), photo: z.record(z.string(), z.unknown()).nullable().optional(), updated_at: z.string().optional(), updated_by_user_id: z.string().optional(), url: z.string().optional()}); export const customObjectRecordsJobsResponse = z.object({job_status: z.object({id: z.string().optional(), message: z.string().nullable().optional(), progress: z.number().int().nullable().optional(), results: z.array(customObjectRecord).nullable().optional(), status: z.string().optional(), total: z.number().int().optional(), url: z.string().optional()}).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 }; /** * Custom Object Record Bulk Jobs * * POST /api/v2/custom_objects/{custom_object_key}/jobs */ export const createApiApiV2CustomObjectsCustomObjectKeyJobs = async (args: CreateApiApiV2CustomObjectsCustomObjectKeyJobsArgs, options?: CreateApiApiV2CustomObjectsCustomObjectKeyJobsOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/custom_objects/${encodeURIComponent(String(args.custom_object_key))}/jobs${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(customObjectRecordsJobsResponse, await res.json()) };