import {z} from 'zod' export type ChannelType = 'sms' | 'inbound_call' | 'outbound_call'; export type ChannelCreate = {channel_type: ChannelType, agent_id?: (string | null) | undefined, agent_revision_id?: (string | null) | undefined, team_id?: (string | null) | undefined, team_revision_id?: (string | null) | undefined}; export type CreateApiV1CommunicationPhoneNumbersPhoneNumberIdChannelsArgs = {phone_number_id: string, body: ChannelCreate}; export type CreateApiV1CommunicationPhoneNumbersPhoneNumberIdChannelsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const channelType = z.enum(["sms", "inbound_call", "outbound_call"]); export const channelResponse = z.object({id: z.string(), phone_number_id: z.string(), channel_type: channelType, agent_id: z.string().nullable().optional(), agent_revision_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), team_revision_id: z.string().nullable().optional(), created_at: z.string(), revision_state: z.enum(["pinned_current", "pinned_stale", "legacy_unpinned"]).nullable().optional(), pinned_revision_name: z.string().nullable().optional(), active_revision_id: 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 }; /** * Bind an agent or team to a channel on a phone number * * POST /v1/communication/phone-numbers/{phone_number_id}/channels */ export const createApiV1CommunicationPhoneNumbersPhoneNumberIdChannels = async (args: CreateApiV1CommunicationPhoneNumbersPhoneNumberIdChannelsArgs, options: CreateApiV1CommunicationPhoneNumbersPhoneNumberIdChannelsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/communication/phone-numbers/${encodeURIComponent(String(args.phone_number_id))}/channels${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(channelResponse, await res.json()) };