import {z} from 'zod' export type GetApiAttemptsArgs = {connection_id: string}; export type GetApiAttemptsOptions = {baseUrl?: string | undefined, appId?: string | undefined, secret?: string | undefined, fetch?: typeof fetch | undefined}; export const stage = z.object({created_at: z.string(), id: z.string(), interactive_fields_names: z.array(z.string()).nullable().optional(), interactive_html: z.string().nullable().optional(), name: z.enum(["start", "connect", "interactive", "fetch_holder_info", "fetch_accounts", "fetch_recent", "fetch_full", "disconnect", "finish"]), updated_at: z.string()}); export const simplifiedAttempt = z.object({api_mode: z.enum(["app", "service"]).optional(), api_version: z.string().optional(), automatic_fetch: z.boolean().optional(), daily_refresh: z.boolean().optional(), categorize: z.boolean().optional(), created_at: z.string().optional(), custom_fields: z.object({}).optional(), device_type: z.enum(["desktop", "tablet", "mobile"]).optional(), remote_ip: z.string().optional(), exclude_accounts: z.array(z.string()).optional(), user_present: z.boolean().optional(), customer_last_logged_at: z.string().optional(), fail_at: z.string().optional(), fail_error_class: z.string().optional(), fail_message: z.string().optional(), fetch_scopes: z.array(z.enum(["accounts", "holder_info", "transactions"])).optional(), finished: z.boolean().optional(), finished_recent: z.boolean().optional(), from_date: z.string().optional(), id: z.string().optional(), interactive: z.boolean().optional(), locale: z.string().optional(), partial: z.boolean().optional(), store_credentials: z.boolean().optional(), success_at: z.string().optional(), to_date: z.string().optional(), updated_at: z.string().optional(), show_consent_confirmation: z.boolean().optional(), include_natures: z.array(z.string()).optional(), last_stage: stage.optional()}); export const attemptsResponse = z.object({data: z.array(simplifiedAttempt).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 of attempts * * GET /attempts */ export const getApiAttempts = async (args: GetApiAttemptsArgs, options?: GetApiAttemptsOptions) => { const {baseUrl = "https://www.saltedge.com/api/v5", fetch: fetchFn = fetch, appId, secret} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.connection_id != null) query.append("connection_id", String(args.connection_id)) if (appId !== undefined) headers.set("App-id", appId) if (secret !== undefined) headers.set("Secret", secret) const url = `${baseUrl.replace(/\/+$/, '')}/attempts${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(attemptsResponse, await res.json()) };