import {z} from 'zod' export type BulkOperationExtensionReference = {id: string}; export type PostalAddress = {street: string, street2?: string | undefined, city: string, state?: string | undefined, zip: string, country: string}; export type PhoneNumberTollType = 'Toll' | 'TollFree'; export type PhoneNumberDefinitionTollType = {tollType: PhoneNumberTollType}; export type PhoneNumberDefinitionPreferredAreaCode = {preferredAreaCode: string}; export type PhoneNumberDefinitionDirectNumberWithReservationId = {phoneNumber: string, reservationId?: string | undefined}; export type PhoneNumberDefinitionNumberId = {phoneNumberId: string}; export type BulkAddDevicesItem = {costCenterId?: string | undefined, extension: BulkOperationExtensionReference, type: 'OtherPhone' | 'WebRTC', emergency: {address: PostalAddress} | {location: {id: string}}, phoneInfo: PhoneNumberDefinitionTollType | PhoneNumberDefinitionPreferredAreaCode | PhoneNumberDefinitionDirectNumberWithReservationId | PhoneNumberDefinitionNumberId}; export type BulkAddDevicesRequest = {records: Array}; export type CreateApiRestapiV2AccountsAccountIdDevicesBulkAddArgs = {accountId: string, body: BulkAddDevicesRequest}; export type CreateApiRestapiV2AccountsAccountIdDevicesBulkAddOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkOperationExtensionReference = z.object({id: z.string()}); export const phoneNumberE164 = z.string().max(18); export const bulkAddDevicesSuccessItem = z.object({successful: z.boolean(), id: z.string(), extension: bulkOperationExtensionReference, phoneNumber: phoneNumberE164}); export const apiErrorWithParameter = z.object({errorCode: z.string(), message: z.string(), parameterName: z.string().optional(), parameterValue: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const bulkAddDevicesErrorItem = z.object({successful: z.boolean(), errors: z.array(apiErrorWithParameter)}); export const bulkAddDevicesResponse = z.object({results: z.array(z.union([bulkAddDevicesSuccessItem, bulkAddDevicesErrorItem]))}); 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 }; /** * Add BYOD Devices * * POST /restapi/v2/accounts/{accountId}/devices/bulk-add */ export const createApiRestapiV2AccountsAccountIdDevicesBulkAdd = async (args: CreateApiRestapiV2AccountsAccountIdDevicesBulkAddArgs, options?: CreateApiRestapiV2AccountsAccountIdDevicesBulkAddOptions) => { 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))}/devices/bulk-add${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(bulkAddDevicesResponse, await res.json()) };