import {z} from 'zod' export type GetApiApiV2IncrementalTicketEventsOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const ticketMetricEventBaseObject = z.object({deleted: z.boolean().optional(), id: z.number().int().optional(), instance_id: z.number().int().optional(), metric: z.enum(["agent_work_time", "pausable_update_time", "periodic_update_time", "reply_time", "requester_wait_time", "resolution_time", "group_ownership_time"]).optional(), ticket_id: z.number().int().optional(), time: z.string().optional(), type: z.enum(["activate", "pause", "fulfill", "apply_sla", "apply_group_sla", "breach", "update_status", "measure"]).optional()}); export const exportIncrementalTicketEventsResponse = z.object({count: z.number().int().optional(), end_of_stream: z.boolean().optional(), end_time: z.number().int().optional(), next_page: z.string().nullable().optional(), ticket_events: z.array(ticketMetricEventBaseObject).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 }; /** * Incremental Ticket Event Export * * GET /api/v2/incremental/ticket_events */ export const getApiApiV2IncrementalTicketEvents = async (options?: GetApiApiV2IncrementalTicketEventsOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/incremental/ticket_events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(exportIncrementalTicketEventsResponse, await res.json()) };