import {z} from 'zod' export type GetApiV1VoiceOutboundQueuedArgs = {agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, paused?: (boolean | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1VoiceOutboundQueuedOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const queuedCall = z.object({conversation_id: z.string(), to_number: z.string().nullable().optional(), agent_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), paused: z.boolean(), created_at: z.string(), eligible_at: z.string().nullable().optional(), queue_expires_at: z.string().nullable().optional()}); export const cursorPageQueuedCall = z.object({items: z.array(queuedCall), next_cursor: z.string().nullable().optional(), 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 queued outbound calls (optionally filtered by agent/team/paused) * * GET /v1/voice/outbound/queued */ export const getApiV1VoiceOutboundQueued = async (args: GetApiV1VoiceOutboundQueuedArgs, options: GetApiV1VoiceOutboundQueuedOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.team_id != null) query.append("team_id", String(args.team_id)) if (args.paused != null) query.append("paused", String(args.paused)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/voice/outbound/queued${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(cursorPageQueuedCall, await res.json()) };