import {z} from 'zod' export type CollaboratorObject = {email?: string | undefined, name?: string | undefined}; 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 TicketCommentObject = {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}; export type EmailCCObject = {action?: ('put' | 'delete') | undefined, user_email?: string | undefined, user_id?: string | undefined, user_name?: string | undefined}; export type FollowerObject = {action?: ('put' | 'delete') | undefined, user_email?: string | undefined, user_id?: string | undefined}; export type ViaObject = {channel?: string | undefined, source?: ({from?: ({address?: (string | null) | undefined, id?: (number | null) | undefined, name?: (string | null) | undefined, title?: (string | null) | undefined}) | undefined, rel?: (string | null) | undefined, to?: ({address?: string | undefined, name?: string | undefined}) | undefined} | {[key: string]: unknown}) | undefined}; export type TicketCreateInput = {additional_collaborators?: Array | undefined, assignee_email?: string | undefined, assignee_id?: number | undefined, attribute_value_ids?: Array | undefined, collaborator_ids?: Array | undefined, comment: TicketCommentObject, custom_fields?: (Array<{id?: number | undefined, value?: (string | number | boolean | Array | number) | undefined}>) | undefined, custom_status_id?: number | undefined, due_at?: (string | null) | undefined, email_ccs?: Array | undefined, external_id?: string | undefined, followers?: Array | undefined, group_id?: number | undefined, organization_id?: number | undefined, priority?: ('urgent' | 'high' | 'normal' | 'low') | undefined, problem_id?: number | undefined, requester_id?: number | undefined, safe_update?: boolean | undefined, sharing_agreement_ids?: Array | undefined, status?: ('new' | 'open' | 'pending' | 'hold' | 'solved' | 'closed') | undefined, subject?: string | undefined, tags?: Array | undefined, type?: ('problem' | 'incident' | 'question' | 'task') | undefined, updated_stamp?: string | undefined, brand_id?: number | undefined, collaborators?: Array | undefined, email_cc_ids?: Array | undefined, follower_ids?: Array | undefined, macro_ids?: Array | undefined, raw_subject?: string | undefined, recipient?: string | undefined, submitter_id?: number | undefined, ticket_form_id?: number | undefined, via?: ViaObject | undefined, via_followup_source_id?: number | undefined}; export type TicketsCreateRequest = {tickets?: Array | undefined}; export type CreateApiApiV2TicketsCreateManyArgs = {body: TicketsCreateRequest}; export type CreateApiApiV2TicketsCreateManyOptions = {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 }; /** * Create Many Tickets * * POST /api/v2/tickets/create_many */ export const createApiApiV2TicketsCreateMany = async (args: CreateApiApiV2TicketsCreateManyArgs, options?: CreateApiApiV2TicketsCreateManyOptions) => { 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/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()) };