import {z} from 'zod' export type GetApiRestapiV2AccountsAccountIdArgs = {accountId: string}; export type GetApiRestapiV2AccountsAccountIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumberE164 = z.string().max(18); export const accountStatusInfo = z.object({reason: z.enum(["SuspendedVoluntarily", "SuspendedInvoluntarily", "CancelledVoluntarily", "CancelledInvoluntarily"]).optional(), comment: z.string().min(1).max(256).optional(), till: z.string().optional()}); export const postalAddress = z.object({street: z.string().max(256), street2: z.string().max(256).optional(), city: z.string().max(64), state: z.string().max(64).optional(), zip: z.string().max(10), country: z.string().min(2).max(2)}); export const serviceInfoPackage = z.object({id: z.string(), version: z.literal("1")}); export const serviceInfoBrand = z.object({id: z.string(), name: z.string()}); export const serviceInfoCountryShortModel = z.object({id: z.string().optional(), uri: z.string().optional(), name: z.string().optional(), isoCode: z.string().optional(), callingCode: z.string().optional()}); export const serviceInfoUBrand = z.object({id: z.string().optional(), name: z.string().optional()}); export const serviceInfoPlan = z.object({id: z.string().optional(), name: z.string().optional(), edition: z.string().optional(), freemiumProductType: z.enum(["Freyja", "Phoenix"]).optional()}); export const serviceInfoV2 = z.object({package: serviceInfoPackage, brand: serviceInfoBrand.optional(), contractedCountry: serviceInfoCountryShortModel.optional(), uBrand: serviceInfoUBrand.optional(), servicePlan: serviceInfoPlan.optional()}); export const extensionNumber = z.string().min(1).max(8); export const systemUserContactInfo = z.object({firstName: z.string().max(64), lastName: z.string().max(64), email: z.string().max(64), contactPhone: phoneNumberE164, id: z.string(), extensionNumber: extensionNumber}); export const accountInfo = z.object({id: z.string(), externalAccountId: z.string().max(30).optional(), mainNumber: phoneNumberE164.optional(), status: z.enum(["Initial", "Unconfirmed", "Confirmed", "Disabled"]), statusInfo: accountStatusInfo.optional(), companyName: z.string().max(64).optional(), companyAddress: postalAddress.optional(), serviceInfo: serviceInfoV2, contactInfo: systemUserContactInfo.optional(), opportunityId: z.string().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 }; /** * Get Account Info * * GET /restapi/v2/accounts/{accountId} */ export const getApiRestapiV2AccountsAccountId = async (args: GetApiRestapiV2AccountsAccountIdArgs, options?: GetApiRestapiV2AccountsAccountIdOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/v2/accounts/${encodeURIComponent(String(args.accountId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(accountInfo, await res.json()) };