import {z} from 'zod' export type GetApiBalanceEntriesBalanceEntryIdArgs = {'Finix-Version'?: string | undefined}; export type GetApiBalanceEntriesBalanceEntryIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string().min(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const amount = z.number().int(); export const currency = z.enum(["CAD", "USD"]); export const tags = z.record(z.string(), z.unknown()).nullable(); export const balanceEntry = z.object({id: id.optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), amount: amount.optional(), created_by: z.string().optional(), currency: currency.optional(), description: z.string().nullable().optional(), entity_id: z.string().optional(), entity_type: z.enum(["BALANCE_ADJUSTMENT", "TRANSFER"]).optional(), estimated_posted_date: z.string().optional(), linked_to: z.string().optional(), linked_type: z.literal("APPLICATION").optional(), parent_balance_entry_id: z.string().nullable().optional(), posted_at: z.string().nullable().optional(), state: z.enum(["SUCCEEDED", "PENDING", "FAILED", "RETURNED"]).optional(), tags: tags.optional(), transaction_date: z.string().nullable().optional(), type: z.enum(["BALANCE_TOP_UP_ACH", "BALANCE_WITHDRAWAL_ACH", "MANUAL_BALANCE_ADJUSTMENT_DECREASE", "MANUAL_BALANCE_ADJUSTMENT_INCREASE", "PULL_FROM_ACH", "PULL_FROM_CARD", "PUSH_TO_ACH", "PUSH_TO_CARD", "PULL_FROM_SAME_DAY_ACH", "PUSH_TO_SAME_DAY_ACH"]).optional(), wire_details: z.object({external_reference_number: z.string().nullable().optional()}).optional(), _links: z.object({self: z.object({href: z.string().optional()}).optional()}).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 }; /** * Fetch a Balance Entry * * GET /balance_entries/{balance_entry_id} */ export const getApiBalanceEntriesBalanceEntryId = async (args: GetApiBalanceEntriesBalanceEntryIdArgs, options?: GetApiBalanceEntriesBalanceEntryIdOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/balance_entries/${encodeURIComponent(String(args.balance_entry_id))}${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(balanceEntry, await res.json()) };