import {z} from 'zod' export type GetApiRestapiV10AccountAccountIdA2pSmsOptOutsArgs = {accountId: string, from?: string | undefined, to?: string | undefined, status?: ('optout' | 'optin' | 'all') | undefined, pageToken?: string | undefined, perPage?: number | undefined}; export type GetApiRestapiV10AccountAccountIdA2pSmsOptOutsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const optOutResponse = z.object({from: z.string().optional(), to: z.string().optional(), status: z.enum(["OptIn", "OptOut"]).optional(), source: z.enum(["Recipient", "Account", "Upstream", "Carrier"]).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 optOutListResponse = z.object({records: z.array(optOutResponse).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 Opted Out Numbers * * GET /restapi/v1.0/account/{accountId}/a2p-sms/opt-outs */ export const getApiRestapiV10AccountAccountIdA2pSmsOptOuts = async (args: GetApiRestapiV10AccountAccountIdA2pSmsOptOutsArgs, options?: GetApiRestapiV10AccountAccountIdA2pSmsOptOutsOptions) => { 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.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.status != null) query.append("status", String(args.status)) 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/opt-outs${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(optOutListResponse, await res.json()) };