import {z} from 'zod' export type GetApi10KbAccountsSearchSearchKeyArgs = {searchKey: string, offset?: number | undefined, limit?: number | undefined, accountWithBalance?: boolean | undefined, accountWithBalanceAndCBA?: boolean | undefined, audit?: ('FULL' | 'MINIMAL' | 'NONE') | undefined}; export type GetApi10KbAccountsSearchSearchKeyOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const entity = z.object({id: z.string().optional(), updatedDate: z.string().optional(), createdDate: z.string().optional()}); export const auditLog = z.object({changeType: z.string().optional(), changeDate: z.string().optional(), objectType: z.enum(["ACCOUNT", "ACCOUNT_EMAIL", "BLOCKING_STATES", "BUNDLE", "CUSTOM_FIELD", "INVOICE", "PAYMENT", "TRANSACTION", "INVOICE_ITEM", "INVOICE_PAYMENT", "SUBSCRIPTION", "SUBSCRIPTION_EVENT", "SERVICE_BROADCAST", "PAYMENT_ATTEMPT", "PAYMENT_METHOD", "TAG", "TAG_DEFINITION", "TENANT", "TENANT_KVS"]).optional(), objectId: z.string().optional(), changedBy: z.string().optional(), reasonCode: z.string().optional(), comments: z.string().optional(), userToken: z.string().optional(), history: entity.optional()}); export const account = z.object({accountId: z.string().optional(), name: z.string().optional(), firstNameLength: z.number().int().optional(), externalKey: z.string().optional(), email: z.string().optional(), billCycleDayLocal: z.number().int().optional(), currency: z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYR", "BZD", "CAD", "CDF", "CHF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LVL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "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", "SEK", "SGD", "SHP", "SLL", "SOS", "SPL", "SRD", "STD", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TVD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XCD", "XDR", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWD", "BTC"]).optional(), parentAccountId: z.string().optional(), isPaymentDelegatedToParent: z.boolean().optional(), paymentMethodId: z.string().optional(), referenceTime: z.string().optional(), timeZone: z.string().optional(), address1: z.string().optional(), address2: z.string().optional(), postalCode: z.string().optional(), company: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), locale: z.string().optional(), phone: z.string().optional(), notes: z.string().optional(), isMigrated: z.boolean().optional(), accountBalance: z.number().optional(), accountCBA: z.number().optional(), auditLogs: z.array(auditLog).optional()}); export const getApi10KbAccountsSearchSearchKeyResponse = z.array(account); 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 }; /** * Search accounts * * GET /1.0/kb/accounts/search/{searchKey} * * Also accepts auth: Killbill_Api_Key, Killbill_Api_Secret */ export const getApi10KbAccountsSearchSearchKey = async (args: GetApi10KbAccountsSearchSearchKeyArgs, options: GetApi10KbAccountsSearchSearchKeyOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.accountWithBalance != null) query.append("accountWithBalance", String(args.accountWithBalance)) if (args.accountWithBalanceAndCBA != null) query.append("accountWithBalanceAndCBA", String(args.accountWithBalanceAndCBA)) if (args.audit != null) query.append("audit", String(args.audit)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/kb/accounts/search/${encodeURIComponent(String(args.searchKey))}${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(getApi10KbAccountsSearchSearchKeyResponse, await res.json()) };