import {z} from 'zod' export type GetApiAccountsAccountIdStatementsArgs = {starting_after?: string | undefined, ending_before?: string | undefined, limit?: string | undefined}; export type GetApiAccountsAccountIdStatementsOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const accountDeposit = z.object({}); export const accountCredit = z.object({credit_limit: z.number().int().optional(), security_deposit_account_id: z.string().nullable().optional()}); export const statement = z.union([z.object({statement_id: z.string(), account_id: z.string().optional(), statement_month: z.string(), type: z.enum(["credit", "deposit"]), statement_start_date: z.string(), statement_end_date: z.string(), fees: z.number().int(), transactions: z.array(z.object({transaction_date: z.string(), settled_date: z.string(), amount: z.number().int(), transaction_description: z.string(), transaction_type: z.enum(["Fee", "Refund", "Payment", "Purchase", "Interest"])})), deposit: accountDeposit.optional()}), z.object({statement_id: z.string(), account_id: z.string().optional(), statement_month: z.string(), type: z.enum(["credit", "deposit"]), statement_start_date: z.string(), statement_end_date: z.string(), fees: z.number().int(), transactions: z.array(z.object({transaction_date: z.string(), settled_date: z.string(), amount: z.number().int(), transaction_description: z.string(), transaction_type: z.enum(["Fee", "Refund", "Payment", "Purchase", "Interest"])})), credit: accountCredit.optional()})]); export const getApiAccountsAccountIdStatementsResponse = z.object({has_more: z.boolean().optional(), data: z.array(statement).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 }; /** * Get all statements for an account * * GET /accounts/{account_id}/statements */ export const getApiAccountsAccountIdStatements = async (args: GetApiAccountsAccountIdStatementsArgs, options?: GetApiAccountsAccountIdStatementsOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/statements${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(getApiAccountsAccountIdStatementsResponse, await res.json()) };