import {z} from 'zod' export type GetApiAccountsArgs = {'x-entity-id'?: string | undefined}; export type GetApiAccountsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pageMetadata = z.object({nextOffset: z.string().nullable().optional()}); export const identifier = z.string().max(256); export const accountType = z.enum(["BROKERAGEPRODUCT", "CHECKING", "MONEYMARKET", "SAVINGS", "TAXABLE"]); export const accountStatus = z.enum(["CLOSED", "DELINQUENT", "NEGATIVECURRENTBALANCE", "OPEN", "PAID", "PENDINGCLOSE", "PENDINGOPEN", "RESTRICTED"]); export const iso4217Code = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BOV", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLF", "CLP", "CNY", "COP", "COU", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MXV", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SLE", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "SSP", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "USN", "UYI", "UYU", "UYW", "UZS", "VED", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XOF", "XPD", "XPF", "XPT", "XSU", "XTS", "XUA", "XXX", "YER", "ZAR", "ZMW", "ZWG", "ZWL"]); export const currency = z.object({currencyCode: iso4217Code}); export const depositAccountInfo = z.object({accountId: identifier, accountType: accountType, accountNumberDisplay: z.string().nullable().optional(), productName: z.string(), nickname: z.string().nullable().optional(), status: accountStatus, currency: currency, bankProductType: z.string().nullable().optional(), referenceId: z.string().nullable().optional()}); export const depositAccountDescriptor = z.object({depositAccount: depositAccountInfo}); export const accountDescriptor = z.object({accountId: identifier, accountType: accountType, accountNumberDisplay: z.string().nullable().optional(), productName: z.string(), nickname: z.string().nullable().optional(), status: accountStatus, currency: currency}); export const investmentAccountDescriptor = z.object({investmentAccount: accountDescriptor}); export const accountCategory = z.enum(["DEPOSIT_ACCOUNT", "DIGITAL_WALLET", "INVESTMENT_ACCOUNT"]); export const digitalWalletType = z.literal("DIGITALWALLET"); export const digitalWalletInfo = z.object({accountId: identifier, accountCategory: accountCategory.optional(), accountType: digitalWalletType, accountNumberDisplay: z.string().nullable().optional(), productName: z.string(), nickname: z.string().nullable().optional(), status: accountStatus, currency: currency}); export const digitalWalletDescriptor = z.object({digitalWallet: digitalWalletInfo}); export const accountWithDescriptor = z.union([depositAccountDescriptor, investmentAccountDescriptor, digitalWalletDescriptor]); export const accounts = z.object({page: pageMetadata.nullable().optional(), accounts: z.array(accountWithDescriptor)}); 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 /accounts */ export const getApiAccounts = async (args: GetApiAccountsArgs, options?: GetApiAccountsOptions) => { const {baseUrl = "https://api.meow.com/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-entity-id'] != null) headers.set("x-entity-id", String(args['x-entity-id'])) if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/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(accounts, await res.json()) };