import {z} from 'zod' export type GetApiBankAccountsArgs = {classification?: ('ASSET' | 'EQUITY' | 'EXPENSE' | 'LIABILITY' | 'REVENUE' | '__null__') | undefined, created_ts_after?: string | undefined, created_ts_before?: string | undefined, currency?: ('AED' | 'AFN' | 'ALL' | 'AMD' | 'ARS' | 'AUD' | 'AZN' | 'BAM' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BND' | 'BOB' | 'BRL' | 'BWP' | 'BYN' | 'BZD' | 'CAD' | 'CDF' | 'CHF' | 'CLP' | 'CNY' | 'COP' | 'CRC' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'GBP' | 'GEL' | 'GHS' | 'GNF' | 'GTQ' | 'HKD' | 'HNL' | 'HRK' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KHR' | 'KMF' | 'KRW' | 'KWD' | 'KZT' | 'LBP' | 'LKR' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MOP' | 'MUR' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SDG' | 'SEK' | 'SGD' | 'SOS' | 'SYP' | 'THB' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'UYU' | 'UZS' | 'VES' | 'VND' | 'XAF' | 'XOF' | 'YER' | 'ZAR' | 'ZMW' | '__null__') | undefined, expand?: string | undefined, modified_ts_after?: string | undefined, modified_ts_before?: string | undefined, page?: number | undefined, page_size?: number | undefined, sub_classification?: ('BANK' | 'CREDIT_CARD' | 'PETTY_CASH' | '__null__') | undefined}; export type GetApiBankAccountsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bankAccountSubClassificationEnum = z.enum(["BANK", "PETTY_CASH", "CREDIT_CARD"]); export const apiV1BankAccount = z.object({account: z.string(), classification: z.string(), created_ts: z.string(), currency: z.string(), id: z.string(), legacy_id: z.string(), modified_ts: z.string(), name: z.string(), sub_classification: bankAccountSubClassificationEnum}); export const paginatedapiV1BankAccountList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(apiV1BankAccount)}); 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 }; /** * List bank accounts * * GET /bank-accounts/ */ export const getApiBankAccounts = async (args: GetApiBankAccountsArgs, options?: GetApiBankAccountsOptions) => { const {baseUrl = "https://api.wafeq.com/v1", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.classification != null) query.append("classification", String(args.classification)) if (args.created_ts_after != null) query.append("created_ts_after", String(args.created_ts_after)) if (args.created_ts_before != null) query.append("created_ts_before", String(args.created_ts_before)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.expand != null) query.append("expand", String(args.expand)) if (args.modified_ts_after != null) query.append("modified_ts_after", String(args.modified_ts_after)) if (args.modified_ts_before != null) query.append("modified_ts_before", String(args.modified_ts_before)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.sub_classification != null) query.append("sub_classification", String(args.sub_classification)) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/bank-accounts/${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(paginatedapiV1BankAccountList, await res.json()) };