import {z} from 'zod' export type AttachmentBaseObject = {content_type?: string | undefined, content_url?: string | undefined, deleted?: boolean | undefined, file_name?: string | undefined, height?: number | undefined, id?: number | undefined, inline?: boolean | undefined, malware_access_override?: boolean | undefined, malware_scan_result?: string | undefined, mapped_content_url?: string | undefined, size?: number | undefined, url?: string | undefined, width?: number | undefined}; export type AttachmentObject = {content_type?: string | undefined, content_url?: string | undefined, deleted?: boolean | undefined, file_name?: string | undefined, height?: number | undefined, id?: number | undefined, inline?: boolean | undefined, malware_access_override?: boolean | undefined, malware_scan_result?: string | undefined, mapped_content_url?: string | undefined, size?: number | undefined, url?: string | undefined, width?: number | undefined, thumbnails?: Array | undefined}; export type TicketAuditViaObject = {channel?: string | undefined, source?: {[key: string]: unknown} | undefined}; export type TicketImportInput = {assignee_id?: number | undefined, comments?: (Array<{value?: string | undefined, attachments?: Array | undefined, audit_id?: number | undefined, author_id?: number | undefined, body?: string | undefined, created_at?: string | undefined, html_body?: string | undefined, id?: number | undefined, metadata?: {[key: string]: unknown} | undefined, plain_body?: string | undefined, public?: boolean | undefined, type?: string | undefined, uploads?: Array | undefined, via?: TicketAuditViaObject | undefined}>) | undefined, description?: string | undefined, requester_id?: number | undefined, subject?: string | undefined, tags?: Array | undefined}; export type TicketBulkImportRequest = {tickets?: Array | undefined}; export type CreateApiApiV2ImportsTicketsCreateManyArgs = {archive_immediately?: boolean | undefined, body: TicketBulkImportRequest}; export type CreateApiApiV2ImportsTicketsCreateManyOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const jobStatusResultObject = z.union([z.object({id: z.number().int(), index: z.number().int()}).and(z.record(z.string(), z.unknown())), z.object({action: z.string(), id: z.number().int(), status: z.string(), success: z.boolean()}).and(z.record(z.string(), z.unknown())), z.object({action: z.string(), details: z.string(), error: z.string(), id: z.number().int(), success: z.boolean()}).and(z.record(z.string(), z.unknown()))]); export const jobStatusObject = z.object({id: z.string().optional(), job_type: z.string().optional(), message: z.string().nullable().optional(), progress: z.number().int().nullable().optional(), results: z.union([z.array(jobStatusResultObject).nullable(), z.object({success: z.boolean()})]).optional(), status: z.string().optional(), total: z.number().int().nullable().optional(), url: z.string().optional()}); export const jobStatusResponse = z.object({job_status: jobStatusObject.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 }; /** * Ticket Bulk Import * * POST /api/v2/imports/tickets/create_many */ export const createApiApiV2ImportsTicketsCreateMany = async (args: CreateApiApiV2ImportsTicketsCreateManyArgs, options?: CreateApiApiV2ImportsTicketsCreateManyOptions) => { 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 (args.archive_immediately != null) query.append("archive_immediately", String(args.archive_immediately)) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/imports/tickets/create_many${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(jobStatusResponse, await res.json()) };