import {z} from 'zod' export type CreateApiCallQueuesArgs = {body: {name: string, extension_number: string, polling_strategy: 'RING_SIMULTANEOUSLY' | 'PRIORITIZED_HUNT' | 'CYCLIC_HUNT' | 'LEAST_WORKED_HUNT' | 'SKILL_BASED_ROUTING_PRIORITIZED_HUNT' | 'SKILL_BASED_ROUTING_CYCLIC_HUNT' | 'SKILL_BASED_ROUTING_LEAST_WORKED_HUNT', ring_time?: number | undefined, max_callers?: number | undefined, moh_prompt_file_id?: string | undefined, intro_type?: ('DISABLE' | 'NORMAL' | 'FULL') | undefined, intro_prompt_file_id?: string | undefined, max_waiting_time?: number | undefined, wait_when_no_agents_online?: boolean | undefined, inform_position?: ('DISABLE' | 'PERIODICALLY' | 'ON_CONNECTED') | undefined, inform_position_interval?: number | undefined, agent_auto_ready?: boolean | undefined, agent_auto_not_ready_after_non_acd_call?: boolean | undefined, sla_time?: number | undefined, language?: string | undefined, no_answer_forward_rule?: ({action?: ('FORWARD_TO_NUMBER' | 'FORWARD_TO_VOICEMAIL' | 'HANGUP') | undefined, number?: string | undefined}) | undefined, enable_callback?: boolean | undefined, callback_mode?: ('ACTIVE' | 'TIMEOUT') | undefined, callback_timeout?: number | undefined, callback_outbound_prefix?: string | undefined, outbound_caller_ids?: (Array<{provider_id?: string | undefined, caller_id?: string | undefined, description?: string | undefined}>) | undefined, enable_sticky_routing?: boolean | undefined, sticky_routing_duration?: number | undefined, enable_paid?: boolean | undefined, enable_prid?: boolean | undefined, extension_number_as_to_header?: boolean | undefined}}; export type CreateApiCallQueuesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCallQueuesResponse = z.object({id: z.string().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 a call queue * * POST /call_queues */ export const createApiCallQueues = async (args: CreateApiCallQueuesArgs, options?: CreateApiCallQueuesOptions) => { const {baseUrl = "https://HOSTNAME:8887/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/call_queues${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(createApiCallQueuesResponse, await res.json()) };