import {z} from 'zod' export type RevAppComponentsLink = {rel?: string | undefined, href?: string | undefined}; export type RevAppPostModelsAddress = {override_address_verification?: boolean | undefined, address_id?: number | undefined, customer_id?: number | undefined, first_name?: string | undefined, middle_initial?: string | undefined, last_name?: string | undefined, company_name?: string | undefined, line_1?: string | undefined, line_2?: string | undefined, city?: string | undefined, state_or_province?: string | undefined, postal_code?: string | undefined, postal_code_extension?: string | undefined, country_code?: ('AFG' | 'ALA' | 'ALB' | 'DZA' | 'ASM' | 'AND' | 'AGO' | 'AIA' | 'ATA' | 'ATG' | 'ARG' | 'ARM' | 'ABW' | 'AUS' | 'AUT' | 'AZE' | 'BHS' | 'BHR' | 'BGD' | 'BRB' | 'BLR' | 'BEL' | 'BLZ' | 'BEN' | 'BMU' | 'BTN' | 'BOL' | 'BES' | 'BIH' | 'BWA' | 'BVT' | 'BRA' | 'IOT' | 'BRN' | 'BGR' | 'BFA' | 'BDI' | 'CPV' | 'KHM' | 'CMR' | 'CAN' | 'CYM' | 'CAF' | 'TCD' | 'CHL' | 'CHN' | 'CXR' | 'CCK' | 'COL' | 'COM' | 'COG' | 'COD' | 'COK' | 'CRI' | 'CIV' | 'HRV' | 'CUB' | 'CUW' | 'CYP' | 'CZE' | 'DNK' | 'DJI' | 'DMA' | 'DOM' | 'ECU' | 'EGY' | 'SLV' | 'GNQ' | 'ERI' | 'EST' | 'ETH' | 'FLK' | 'FRO' | 'FJI' | 'FIN' | 'FRA' | 'GUF' | 'PYF' | 'ATF' | 'GAB' | 'GMB' | 'GEO' | 'DEU' | 'GHA' | 'GIB' | 'GRC' | 'GRL' | 'GRD' | 'GLP' | 'GUM' | 'GTM' | 'GGY' | 'GIN' | 'GNB' | 'GUY' | 'HTI' | 'HMD' | 'VAT' | 'HND' | 'HKG' | 'HUN' | 'ISL' | 'IND' | 'IDN' | 'IRN' | 'IRQ' | 'IRL' | 'IMN' | 'ISR' | 'ITA' | 'JAM' | 'JPN' | 'JEY' | 'JOR' | 'KAZ' | 'KEN' | 'KIR' | 'PRK' | 'KOR' | 'XKX' | 'KWT' | 'KGZ' | 'LAO' | 'LVA' | 'LBN' | 'LSO' | 'LBR' | 'LBY' | 'LIE' | 'LTU' | 'LUX' | 'MAC' | 'MKD' | 'MDG' | 'MWI' | 'MYS' | 'MDV' | 'MLI' | 'MLT' | 'MHL' | 'MTQ' | 'MRT' | 'MUS' | 'MYT' | 'MEX' | 'FSM' | 'MDA' | 'MCO' | 'MNG' | 'MNE' | 'MSR' | 'MAR' | 'MOZ' | 'MMR' | 'NAM' | 'NRU' | 'NPL' | 'NLD' | 'NCL' | 'NZL' | 'NIC' | 'NER' | 'NGA' | 'NIU' | 'NFK' | 'MNP' | 'NOR' | 'OMN' | 'PAK' | 'PLW' | 'PSE' | 'PAN' | 'PNG' | 'PRY' | 'PER' | 'PHL' | 'PCN' | 'POL' | 'PRT' | 'PRI' | 'QAT' | 'REU' | 'ROU' | 'RUS' | 'RWA' | 'BLM' | 'SHN' | 'KNA' | 'LCA' | 'MAF' | 'SPM' | 'VCT' | 'WSM' | 'SMR' | 'STP' | 'SAU' | 'SEN' | 'SRB' | 'SYC' | 'SLE' | 'SGP' | 'SXM' | 'SVK' | 'SVN' | 'SLB' | 'SOM' | 'ZAF' | 'SGS' | 'SSD' | 'ESP' | 'LKA' | 'SDN' | 'SUR' | 'SJM' | 'SWZ' | 'SWE' | 'CHE' | 'SYR' | 'TWN' | 'TJK' | 'TZA' | 'THA' | 'TLS' | 'TGO' | 'TKL' | 'TON' | 'TTO' | 'TUN' | 'TUR' | 'TKM' | 'TCA' | 'TUV' | 'UGA' | 'UKR' | 'ARE' | 'GBR' | 'USA' | 'UMI' | 'URY' | 'UZB' | 'VUT' | 'VEN' | 'VNM' | 'VGB' | 'VIR' | 'WLF' | 'ESH' | 'YEM' | 'ZMB' | 'ZWE' | 'ANT' | 'SCG' | 'YUG') | undefined, created_date?: string | undefined, modified_date?: string | undefined, is_provider_validated?: boolean | undefined, is_USPS_validated?: boolean | undefined, links?: Array | undefined}; export type RevAppModelsCustomersAutoDebit = {enabled?: boolean | undefined, day?: number | undefined, payment_account_id?: number | undefined}; export type RevAppModelsCustomersFinance = {amount_due?: number | undefined, amount_overdue?: number | undefined, bill_profile_id?: number | undefined, billing_method?: ('PRINT_ONLY' | 'EMAIL_ONLY' | 'BOTH' | 'NONE') | undefined, balance?: number | undefined, balance_limit?: number | undefined, balance_limit_enabled?: boolean | undefined, due_date?: string | undefined, collection_template_id?: number | undefined, contract_end_date?: string | undefined, contract_start_date?: string | undefined, cycle_date?: string | undefined, late_fees_enabled?: boolean | undefined, payment_terms_days?: number | undefined, tax_exempt_enabled?: boolean | undefined, tax_exempt_types?: string | undefined, usage_file_enabled?: boolean | undefined, usage_file_format?: ('H2O' | 'EMI' | 'Reseller') | undefined, payment_fees_enabled?: boolean | undefined, links?: Array | undefined}; export type RevAppModelsCustomersProfile = {class?: ('Business' | 'Residential') | undefined, date_of_birth?: string | undefined, language?: string | undefined, tax_id?: string | undefined}; export type RevAppModelsFieldValue = {field_id?: number | undefined, label?: string | undefined, value?: string | undefined, links?: Array | undefined}; export type RevAppPostModelsCustomer = {billing_address?: RevAppPostModelsAddress | undefined, service_address?: RevAppPostModelsAddress | undefined, listing_address?: RevAppPostModelsAddress | undefined, customer_id?: number | undefined, parent_customer_id?: number | undefined, account_number?: string | undefined, activated_date?: string | undefined, agent_id?: number | undefined, secondary_agent_id?: number | undefined, close_date?: string | undefined, created_by?: number | undefined, created_date?: string | undefined, updated_date?: string | undefined, email?: string | undefined, is_parent?: boolean | undefined, registration_code?: string | undefined, security_pin?: string | undefined, sold_by?: number | undefined, source?: string | undefined, status?: ('ALL' | 'PROSPECT' | 'PENDING' | 'CLOSED' | 'OPEN') | undefined, auto_debit?: RevAppModelsCustomersAutoDebit | undefined, finance?: RevAppModelsCustomersFinance | undefined, profile?: RevAppModelsCustomersProfile | undefined, fields?: Array | undefined, suspended?: boolean | undefined, suspended_date?: string | undefined, culture?: string | undefined, prevent_closing_for_no_active_service_products?: boolean | undefined, links?: Array | undefined}; export type CreateApiV1CustomersArgs = {body: RevAppPostModelsCustomer}; export type CreateApiV1CustomersOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revWebAPIModelsCreate = z.object({ok: z.boolean().optional(), id: z.number().int().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 }; /** * Create a new customer * * POST /v1/Customers */ export const createApiV1Customers = async (args: CreateApiV1CustomersArgs, options?: CreateApiV1CustomersOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Customers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(revWebAPIModelsCreate, await res.json()) };