import {z} from 'zod' export type Cfdi = string; export type Nfe = string; export type Peppol = string; export type CreateApiReceivablesArgs = {'Idempotency-Key'?: string | undefined, body: {id: string, obligor_id?: string | undefined, origination_date?: string | undefined, currency?: string | undefined, product_type?: ('term_loan' | 'line_of_credit' | 'buy_now_pay_later' | 'credit_card' | 'invoice' | 'earned_wage_access' | 'lease' | 'gig_payout' | 'creator_payout' | 'merchant_payout' | 'rental_payout' | 'marketplace_payout' | 'real_estate_commission' | 'subscription' | 'government_salary' | 'government_contract' | 'bank_transfer' | 'stock_sell_settlement') | undefined, funded_by?: ('sivo' | 'balance_sheet' | 'other') | undefined, delinquent_amount?: number | undefined, delinquent_times?: number | undefined, creditor_id?: (string | null) | undefined, origination_fee?: (number | null) | undefined, periodic_fee?: ({cycle_type: string, amount: number} | null) | undefined, collateral?: ('cash' | 'vehicle' | 'equipment' | 'all_business_assets' | 'receivables' | 'residential_property' | 'commercial_property' | 'other' | 'null' | null) | undefined, term?: (string | null) | undefined, interest_rate?: (number | null) | undefined, credit_limit?: (number | null) | undefined, payment_frequency?: (string | null) | undefined, metadata?: ({[key: string]: string | number | boolean | null} | null) | undefined, fund_name?: (string | null) | undefined, delinquent_since?: (string | null) | undefined, fiscal_id?: (Cfdi | Nfe | Peppol | null) | undefined}}; export type CreateApiReceivablesOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cfdi = z.string(); export const nfe = z.string(); export const peppol = z.string(); export const createApiReceivablesResponse = z.object({obligor_id: z.string().min(1).max(255), origination_date: z.string(), currency: z.string(), product_type: z.enum(["term_loan", "line_of_credit", "buy_now_pay_later", "credit_card", "invoice", "earned_wage_access", "lease", "gig_payout", "creator_payout", "merchant_payout", "rental_payout", "marketplace_payout", "real_estate_commission", "subscription", "government_salary", "government_contract", "bank_transfer", "stock_sell_settlement"]), funded_by: z.enum(["sivo", "balance_sheet", "other"]), delinquent_amount: z.number().int().gte(0), delinquent_times: z.number().int().gte(0).lte(2147483647), creditor_id: z.string().min(1).max(255).optional(), origination_fee: z.number().int().gte(0).optional(), periodic_fee: z.object({cycle_type: z.string(), amount: z.number().int().gte(0)}).optional(), collateral: z.enum(["cash", "vehicle", "equipment", "all_business_assets", "receivables", "residential_property", "commercial_property", "other"]).optional(), term: z.string().optional(), interest_rate: z.number().gte(-10).lte(10).optional(), credit_limit: z.number().int().gte(0).optional(), payment_frequency: z.string().optional(), metadata: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]).nullable()).optional(), fund_name: z.string().min(1).optional(), delinquent_since: z.string().optional(), fiscal_id: z.union([cfdi, nfe, peppol]).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) a receivable * * POST /receivables * * Also accepts auth: OAuth2 */ export const createApiReceivables = async (args: CreateApiReceivablesArgs, options?: CreateApiReceivablesOptions) => { 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(/\/+$/, '')}/receivables${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(createApiReceivablesResponse, await res.json()) };