import {z} from 'zod' export type GetApiXmsV1ServicePlanIdInboundsArgs = {page?: number | undefined, page_size?: number | undefined, to?: Array | undefined, start_date?: string | undefined, end_date?: string | undefined, client_reference?: string | undefined}; export type GetApiXmsV1ServicePlanIdInboundsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const textMessage = z.object({client_reference: z.string().optional(), from: z.string(), id: z.string(), operator_id: z.string().optional(), received_at: z.string(), sent_at: z.string().optional(), to: z.string(), type: z.literal("mo_text"), body: z.string().max(2000)}); export const binaryMessage = z.object({client_reference: z.string().optional(), from: z.string(), id: z.string(), operator_id: z.string().optional(), received_at: z.string(), sent_at: z.string().optional(), to: z.string(), type: z.literal("mo_binary"), body: z.string(), udh: z.string()}); export const mmsMedia = z.object({code: z.number().int(), content_type: z.string(), status: z.enum(["Uploaded", "Failed"]), url: z.string().optional()}); export const mmsMoBody = z.object({media: z.array(mmsMedia).optional(), message: z.string().optional(), subject: z.string().optional()}); export const mediaMessage = z.object({client_reference: z.string().optional(), from: z.string(), id: z.string(), operator_id: z.string().optional(), received_at: z.string(), sent_at: z.string().optional(), to: z.string(), type: z.literal("mo_media"), body: mmsMoBody}); export const inbound = z.discriminatedUnion("type", [textMessage, binaryMessage, mediaMessage]); export const apiInboundList = z.object({count: z.number().int().optional(), page: z.number().int().optional(), inbounds: z.array(inbound).optional(), page_size: z.number().int().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 incoming messages * * GET /xms/v1/{service_plan_id}/inbounds */ export const getApiXmsV1ServicePlanIdInbounds = async (args: GetApiXmsV1ServicePlanIdInboundsArgs, options?: GetApiXmsV1ServicePlanIdInboundsOptions) => { const {baseUrl = "https://us.sms.api.sinch.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.to != null) query.append("to", args.to.join(',')) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.client_reference != null) query.append("client_reference", String(args.client_reference)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/xms/v1/${encodeURIComponent(String(args.service_plan_id))}/inbounds${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(apiInboundList, await res.json()) };