import {z} from 'zod' export type GetApiCustomersArgs = {limit?: number | undefined, page?: string | undefined, filter?: ({email?: ({eq?: string | undefined}) | undefined, business_name?: ({eq?: string | undefined}) | undefined, external_id?: ({eq?: string | undefined}) | undefined, created_at?: ({eq?: string | undefined, gt?: string | undefined, lt?: string | undefined, gte?: string | undefined, lte?: string | undefined}) | undefined, amount_approved?: ({eq?: number | undefined, gt?: number | undefined, lt?: number | undefined, gte?: number | undefined, lte?: number | undefined}) | undefined, amount_available?: ({eq?: number | undefined, gt?: number | undefined, lt?: number | undefined, gte?: number | undefined, lte?: number | undefined}) | undefined, amount_authorized?: ({eq?: number | undefined, gt?: number | undefined, lt?: number | undefined, gte?: number | undefined, lte?: number | undefined}) | undefined, amount_balance?: ({eq?: number | undefined, gt?: number | undefined, lt?: number | undefined, gte?: number | undefined, lte?: number | undefined}) | undefined, amount_unapplied_payments?: ({eq?: number | undefined, gt?: number | undefined, lt?: number | undefined, gte?: number | undefined, lte?: number | undefined}) | undefined, advance_rate?: ({eq?: number | undefined, gt?: number | undefined, lt?: number | undefined, gte?: number | undefined, lte?: number | undefined}) | undefined, archived?: ({eq?: boolean | undefined, ne?: boolean | undefined}) | undefined}) | undefined, sort?: string | undefined}; export type GetApiCustomersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const customer = z.object({id: z.string().optional(), external_id: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), source: z.enum(["QUICKBOOKS", "MERCHANT_USER", "ADMIN_USER", "APPLICATION", "CUSTOMER_USER", "API"]).optional(), business_address: z.string().optional(), business_city: z.string().optional(), business_state: z.string().optional(), business_zip: z.string().optional(), business_country: z.string().optional(), business_age_range: z.enum(["0-2", "2-5", "5-10", "10+"]).optional(), business_ap_email: z.string().optional(), business_ap_phone: z.string().optional(), business_ap_phone_extension: z.string().optional(), business_name: z.string().optional(), business_trade_name: z.string().optional(), business_phone: z.string().optional(), business_type: z.enum(["sole_prop_or_partnership", "llc", "corporation", "nonprofit", "government"]).optional(), email: z.string().optional(), personal_name_first: z.string().optional(), personal_name_last: z.string().optional(), personal_phone: z.string().optional(), amount_approved: z.number().int().optional(), amount_authorized: z.number().int().optional(), amount_available: z.number().int().optional(), amount_balance: z.number().int().optional(), amount_unapplied_payments: z.number().int().optional(), default_terms: z.enum(["net7", "net10", "net10th", "net15", "net20", "net30", "net45", "net60", "net75", "net90", "net120", "net180", "null"]).nullable().optional(), finance_type: z.enum(["advanced", "non_advanced"]).optional(), advance_rate: z.number().gte(0).lte(1).nullable().optional(), credit_status: z.enum(["approved", "hold", "declined", "pending", "deactivated", "null"]).nullable().optional(), net_terms_status: z.enum(["enrolled", "pending_enrollment", "enrollment_expired", "null"]).nullable().optional(), net_terms_enrollment_url: z.string().nullable().optional(), net_terms_enrollment_expires_at: z.string().nullable().optional(), credit_check_requested_at: z.string().nullable().optional(), archived: z.boolean().optional(), duns_number: z.string().nullable().optional(), credit_decisions: z.array(z.object({id: z.string().optional(), created_at: z.string().optional(), decision: z.enum(["approved", "declined", "hold", "line_adjustment", "deactivate"]).optional(), amount: z.number().int().optional(), advance_rate: z.number().gte(0).lte(1).optional(), decline_code: z.enum(["no_business_found", "thin_file", "failing_credit", "not_approved_need_more_info", "sole_prop", "applicant_business_association", "merchant_risk", "fraud", "no_us_address", "duplicate", "test_account", "email_verification_freemail", "email_verification_domain", "address_verification", "poor_cash_flow", "additional_info_not_provided", "other"]).nullable().optional(), hold_code: z.enum(["past_due", "failed_payment", "requested_by_customer", "requested_by_merchant", "dispute", "fraud", "churned", "payment_plan", "inactive", "other"]).nullable().optional()})).optional()}); export const customerList = z.object({count: z.number().int().optional(), limit: z.number().int().optional(), page: z.number().int().optional(), results: z.array(customer).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 }; /** * List all customers * * GET /customers * * Also accepts auth: basicAuth */ export const getApiCustomers = async (args: GetApiCustomersArgs, options?: GetApiCustomersOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = 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.page != null) query.append("page", String(args.page)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (args.sort != null) query.append("sort", String(args.sort)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers${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(customerList, await res.json()) };