import {z} from 'zod' export type GetApiMessagesMessageIdArgs = {message_id: string}; export type GetApiMessagesMessageIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneMessagePart = z.object({file_id: z.string().optional(), mime_type: z.string().optional(), name: z.string().optional(), size_bytes: z.number().int().optional(), text: z.string().optional(), type: z.enum(["text", "media"])}); export const phoneMessagePhoneDeletion = z.object({next_check_at: z.string().nullable(), reason: z.string(), status: z.enum(["pending", "deleted", "not_found", "ambiguous", "not_default_app", "not_matched", "failed", "unconfirmed", "not_sent"])}); export const phoneMessage = z.object({accepted_at: z.string().optional(), api_key_id: z.string().optional(), body: z.string(), channel: z.enum(["sms", "mms", "rcs"]), conversation_id: z.string(), created_at: z.string(), deleted_at: z.string().optional(), delivered_at: z.string().optional(), direction: z.enum(["inbound", "outbound"]), error: z.string().optional(), error_code: z.enum(["phone_unreachable", "no_result", "policy_off", "expired", "no_subscription", "no_service", "radio_off", "carrier_rejected", "phone_rate_limited", "media_fetch_failed", "connection_lost", "queue_full", "send_failed"]).optional(), failed_at: z.string().optional(), from: z.string().nullable(), id: z.string(), is_group: z.boolean(), parts: z.array(phoneMessagePart).nullable(), phone_deletion: phoneMessagePhoneDeletion.optional(), phone_id: z.string(), received_at: z.string().nullable(), sent_at: z.string().nullable(), status: z.enum(["queued", "sent", "delivered", "failed", "unconfirmed"]).optional(), to: z.array(z.string()).nullable(), truncated: z.boolean(), user_id: z.string().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 }; /** * Get a message * * GET /messages/{message_id} */ export const getApiMessagesMessageId = async (args: GetApiMessagesMessageIdArgs, options?: GetApiMessagesMessageIdOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/messages/${encodeURIComponent(String(args.message_id))}${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(phoneMessage, await res.json()) };