import {z} from 'zod' export type GetApiAccountsAccountIdStatementsYyyyMmOptions = {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 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 a statement for an account * * GET /accounts/{account_id}/statements/{yyyy}/{mm} */ export const getApiAccountsAccountIdStatementsYyyyMm = async (options?: GetApiAccountsAccountIdStatementsYyyyMmOptions) => { 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 (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/statements/${encodeURIComponent(String(args.yyyy))}/${encodeURIComponent(String(args.mm))}${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(statement, await res.json()) };