import {z} from 'zod' export type GetApiV1BankingAccountStatementArgs = {limit: number, offset: number, date_begin: string, date_end: string}; export type GetApiV1BankingAccountStatementOptions = {baseUrl: string, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const accountStatementItem = z.object({seller_name: z.string(), seller_id: z.string(), date: z.string(), net_amount: z.string(), blocked_amount: z.string(), current_balance: z.string(), amount: z.string(), fee: z.string(), entry_type: z.string(), entry_description: z.string(), transaction_id: z.string().optional(), reference_id: z.string().optional(), payment_id: z.string().optional()}); export const accountStatementResponse = z.object({limit: z.number(), offset: z.number(), has_more: z.boolean(), total: z.number(), total_pages: z.number(), page: z.number(), resource: z.string(), items: z.array(accountStatementItem)}); 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 account statement * * GET /v1/banking/account_statement * * Also accepts auth: x-api-key, authorization */ export const getApiV1BankingAccountStatement = async (args: GetApiV1BankingAccountStatementArgs, options: GetApiV1BankingAccountStatementOptions) => { const {baseUrl, fetch: fetchFn = fetch, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.date_begin != null) query.append("date_begin", String(args.date_begin)) if (args.date_end != null) query.append("date_end", String(args.date_end)) if (authorization !== undefined) headers.set("authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/v1/banking/account_statement${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(accountStatementResponse, await res.json()) };