import {z} from 'zod' export type CallManagerParticipantAddress = {type: 'extension', target: string}; export type CreateApiServiceAgentsCallsIdParticipantsArgs = {id: string, body: {address: CallManagerParticipantAddress}}; export type CreateApiServiceAgentsCallsIdParticipantsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const callManagerGroupcallStatus = z.enum(["progressing", "hangingup", "hangup"]); export const commonAddress = z.object({type: z.enum(["", "agent", "conference", "email", "extension", "line", "sip", "tel", "web_session"]).optional(), target: z.string().optional(), target_name: z.string().optional(), name: z.string().optional(), detail: z.string().optional()}); export const callManagerGroupcallRingMethod = z.enum(["", "ring_all", "linear"]); export const callManagerGroupcallAnswerMethod = z.enum(["", "hangup_others"]); export const callManagerGroupcall = z.object({id: z.string().optional(), customer_id: z.string().optional(), owner_type: z.string().optional(), owner_id: z.string().optional(), status: callManagerGroupcallStatus.optional(), flow_id: z.string().optional(), source: commonAddress.optional(), destinations: z.array(commonAddress).optional(), master_call_id: z.string().nullable().optional(), master_groupcall_id: z.string().nullable().optional(), ring_method: callManagerGroupcallRingMethod.optional(), answer_method: callManagerGroupcallAnswerMethod.optional(), answer_call_id: z.string().optional(), call_ids: z.array(z.string()).optional(), answer_groupcall_id: z.string().optional(), groupcall_ids: z.array(z.string()).optional(), call_count: z.number().int().optional(), groupcall_count: z.number().int().optional(), dial_index: z.number().int().nullable().optional(), tm_create: z.string().optional(), tm_update: z.string().optional(), tm_delete: z.string().nullable().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 }; /** * Add a participant to a call * * POST /service_agents/calls/{id}/participants */ export const createApiServiceAgentsCallsIdParticipants = async (args: CreateApiServiceAgentsCallsIdParticipantsArgs, options?: CreateApiServiceAgentsCallsIdParticipantsOptions) => { const {baseUrl = "https://api.voipbin.net/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/service_agents/calls/${encodeURIComponent(String(args.id))}/participants${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(callManagerGroupcall, await res.json()) };