import {z} from 'zod' export type FileStatus = 'created' | 'approved' | 'canceled' | 'sent' | 'rejected' | 'processed' | 'received'; export type FileCategory = 'bank_file' | 'payment_file' | 'report_file' | 'return_request_file' | 'return_file' | 'account_report' | 'account_statement' | 'account_notification' | 'inquiry_category' | 'denied_inquiry_file' | 'denied_return_request_file' | 'error_file_format' | 'unknown_file_format' | 'forwarded_message'; export type FileDirection = 'incoming' | 'outgoing'; export type FileFormat = 'pain.001' | 'pain.002' | 'pain.008' | 'pacs.002' | 'pacs.003' | 'pacs.004' | 'pacs.007' | 'pacs.008' | 'pacs.028' | 'camt.027' | 'camt.029' | 'camt.052' | 'camt.053' | 'camt.054' | 'camt.056' | 'camt.087' | 'mt.950' | 'head.002' | 'error.format' | 'unknown.format' | 'multiple.formats' | 'custom.format'; export type GetApiFilesArgs = {limit?: number | undefined, starting_after?: string | undefined, sort_order?: ('asc' | 'desc') | undefined, start_date?: string | undefined, end_date?: string | undefined, status?: FileStatus | undefined, category?: FileCategory | undefined, direction?: FileDirection | undefined, format?: FileFormat | undefined, connected_account_id?: string | undefined}; export type GetApiFilesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const fileConnectedAccountId = z.string(); export const fileConnectedAccountIds = z.array(z.string()); export const fileCategory = z.enum(["bank_file", "payment_file", "report_file", "return_request_file", "return_file", "account_report", "account_statement", "account_notification", "inquiry_category", "denied_inquiry_file", "denied_return_request_file", "error_file_format", "unknown_file_format", "forwarded_message"]); export const fileStatus = z.enum(["created", "approved", "canceled", "sent", "rejected", "processed", "received"]); export const fileDetails = z.object({id: z.string(), object: z.literal("file"), connected_account_id: fileConnectedAccountId, connected_account_ids: fileConnectedAccountIds, direction: z.enum(["outgoing", "incoming"]), category: fileCategory, format: z.string(), filename: z.string(), size: z.number().int().gte(0), summary: z.object({}), status: fileStatus, bank_data: z.object({message_ids: z.array(z.string())}), status_details: z.string(), created_at: z.string()}); export const getApiFilesResponse = z.object({records: z.array(fileDetails)}); 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 files * * GET /files */ export const getApiFiles = async (args: GetApiFilesArgs, options?: GetApiFilesOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = 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.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) 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.status != null) query.append("status", String(args.status)) if (args.category != null) query.append("category", String(args.category)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.format != null) query.append("format", String(args.format)) if (args.connected_account_id != null) query.append("connected_account_id", String(args.connected_account_id)) if (sec0 !== undefined) headers.set("x-api-key", sec0) 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(getApiFilesResponse, await res.json()) };