import {z} from 'zod' export type TerminalDisposition = 'campaign_ended' | 'removed' | 'do_not_call' | 'completed' | 'cancelled' | 'suppressed' | 'attempt_ceiling_reached' | 'callback_ceiling_reached' | 'redirect_ceiling_reached' | 'permission_not_granted' | 'permission_declined' | 'permission_request_blocked' | 'permission_expired' | 'callback_time_invalid' | 'redirect_number_invalid' | 'couldnt_process'; export type GetApiV1ConvaiOutboundCampaignsCampaignsCampaignIdLeadsArgs = {campaign_id: string, page_size?: number | undefined, search?: (string | null) | undefined, status?: ('in_progress' | 'finished' | null) | undefined, disposition?: (Array | null) | undefined, cursor?: (string | null) | undefined, 'xi-api-key'?: (string | null) | undefined}; export type GetApiV1ConvaiOutboundCampaignsCampaignsCampaignIdLeadsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const terminalDisposition = z.enum(["campaign_ended", "removed", "do_not_call", "completed", "cancelled", "suppressed", "attempt_ceiling_reached", "callback_ceiling_reached", "redirect_ceiling_reached", "permission_not_granted", "permission_declined", "permission_request_blocked", "permission_expired", "callback_time_invalid", "redirect_number_invalid", "couldnt_process"]); export const flattenedConfigOverrides = z.object({language: z.string().nullable().optional(), first_message: z.string().nullable().optional(), prompt: z.string().nullable().optional(), voice_id: z.string().nullable().optional()}); export const batchCallRecipientStatus = z.enum(["pending", "dispatched", "initiated", "in_progress", "completed", "failed", "cancelled", "voicemail"]); export const leadDeliveryView = z.object({status: batchCallRecipientStatus, stage: z.enum(["queued", "sent", "delivered", "read", "replied", "failed"]), sent_at_unix: z.number().int().nullable(), delivered_at_unix: z.number().int().nullable(), read_at_unix: z.number().int().nullable(), replied_at_unix: z.number().int().nullable(), failed_at_unix: z.number().int().nullable(), error_message: z.string().nullable()}); export const leadView = z.object({lead_id: z.string(), recipient_id: z.string().nullable(), phone_number: z.string(), phone_numbers: z.array(z.string()).optional(), phone_numbers_index: z.number().int(), user_id: z.string(), attempt_count: z.number().int(), failure_count: z.number().int(), callback_count: z.number().int(), redirect_count: z.number().int(), do_not_call: z.boolean(), terminal_disposition: terminalDisposition.nullable(), created_at_unix: z.number().int().nullable(), updated_at_unix: z.number().int(), dynamic_variables: z.record(z.string(), z.unknown()), overrides: flattenedConfigOverrides, delivery: leadDeliveryView.nullable().optional()}); export const campaignLeadsResponse = z.object({leads: z.array(leadView), next_cursor: z.string().nullable(), has_more: z.boolean()}); 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 }; /** * List Outbound Campaign Leads * * GET /v1/convai/outbound-campaigns/campaigns/{campaign_id}/leads */ export const getApiV1ConvaiOutboundCampaignsCampaignsCampaignIdLeads = async (args: GetApiV1ConvaiOutboundCampaignsCampaignsCampaignIdLeadsArgs, options: GetApiV1ConvaiOutboundCampaignsCampaignsCampaignIdLeadsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.search != null) query.append("search", String(args.search)) if (args.status != null) query.append("status", String(args.status)) for (const item of args.disposition ?? []) query.append("disposition", String(item)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args['xi-api-key'] != null) headers.set("xi-api-key", String(args['xi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/convai/outbound-campaigns/campaigns/${encodeURIComponent(String(args.campaign_id))}/leads${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(campaignLeadsResponse, await res.json()) };