import {z} from 'zod' export type SmsDirectionEnum = 'Inbound' | 'Outbound'; export type GetApiRestapiV10AccountAccountIdA2pSmsMessagesArgs = {accountId: string, batchId?: string | undefined, direction?: SmsDirectionEnum | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, view?: ('Simple' | 'Detailed') | undefined, phoneNumber?: Array | undefined, pageToken?: string | undefined, perPage?: number | undefined}; export type GetApiRestapiV10AccountAccountIdA2pSmsMessagesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const smsStatusEnum = z.enum(["Queued", "Delivered", "Sent", "SendingFailed", "DeliveryFailed"]); export const smsDirectionEnum = z.enum(["Inbound", "Outbound"]); export const messageListMessageResponse = z.object({id: z.number().int().optional(), batchId: z.string().optional(), from: z.string().optional(), to: z.array(z.string()).optional(), creationTime: z.string().optional(), lastModifiedTime: z.string().optional(), messageStatus: smsStatusEnum.optional(), segmentCount: z.number().int().optional(), text: z.string().optional(), cost: z.number().optional(), direction: smsDirectionEnum.optional(), errorCode: z.string().optional()}); export const pagingResource = z.object({pageToken: z.string().optional(), perPage: z.number().int().optional(), firstPageToken: z.string().optional(), previousPageToken: z.string().optional(), nextPageToken: z.string().optional()}); export const messageListResponse = z.object({records: z.array(messageListMessageResponse).optional(), paging: pagingResource.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 Messages * * GET /restapi/v1.0/account/{accountId}/a2p-sms/messages */ export const getApiRestapiV10AccountAccountIdA2pSmsMessages = async (args: GetApiRestapiV10AccountAccountIdA2pSmsMessagesArgs, options?: GetApiRestapiV10AccountAccountIdA2pSmsMessagesOptions) => { 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)) if (args.view != null) query.append("view", String(args.view)) for (const item of args.phoneNumber ?? []) query.append("phoneNumber", String(item)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/v1.0/account/${encodeURIComponent(String(args.accountId))}/a2p-sms/messages${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(messageListResponse, await res.json()) };