import {z} from 'zod' export type CallerIdDeviceInfoRequest = {id?: string | undefined}; export type CallerIdPhoneInfoRequest = {id?: string | undefined}; export type CallerIdByDeviceInfoRequest = {type?: string | undefined, phoneInfo?: CallerIdPhoneInfoRequest | undefined}; export type CallerIdByDeviceRequest = {device?: CallerIdDeviceInfoRequest | undefined, callerId?: CallerIdByDeviceInfoRequest | undefined}; export type CallerIdByFeatureInfoRequest = {type?: string | undefined, phoneInfo?: CallerIdPhoneInfoRequest | undefined}; export type CallerIdByFeatureRequest = {feature?: ('RingOut' | 'RingMe' | 'CallFlip' | 'FaxNumber' | 'AdditionalSoftphone' | 'Alternate' | 'CommonPhone' | 'MobileApp' | 'Delegated') | undefined, callerId?: CallerIdByFeatureInfoRequest | undefined}; export type ExtensionCallerIdInfoRequest = {uri?: string | undefined, byDevice?: Array | undefined, byFeature?: Array | undefined, extensionNameForOutboundCalls?: boolean | undefined, extensionNumberForInternalCalls?: boolean | undefined}; export type UpdateApiRestapiV10AccountAccountIdExtensionExtensionIdCallerIdArgs = {accountId: string, extensionId: string, body: ExtensionCallerIdInfoRequest}; export type UpdateApiRestapiV10AccountAccountIdExtensionExtensionIdCallerIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const callerIdDeviceInfo = z.object({id: z.string().optional(), uri: z.string().optional(), name: z.string().optional()}); export const callerIdPhoneInfo = z.object({id: z.string().optional(), uri: z.string().optional(), phoneNumber: z.string().optional()}); export const callerIdByDeviceInfo = z.object({type: z.string().optional(), phoneInfo: callerIdPhoneInfo.optional()}); export const callerIdByDevice = z.object({device: callerIdDeviceInfo.optional(), callerId: callerIdByDeviceInfo.optional()}); export const callerIdByFeatureInfo = z.object({type: z.string().optional(), phoneInfo: callerIdPhoneInfo.optional()}); export const callerIdByFeature = z.object({feature: z.enum(["RingOut", "RingMe", "CallFlip", "FaxNumber", "AdditionalSoftphone", "Alternate", "CommonPhone", "MobileApp", "Delegated"]).optional(), callerId: callerIdByFeatureInfo.optional()}); export const extensionCallerIdInfo = z.object({uri: z.string().optional(), byDevice: z.array(callerIdByDevice).optional(), byFeature: z.array(callerIdByFeature).optional(), extensionNameForOutboundCalls: z.boolean().optional(), extensionNumberForInternalCalls: z.boolean().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 }; /** * Update Extension Caller ID * * PUT /restapi/v1.0/account/{accountId}/extension/{extensionId}/caller-id */ export const updateApiRestapiV10AccountAccountIdExtensionExtensionIdCallerId = async (args: UpdateApiRestapiV10AccountAccountIdExtensionExtensionIdCallerIdArgs, options?: UpdateApiRestapiV10AccountAccountIdExtensionExtensionIdCallerIdOptions) => { 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))}/extension/${encodeURIComponent(String(args.extensionId))}/caller-id${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(extensionCallerIdInfo, await res.json()) };