import {z} from 'zod' export type GetApiV1BankingFutureTransferArgs = {limit: number, offset: number, expected_date_begin: string, expected_date_end: string}; export type GetApiV1BankingFutureTransferOptions = {baseUrl: string, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const futureTransferItem = z.object({expected_on: z.string(), amount: z.string(), bank_account: z.string()}); export const futureTransferResponse = 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(futureTransferItem)}); 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 future account transfers * * GET /v1/banking/future_transfer * * Also accepts auth: x-api-key, authorization */ export const getApiV1BankingFutureTransfer = async (args: GetApiV1BankingFutureTransferArgs, options: GetApiV1BankingFutureTransferOptions) => { 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.expected_date_begin != null) query.append("expected_date_begin", String(args.expected_date_begin)) if (args.expected_date_end != null) query.append("expected_date_end", String(args.expected_date_end)) if (authorization !== undefined) headers.set("authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/v1/banking/future_transfer${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(futureTransferResponse, await res.json()) };