import {z} from 'zod' export type PhoneNumberId = string; export type ReplacePhoneNumberRequest = {targetPhoneNumberId?: PhoneNumberId | undefined}; export type CreateApiRestapiV2AccountsAccountIdPhoneNumbersPhoneNumberIdReplaceArgs = {accountId: string, phoneNumberId: PhoneNumberId, body: ReplacePhoneNumberRequest}; export type CreateApiRestapiV2AccountsAccountIdPhoneNumbersPhoneNumberIdReplaceOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumberId = z.string().max(18); export const phoneNumberE164 = z.string().max(18); export const phoneNumberType = z.enum(["VoiceFax", "VoiceOnly", "FaxOnly"]).nullable(); export const phoneNumberTollType = z.enum(["Toll", "TollFree"]); export const phoneNumberUsageType = z.enum(["MainCompanyNumber", "DirectNumber", "Inventory", "InventoryPartnerBusinessMobileNumber", "PartnerBusinessMobileNumber", "AdditionalCompanyNumber", "CompanyNumber", "PhoneLine", "CompanyFaxNumber", "ForwardedNumber", "ForwardedCompanyNumber", "ContactCenterNumber", "ConferencingNumber", "MeetingsNumber", "BusinessMobileNumber", "ELIN"]); export const phoneNumberStatus = z.enum(["Normal", "Pending", "PortedIn", "Temporary", "Unknown"]); export const extensionNumber = z.string().min(1).max(8); export const accountPhoneNumberInfo = z.object({id: phoneNumberId, phoneNumber: phoneNumberE164, type: phoneNumberType.optional(), tollType: phoneNumberTollType, usageType: phoneNumberUsageType, byocNumber: z.boolean().optional(), status: phoneNumberStatus, extension: z.object({id: z.string(), extensionNumber: extensionNumber.optional()}).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 }; /** * Replace Phone Number * * POST /restapi/v2/accounts/{accountId}/phone-numbers/{phoneNumberId}/replace */ export const createApiRestapiV2AccountsAccountIdPhoneNumbersPhoneNumberIdReplace = async (args: CreateApiRestapiV2AccountsAccountIdPhoneNumbersPhoneNumberIdReplaceArgs, options?: CreateApiRestapiV2AccountsAccountIdPhoneNumbersPhoneNumberIdReplaceOptions) => { 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/v2/accounts/${encodeURIComponent(String(args.accountId))}/phone-numbers/${encodeURIComponent(String(args.phoneNumberId))}/replace${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(accountPhoneNumberInfo, await res.json()) };