import {z} from 'zod' export type TwilioRegionId = 'us1' | 'ie1' | 'au1'; export type TwilioEdgeLocation = 'ashburn' | 'dublin' | 'frankfurt' | 'sao-paulo' | 'singapore' | 'sydney' | 'tokyo' | 'umatilla' | 'roaming'; export type RegionConfigRequest = {region_id: TwilioRegionId, token: string, edge_location: TwilioEdgeLocation}; export type CreateTwilioPhoneNumberRequest = {phone_number: string, label: string, supports_inbound?: boolean | undefined, supports_outbound?: boolean | undefined, provider?: 'twilio' | undefined, agent_id?: (string | null) | undefined, sid: string, token: string, account_auth_token?: (string | null) | undefined, region_config?: (RegionConfigRequest | null) | undefined, enable_sms?: boolean | undefined}; export type ExotelApiSubdomain = 'api.in.exotel.com' | 'api.exotel.com'; export type CreateExotelPhoneNumberRequest = {phone_number: string, label: string, supports_inbound?: boolean | undefined, supports_outbound?: boolean | undefined, provider?: 'exotel' | undefined, agent_id?: (string | null) | undefined, account_sid: string, api_key: string, api_token: string, api_subdomain: ExotelApiSubdomain, app_id: string, applet_url?: (string | null) | undefined}; export type SIPMediaEncryptionEnum = 'disabled' | 'allowed' | 'required'; export type SIPTrunkCredentialsRequestModel = {username: string, password?: (string | null) | undefined}; export type InboundSIPTrunkConfigRequestModel = {allowed_addresses?: (Array | null) | undefined, allowed_numbers?: (Array | null) | undefined, media_encryption?: SIPMediaEncryptionEnum | undefined, credentials?: (SIPTrunkCredentialsRequestModel | null) | undefined, remote_domains?: (Array | null) | undefined, attributes_to_headers?: {[key: string]: string} | undefined}; export type SIPTrunkTransportEnum = 'auto' | 'udp' | 'tcp' | 'tls'; export type MediaCodec = 'G722/8000' | 'PCMU/8000' | 'PCMA/8000'; export type OutboundSIPTrunkConfigRequestModel = {address: string, transport?: SIPTrunkTransportEnum | undefined, media_encryption?: SIPMediaEncryptionEnum | undefined, headers?: {[key: string]: string} | undefined, attributes_to_headers?: {[key: string]: string} | undefined, credentials?: (SIPTrunkCredentialsRequestModel | null) | undefined, enabled_codecs?: Array | undefined}; export type CreateSIPTrunkPhoneNumberRequestV2 = {phone_number: string, label: string, supports_inbound?: boolean | undefined, supports_outbound?: boolean | undefined, provider?: 'sip_trunk' | undefined, agent_id?: (string | null) | undefined, inbound_trunk_config?: (InboundSIPTrunkConfigRequestModel | null) | undefined, outbound_trunk_config?: (OutboundSIPTrunkConfigRequestModel | null) | undefined}; export type CreateApiV1ConvaiPhoneNumbersArgs = {'xi-api-key'?: (string | null) | undefined, body: CreateTwilioPhoneNumberRequest | CreateExotelPhoneNumberRequest | CreateSIPTrunkPhoneNumberRequestV2}; export type CreateApiV1ConvaiPhoneNumbersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createPhoneNumberResponseModel = z.object({phone_number_id: z.string()}); 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 }; /** * Import Phone Number * * POST /v1/convai/phone-numbers */ export const createApiV1ConvaiPhoneNumbers = async (args: CreateApiV1ConvaiPhoneNumbersArgs, options: CreateApiV1ConvaiPhoneNumbersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['xi-api-key'] != null) headers.set("xi-api-key", String(args['xi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/convai/phone-numbers${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(createPhoneNumberResponseModel, await res.json()) };