import {z} from 'zod' export type Date = string; export type GetApiTransferLogsArgs = {date_start: Date, date_end: Date}; export type GetApiTransferLogsOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.string(); export const transferLog = z.object({request_id: z.string().optional(), origin_account: z.string().optional(), destination_account: z.string().optional(), destination_name: z.string().optional(), destination_institution_name: z.string().optional(), currency: currency.optional(), amount: z.string().optional(), status: z.enum(["CREATED", "START_CONFIRM", "CONFIRMED", "ERROR", "REJECTED"]).optional(), concept: z.string().optional(), created_at: z.string().optional(), operation_id: z.string().optional()}); export const getApiTransferLogsResponse = z.object({status: z.string().optional(), result: z.array(transferLog).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 }; /** * Obtener listado de transferencias realizadas * * GET /transfer/logs */ export const getApiTransferLogs = async (args: GetApiTransferLogsArgs, options: GetApiTransferLogsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_start != null) query.append("date_start", String(args.date_start)) if (args.date_end != null) query.append("date_end", String(args.date_end)) if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/transfer/logs${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(getApiTransferLogsResponse, await res.json()) };