import {z} from 'zod' export type GetApiFilesArgs = {limit?: number | undefined, offset?: number | undefined, search?: string | undefined, source?: ('upload' | 'capture' | 'message') | undefined, surface?: 'phone' | undefined, session_id?: string | undefined, phone_id?: string | undefined, mime_type?: string | undefined, media_type?: ('image' | 'video') | undefined, min_size_bytes?: number | undefined, max_size_bytes?: number | undefined, created_after?: string | undefined, created_before?: string | undefined, order_by?: string | undefined}; export type GetApiFilesOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fileSummary = z.object({attachment_url: z.string().optional(), bytes_transferred: z.number().int().optional(), capture_error: z.string().optional(), capture_state: z.enum(["detected", "uploading", "ready", "skipped_size", "skipped_quota", "skipped_type", "dropped_teardown", "failed"]).optional(), checksum: z.string().optional(), created_at: z.string(), display_url: z.string().optional(), download_url: z.string().optional(), duration_seconds: z.number().int().optional(), filename: z.string(), height: z.number().int().optional(), id: z.string(), message_id: z.string().optional(), mime_type: z.string(), on_phone_count: z.number().int(), phone_id: z.string().optional(), preview_state: z.enum(["processing", "ready", "unavailable"]), reel_url: z.string().optional(), session_id: z.string().optional(), size_bytes: z.number().int(), source: z.enum(["upload", "capture", "message"]), status: z.enum(["uploading", "ready"]), surface: z.literal("phone").optional(), thumbnail_url: z.string().optional(), width: z.number().int().optional()}); export const fileUsage = z.object({byte_limit: z.number().int(), file_count: z.number().int(), file_limit: z.number().int(), total_bytes: z.number().int()}); export const fileListResponse = z.object({files: z.array(fileSummary).nullable(), total: z.number().int(), usage: fileUsage}); 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 the org's files * * GET /files */ export const getApiFiles = async (args: GetApiFilesArgs, options?: GetApiFilesOptions) => { 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 (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.search != null) query.append("search", String(args.search)) if (args.source != null) query.append("source", String(args.source)) if (args.surface != null) query.append("surface", String(args.surface)) if (args.session_id != null) query.append("session_id", String(args.session_id)) if (args.phone_id != null) query.append("phone_id", String(args.phone_id)) if (args.mime_type != null) query.append("mime_type", String(args.mime_type)) if (args.media_type != null) query.append("media_type", String(args.media_type)) if (args.min_size_bytes != null) query.append("min_size_bytes", String(args.min_size_bytes)) if (args.max_size_bytes != null) query.append("max_size_bytes", String(args.max_size_bytes)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/files${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(fileListResponse, await res.json()) };