import {z} from 'zod' export type AccountingEmail = {email?: string | undefined, type?: ('WORK' | 'HOME' | 'OTHER') | undefined}; export type PropertyAccountingContactEmails = Array; export type AccountingTelephone = {telephone?: string | undefined, type?: ('WORK' | 'HOME' | 'OTHER' | 'FAX' | 'MOBILE') | undefined}; export type PropertyAccountingContactTelephones = Array; export type PropertyAccountingContactBillingAddress = {address1?: string | undefined, address2?: string | undefined, city?: string | undefined, region?: string | undefined, region_code?: string | undefined, postal_code?: string | undefined, country?: string | undefined, country_code?: string | undefined}; export type PropertyAccountingContactShippingAddress = {address1?: string | undefined, address2?: string | undefined, city?: string | undefined, region?: string | undefined, region_code?: string | undefined, postal_code?: string | undefined, country?: string | undefined, country_code?: string | undefined}; export type AccountingContactPaymentMethod = {id?: string | undefined, type: 'ACH' | 'ALIPAY' | 'CARD' | 'GIROPAY' | 'IDEAL' | 'OTHER' | 'PAYPAL' | 'WIRE' | 'CHECK', name?: string | undefined, default?: boolean | undefined}; export type PropertyAccountingContactPaymentMethods = Array; export type PropertyAccountingAssociatedContactEmails = Array; export type AccountingAssociatedContact = {id?: string | undefined, name?: string | undefined, emails?: PropertyAccountingAssociatedContactEmails | undefined}; export type PropertyAccountingContactAssociatedContacts = Array; export type AccountingMetadata = {id?: string | undefined, slug?: string | undefined, value?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined, namespace?: string | undefined, format?: ('TEXT' | 'NUMBER' | 'DATE' | 'BOOLEAN' | 'FILE' | 'TEXTAREA' | 'SINGLE_SELECT' | 'MULTIPLE_SELECT' | 'MEASUREMENT' | 'PRICE' | 'YES_NO' | 'CURRENCY' | 'URL') | undefined, extra_data?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined}; export type PropertyAccountingContactMetadata = Array; export type AccountingContact = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, name?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, emails?: PropertyAccountingContactEmails | undefined, telephones?: PropertyAccountingContactTelephones | undefined, currency?: string | undefined, billing_address?: PropertyAccountingContactBillingAddress | undefined, shipping_address?: PropertyAccountingContactShippingAddress | undefined, is_active?: boolean | undefined, tax_exemption?: ('FEDERAL_GOV' | 'REGION_GOV' | 'LOCAL_GOV' | 'TRIBAL_GOV' | 'CHARITABLE_ORG' | 'RELIGIOUS_ORG' | 'EDUCATIONAL_ORG' | 'MEDICAL_ORG' | 'RESALE' | 'FOREIGN' | 'OTHER') | undefined, tax_number?: string | undefined, is_customer?: boolean | undefined, is_supplier?: boolean | undefined, portal_url?: string | undefined, payment_methods?: PropertyAccountingContactPaymentMethods | undefined, company_name?: string | undefined, identification?: string | undefined, associated_contacts?: PropertyAccountingContactAssociatedContacts | undefined, organization_id?: string | undefined, payment_terms?: ('ON_RECEIPT' | 'NET_7' | 'NET_10' | 'NET_15' | 'NET_20' | 'NET_25' | 'NET_30' | 'NET_45' | 'NET_60' | 'NET_90' | 'OTHER') | undefined, paymentterm_id?: string | undefined, balance_amount?: number | undefined, website?: string | undefined, notes?: string | undefined, metadata?: PropertyAccountingContactMetadata | undefined, raw?: {[key: string]: unknown} | undefined}; export type CreateApiAccountingConnectionIdContactArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'name' | 'first_name' | 'last_name' | 'emails' | 'telephones' | 'currency' | 'billing_address' | 'shipping_address' | 'is_active' | 'tax_exemption' | 'tax_number' | 'is_customer' | 'is_supplier' | 'portal_url' | 'payment_methods' | 'company_name' | 'identification' | 'associated_contacts' | 'organization_id' | 'payment_terms' | 'paymentterm_id' | 'balance_amount' | 'website' | 'notes' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, body: AccountingContact}; export type CreateApiAccountingConnectionIdContactOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const accountingEmail = z.object({email: z.string().optional(), type: z.enum(["WORK", "HOME", "OTHER"]).optional()}); export const propertyAccountingContactEmails = z.array(accountingEmail); export const accountingTelephone = z.object({telephone: z.string().optional(), type: z.enum(["WORK", "HOME", "OTHER", "FAX", "MOBILE"]).optional()}); export const propertyAccountingContactTelephones = z.array(accountingTelephone); export const propertyAccountingContactBillingAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional()}); export const propertyAccountingContactShippingAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional()}); export const accountingContactPaymentMethod = z.object({id: z.string().optional(), type: z.enum(["ACH", "ALIPAY", "CARD", "GIROPAY", "IDEAL", "OTHER", "PAYPAL", "WIRE", "CHECK"]), name: z.string().optional(), default: z.boolean().optional()}); export const propertyAccountingContactPaymentMethods = z.array(accountingContactPaymentMethod); export const propertyAccountingAssociatedContactEmails = z.array(accountingEmail); export const accountingAssociatedContact = z.object({id: z.string().optional(), name: z.string().optional(), emails: propertyAccountingAssociatedContactEmails.optional()}); export const propertyAccountingContactAssociatedContacts = z.array(accountingAssociatedContact); export const accountingMetadata = z.object({id: z.string().optional(), slug: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional()}); export const propertyAccountingContactMetadata = z.array(accountingMetadata); export const accountingContact = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), emails: propertyAccountingContactEmails.optional(), telephones: propertyAccountingContactTelephones.optional(), currency: z.string().optional(), billing_address: propertyAccountingContactBillingAddress.optional(), shipping_address: propertyAccountingContactShippingAddress.optional(), is_active: z.boolean().optional(), tax_exemption: z.enum(["FEDERAL_GOV", "REGION_GOV", "LOCAL_GOV", "TRIBAL_GOV", "CHARITABLE_ORG", "RELIGIOUS_ORG", "EDUCATIONAL_ORG", "MEDICAL_ORG", "RESALE", "FOREIGN", "OTHER"]).optional(), tax_number: z.string().optional(), is_customer: z.boolean().optional(), is_supplier: z.boolean().optional(), portal_url: z.string().optional(), payment_methods: propertyAccountingContactPaymentMethods.optional(), company_name: z.string().optional(), identification: z.string().optional(), associated_contacts: propertyAccountingContactAssociatedContacts.optional(), organization_id: z.string().optional(), payment_terms: z.enum(["ON_RECEIPT", "NET_7", "NET_10", "NET_15", "NET_20", "NET_25", "NET_30", "NET_45", "NET_60", "NET_90", "OTHER"]).optional(), paymentterm_id: z.string().optional(), balance_amount: z.number().optional(), website: z.string().optional(), notes: z.string().optional(), metadata: propertyAccountingContactMetadata.optional(), raw: z.record(z.string(), z.unknown()).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 contact * * POST /accounting/{connection_id}/contact */ export const createApiAccountingConnectionIdContact = async (args: CreateApiAccountingConnectionIdContactArgs, options?: CreateApiAccountingConnectionIdContactOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/accounting/${encodeURIComponent(String(args.connection_id))}/contact${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(accountingContact, await res.json()) };