import {z} from 'zod' export type MakeCallOutCallerInfoRequestFrom = {deviceId?: string | undefined}; export type MakeCallOutCallerInfoRequestTo = {phoneNumber?: string | undefined, extensionNumber?: string | undefined}; export type CountryId = number; export type MakeCallOutRequest = {from: MakeCallOutCallerInfoRequestFrom, to: MakeCallOutCallerInfoRequestTo, countryId?: CountryId | undefined}; export type CreateApiRestapiV10AccountAccountIdTelephonyCallOutArgs = {accountId: string, body: MakeCallOutRequest}; export type CreateApiRestapiV10AccountAccountIdTelephonyCallOutOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const originInfo = z.object({type: z.enum(["Call", "RingOut", "RingMe", "Conference", "GreetingsRecording", "VerificationCall", "Zoom", "CallOut"]).optional()}); export const peerInfo = z.object({sessionId: z.string().optional(), telephonySessionId: z.string().optional(), partyId: z.string().optional()}); export const callStatusInfo = z.object({code: z.enum(["Setup", "Proceeding", "Answered", "Disconnected", "Gone", "Parked", "Hold", "VoiceMail", "FaxReceive", "Tds", "VoiceMailScreening"]).optional(), peerId: peerInfo.optional(), reason: z.enum(["Pickup", "Supervising", "TakeOver", "Timeout", "BlindTransfer", "RccTransfer", "AttendedTransfer", "CallerInputRedirect", "CallFlip", "ParkLocation", "DtmfTransfer", "AgentAnswered", "AgentDropped", "Rejected", "Cancelled", "InternalError", "NoAnswer", "TargetBusy", "InvalidNumber", "InternationalDisabled", "DestinationBlocked", "NotEnoughFunds", "NoSuchUser", "CallPark", "CallRedirected", "CallReplied", "CallSwitch", "CallFinished", "CallDropped"]).optional(), description: z.string().optional()}); export const parkInfo = z.object({id: z.string().optional()}); export const partyInfo = z.object({phoneNumber: z.string().optional(), name: z.string().optional(), deviceId: z.string().optional(), extensionId: z.string().optional()}); export const ownerInfo = z.object({accountId: z.string().optional(), extensionId: z.string().optional()}); export const recordingInfo = z.object({id: z.string().optional(), active: z.boolean().optional()}); export const callParty = z.object({id: z.string().optional(), status: callStatusInfo.optional(), muted: z.boolean().optional(), standAlone: z.boolean().optional(), park: parkInfo.optional(), from: partyInfo.optional(), to: partyInfo.optional(), owner: ownerInfo.optional(), direction: z.enum(["Inbound", "Outbound"]).optional(), conferenceRole: z.enum(["Host", "Participant"]).optional(), ringOutRole: z.enum(["Initiator", "Target"]).optional(), ringMeRole: z.enum(["Initiator", "Target"]).optional(), recordings: z.array(recordingInfo).optional()}); export const callSessionObject = z.object({id: z.string().optional(), origin: originInfo.optional(), voiceCallToken: z.string().optional(), parties: z.array(callParty).optional(), creationTime: z.string().optional()}); export const callSession = z.object({session: callSessionObject.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 }; /** * Make CallOut * * POST /restapi/v1.0/account/{accountId}/telephony/call-out */ export const createApiRestapiV10AccountAccountIdTelephonyCallOut = async (args: CreateApiRestapiV10AccountAccountIdTelephonyCallOutArgs, options?: CreateApiRestapiV10AccountAccountIdTelephonyCallOutOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/v1.0/account/${encodeURIComponent(String(args.accountId))}/telephony/call-out${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(callSession, await res.json()) };