import {z} from 'zod' export type GetApiV1FinancialAccountsFinancialAccountTokenLoanTapesArgs = {begin?: string | undefined, end?: string | undefined, ending_before?: string | undefined, financial_account_token: string, page_size?: number | undefined, starting_after?: string | undefined}; export type GetApiV1FinancialAccountsFinancialAccountTokenLoanTapesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentDetails = z.object({}); export const debitDetails = z.object({}); export const creditDetails = z.object({}); export const statementTotals = z.object({payments: z.number().int(), payment_details: paymentDetails.nullable().optional(), purchases: z.number().int(), fees: z.number().int(), debits: z.number().int(), debit_details: debitDetails.nullable().optional(), credits: z.number().int(), credit_details: creditDetails.nullable().optional(), interest: z.number().int(), cash_advances: z.number().int(), balance_transfers: z.number().int()}); export const categoryBalances = z.object({interest: z.number().int(), principal: z.number().int(), fees: z.number().int()}); export const balances = z.object({past_due: categoryBalances, due: categoryBalances, past_statements_due: categoryBalances, next_statement_due: categoryBalances, installments: categoryBalances.nullable().optional()}); export const periodState = z.enum(["STANDARD", "PROMO", "PENALTY"]); export const financialAccountStatus = z.enum(["OPEN", "CLOSED", "SUSPENDED", "PENDING"]); export const financialAccountSubstatus = z.enum(["CHARGED_OFF_DELINQUENT", "CHARGED_OFF_FRAUD", "END_USER_REQUEST", "BANK_REQUEST", "DELINQUENT", "INTEREST_AND_FEES_PAUSED"]); export const financialAccountState = z.object({status: financialAccountStatus, substatus: financialAccountSubstatus.nullable().optional()}); export const accountStanding = z.object({period_state: periodState, period_number: z.number().int(), consecutive_minimum_payments_made: z.number().int(), consecutive_minimum_payments_missed: z.number().int(), consecutive_full_payments_made: z.number().int(), days_past_due: z.number().int(), has_grace: z.boolean(), financial_account_state: financialAccountState}); export const categoryDetails = z.object({purchases: z.string(), cash_advances: z.string(), balance_transfers: z.string()}); export const paymentAllocation = z.object({interest: z.number().int(), principal: z.number().int(), fees: z.number().int(), interest_details: categoryDetails.nullable(), principal_details: categoryDetails.nullable(), fee_details: categoryDetails.nullable()}); export const balanceDetails = z.object({amount: z.number().int(), remaining: z.number().int()}); export const interestCalculationMethod = z.enum(["DAILY", "AVERAGE_DAILY"]); export const interestDetails = z.object({prime_rate: z.string().nullable(), interest_calculation_method: interestCalculationMethod, effective_apr: categoryDetails, interest_for_period: categoryDetails, daily_balance_amounts: categoryDetails, minimum_interest_charged: z.number().int().nullable().optional(), actual_interest_charged: z.number().int().nullable()}); export const loanTapeResponse = z.object({token: z.string(), financial_account_token: z.string(), date: z.string(), day_of_period: z.number().int().nullable().optional(), created: z.string(), updated: z.string(), version: z.number().int(), ytd_totals: statementTotals, period_totals: statementTotals, day_totals: statementTotals, balances: balances, starting_balance: z.number().int(), ending_balance: z.number().int(), credit_limit: z.number().int(), available_credit: z.number().int(), excess_credits: z.number().int(), account_standing: accountStanding, credit_product_token: z.string(), tier: z.string().nullable().optional(), payment_allocation: paymentAllocation, payment_only_allocation: paymentAllocation.nullable(), minimum_payment_balance: balanceDetails, previous_statement_balance: balanceDetails, interest_details: interestDetails.nullable()}); export const loanTapesResponse = z.object({data: z.array(loanTapeResponse), has_more: z.boolean()}); 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 loan tapes * * GET /v1/financial_accounts/{financial_account_token}/loan_tapes */ export const getApiV1FinancialAccountsFinancialAccountTokenLoanTapes = async (args: GetApiV1FinancialAccountsFinancialAccountTokenLoanTapesArgs, options?: GetApiV1FinancialAccountsFinancialAccountTokenLoanTapesOptions) => { const {baseUrl = "https://sandbox.lithic.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.begin != null) query.append("begin", String(args.begin)) if (args.end != null) query.append("end", String(args.end)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/financial_accounts/${encodeURIComponent(String(args.financial_account_token))}/loan_tapes${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(loanTapesResponse, await res.json()) };