import {z} from 'zod' export type Duns = string; export type Ein = string; export type Ssn = string; export type Rfc = string; export type Nit = string; export type Nif = string; export type Crn = string; export type Bn = string; export type Paydex = string; export type Experian = string; export type Datacredito = string; export type Transunion = string; export type CreateApiAccountsArgs = {'Idempotency-Key'?: string | undefined, body: {id: string, name?: string | undefined, type?: ('business' | 'individual') | undefined, first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, address?: ({region?: string | undefined, country: string, formatted?: string | undefined, street_address?: string | undefined, locality?: string | undefined, postal_code?: string | undefined} | null) | undefined, shipping?: ({region?: string | undefined, country: string, formatted?: string | undefined, street_address?: string | undefined, locality?: string | undefined, postal_code?: string | undefined} | null) | undefined, website?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, industry?: (string | null) | undefined, legal_entity_id?: (Duns | Ein | Ssn | Rfc | Nit | Nif | Crn | Bn | null) | undefined, credit_score?: (Paydex | Experian | Datacredito | Transunion | null) | undefined, kyx_status?: ('draft' | 'submitted' | 'approved' | 'rejected' | 'null' | null) | undefined, active_since?: (string | null) | undefined, monthly_revenue?: (number | null) | undefined, monthly_costs?: (number | null) | undefined, cash_on_hand?: (number | null) | undefined, avg_sale_size?: (number | null) | undefined, performance_score?: (number | null) | undefined, buyer_concentration?: (number | null) | undefined, average_days_to_pay?: (number | null) | undefined, lifetime_spend?: (number | null) | undefined, customer_count?: (number | null) | undefined, insurance?: ({rating: string, limit: number, coverage: number} | null) | undefined, metadata?: ({[key: string]: string | number | boolean | null} | null) | undefined}}; export type CreateApiAccountsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const duns = z.string(); export const ein = z.string(); export const ssn = z.string(); export const rfc = z.string(); export const nit = z.string(); export const nif = z.string(); export const crn = z.string(); export const bn = z.string(); export const paydex = z.string(); export const experian = z.string(); export const datacredito = z.string(); export const transunion = z.string(); export const createApiAccountsResponse = z.object({name: z.string().min(1), type: z.enum(["business", "individual"]), first_name: z.string().min(1).optional(), last_name: z.string().min(1).optional(), address: z.object({region: z.string().min(1).optional(), country: z.string(), formatted: z.string().min(1).optional(), street_address: z.string().min(1).optional(), locality: z.string().min(1).optional(), postal_code: z.string().min(1).optional()}).optional(), shipping: z.object({region: z.string().min(1).optional(), country: z.string(), formatted: z.string().min(1).optional(), street_address: z.string().min(1).optional(), locality: z.string().min(1).optional(), postal_code: z.string().min(1).optional()}).optional(), website: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), industry: z.string().optional(), legal_entity_id: z.union([duns, ein, ssn, rfc, nit, nif, crn, bn]).optional(), credit_score: z.union([paydex, experian, datacredito, transunion]).optional(), kyx_status: z.enum(["draft", "submitted", "approved", "rejected"]).optional(), active_since: z.string().optional(), monthly_revenue: z.number().int().optional(), monthly_costs: z.number().int().optional(), cash_on_hand: z.number().int().optional(), avg_sale_size: z.number().int().optional(), performance_score: z.number().int().gte(1).lte(10).optional(), buyer_concentration: z.number().gte(-10).lte(10).optional(), average_days_to_pay: z.number().int().gte(0).lte(2147483647).optional(), lifetime_spend: z.number().int().optional(), customer_count: z.number().int().gte(0).lte(2147483647).optional(), insurance: z.object({rating: z.string(), limit: z.number().int(), coverage: z.number().gte(-10).lte(10)}).optional(), metadata: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]).nullable()).optional(), id: z.string().min(1).max(255), created_at: z.string(), created_by: z.string(), updated_at: z.string(), updated_by: z.string()}); 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 or update (upsert) an account * * POST /accounts * * Also accepts auth: OAuth2 */ export const createApiAccounts = async (args: CreateApiAccountsArgs, options?: CreateApiAccountsOptions) => { const {baseUrl = "https://core.sivo.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/accounts${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(createApiAccountsResponse, await res.json()) };