import {z} from 'zod' export type GetApiCustomersIdentifierArgs = {identifier: string | string}; export type GetApiCustomersIdentifierOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumber = z.object({country_code: z.string().min(1).max(7).optional(), number: z.string().max(25).optional()}); export const address = z.object({address_line1: z.string().max(200).optional(), address_line2: z.string().max(200).optional(), city: z.string().max(50).optional(), state: z.string().max(3).optional(), zip: z.string().max(50).optional(), country: z.string().min(2).max(2).optional()}); export const accountHolder = z.discriminatedUnion("type", [z.object({type: z.enum(["individual", "corporate", "government"]), first_name: z.string(), last_name: z.string(), tax_id: z.string().optional(), date_of_birth: z.string().optional(), country_of_birth: z.string().optional(), residential_status: z.enum(["resident", "non_resident"]).optional(), billing_address: address, phone: phoneNumber.optional(), identification: z.object({type: z.string().optional(), issuing_country: z.string().optional(), number: z.string().optional()}).optional()}), z.object({type: z.enum(["individual", "corporate", "government"]), company_name: z.string(), tax_id: z.string().optional(), billing_address: address.optional()}), z.object({type: z.enum(["individual", "corporate", "government"]), company_name: z.string(), tax_id: z.string().optional(), billing_address: address.optional()})]); export const bankDetails = z.object({name: z.string().max(128).optional(), branch: z.string().max(128).optional(), address: address.optional()}); export const instrumentMetadata = z.record(z.string(), z.string().max(100)); export const retrieveInstrumentCustomerResponse = z.object({id: z.string(), email: z.string().optional(), name: z.string().optional(), default: z.boolean().optional(), metadata: z.object({}).optional()}); export const retrieveCustomerInstrumentResponse = z.discriminatedUnion("type", [z.object({type: z.string(), id: z.string(), fingerprint: z.string(), account_type: z.enum(["savings", "current", "cash"]).optional(), account_number: z.string().optional(), bank_code: z.string().optional(), branch_code: z.string().optional(), iban: z.string().optional(), bban: z.string().optional(), swift_bic: z.string().optional(), currency: z.string(), country: z.string(), account_holder: accountHolder.optional(), bank: bankDetails.optional(), metadata: instrumentMetadata.optional()}), z.object({type: z.string(), id: z.string(), fingerprint: z.string(), expiry_month: z.number().int().gte(1), expiry_year: z.number().int(), scheme: z.string().optional(), last4: z.string().min(4).max(4), bin: z.string(), card_type: z.enum(["CREDIT", "DEBIT", "PREPAID", "CHARGE"]).optional(), card_category: z.enum(["CONSUMER", "COMMERCIAL"]).optional(), issuer: z.string().optional(), issuer_country: z.string().min(2).max(2).optional(), product_id: z.string().optional(), product_type: z.string().optional(), account_holder: z.object({first_name: z.string().optional(), last_name: z.string().optional(), billing_address: address.optional(), phone: phoneNumber.optional()}).optional(), card_wallet_type: z.enum(["googlepay", "applepay"]).optional(), metadata: instrumentMetadata.optional()}), z.object({type: z.string(), id: z.string(), fingerprint: z.string(), created_on: z.string(), modified_on: z.string(), vault_id: z.string(), instrument_data: z.object({account_type: z.enum(["savings", "checking"]), account_number: z.string().min(4).max(17), bank_code: z.string().min(8).max(9), currency: z.string().min(3).max(3), country: z.string().min(2).max(2)}), account_holder: z.object({first_name: z.string(), last_name: z.string(), company_name: z.string(), type: z.enum(["individual", "corporate"])}), customer: retrieveInstrumentCustomerResponse.optional()}), z.object({type: z.string(), id: z.string(), fingerprint: z.string(), created_on: z.string(), modified_on: z.string(), vault_id: z.string(), instrument_data: z.object({account_number: z.string().min(15).max(34), country: z.string().min(2).max(2), currency: z.string().min(3).max(3), payment_type: z.enum(["recurring", "regular"]), mandate_id: z.string().min(1).max(35), date_of_signature: z.string().min(10).max(10)}).optional(), account_holder: z.object({first_name: z.string(), last_name: z.string(), billing_address: z.object({address_line1: z.string(), address_line2: z.string(), city: z.string(), zip: z.string(), country: z.string().min(2).max(2)})}).optional(), customer: retrieveInstrumentCustomerResponse.optional()})]); export const retrieveCustomerResponse = z.object({id: z.string(), email: z.string(), default: z.string().optional(), name: z.string().optional(), phone: phoneNumber.optional(), metadata: z.object({}).optional(), instruments: z.array(retrieveCustomerInstrumentResponse).optional()}); 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 customer details * * GET /customers/{identifier} * * Also accepts auth: ApiSecretKey */ export const getApiCustomersIdentifier = async (args: GetApiCustomersIdentifierArgs, options: GetApiCustomersIdentifierOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.identifier))}${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(retrieveCustomerResponse, await res.json()) };