import {z} from 'zod' export type SmsDirectionEnum = 'Inbound' | 'Outbound'; export type GetApiRestapiV10AccountAccountIdA2pSmsStatusesArgs = {accountId: string, batchId?: string | undefined, direction?: SmsDirectionEnum | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, phoneNumber?: Array | undefined}; export type GetApiRestapiV10AccountAccountIdA2pSmsStatusesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const messageStatusCounts = z.object({cost: z.number().optional(), count: z.number().int().optional(), errorCodeCounts: z.record(z.string(), z.number().int()).optional()}); export const messageStatusesResponse = z.object({queued: messageStatusCounts.optional(), delivered: messageStatusCounts.optional(), deliveryFailed: messageStatusCounts.optional(), sent: messageStatusCounts.optional(), sendingFailed: messageStatusCounts.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 }; /** * List A2P SMS Statuses * * GET /restapi/v1.0/account/{accountId}/a2p-sms/statuses */ export const getApiRestapiV10AccountAccountIdA2pSmsStatuses = async (args: GetApiRestapiV10AccountAccountIdA2pSmsStatusesArgs, options?: GetApiRestapiV10AccountAccountIdA2pSmsStatusesOptions) => { 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 (args.batchId != null) query.append("batchId", String(args.batchId)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) for (const item of args.phoneNumber ?? []) query.append("phoneNumber", String(item)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/v1.0/account/${encodeURIComponent(String(args.accountId))}/a2p-sms/statuses${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(messageStatusesResponse, await res.json()) };