import {z} from 'zod' export type GetApiAccountingConnectionIdBillArgs = {limit?: number | undefined, offset?: number | undefined, updated_gte?: string | undefined, sort?: string | undefined, order?: string | undefined, query?: string | undefined, contact_id?: string | undefined, org_id?: string | undefined, start_gte?: string | undefined, end_lt?: string | undefined, fields?: (Array<'id' | 'bill_number' | 'created_at' | 'updated_at' | 'due_at' | 'paid_at' | 'refunded_at' | 'cancelled_at' | 'posted_at' | 'total_amount' | 'paid_amount' | 'refund_amount' | 'tax_amount' | 'discount_amount' | 'balance_amount' | 'contact_id' | 'currency' | 'notes' | 'extended_notes' | 'refund_reason' | 'lineitems' | 'status' | 'url' | 'payment_collection_method' | 'attachments' | 'send' | 'organization_id' | 'term' | 'payment_terms' | 'paymentterm_id' | 'payments' | 'category_ids' | 'project_id' | 'metadata' | 'sub_total_amount' | 'exchange_rate' | 'purchaseorder_ids' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string}; export type GetApiAccountingConnectionIdBillOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyAccountingExtendedNoteUser = z.object({id: z.string().optional(), name: z.string().optional()}); export const accountingExtendedNote = z.object({note: z.string().optional(), user: propertyAccountingExtendedNoteUser.optional(), date: z.string().optional(), visibility: z.enum(["PUBLIC", "PRIVATE"]).optional()}); export const propertyAccountingBillExtendedNotes = z.array(accountingExtendedNote); export const propertyAccountingLineitemCategoryIds = z.array(z.string()); export const accountingReference = z.object({id: z.string().optional(), name: z.string().optional()}); export const propertyAccountingLineitemLocations = z.array(accountingReference); export const propertyAccountingLineitemItemVariants = z.array(accountingReference); export const accountingFee = z.object({type: z.enum(["TAX", "DISCOUNT", "PROMOTION", "SHIPPING", "GIFT_WRAP", "COD", "SURCHARGE", "TIP", "SERVICE_CHARGE", "OTHER"]), original_type: z.string().optional(), amount: z.number(), currency: z.string().optional()}); export const propertyAccountingLineitemFees = z.array(accountingFee); export const accountingLineitem = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), refunded_at: z.string().optional(), total_amount: z.number().optional(), refund_amount: z.number().optional(), discount_amount: z.number().optional(), tax_amount: z.number().optional(), item_id: z.string().optional(), unit_amount: z.number().optional(), unit_quantity: z.number().optional(), item_sku: z.string().optional(), item_name: z.string().optional(), item_description: z.string().optional(), notes: z.string().optional(), taxrate_id: z.string().optional(), account_id: z.string().optional(), category_ids: propertyAccountingLineitemCategoryIds.optional(), locations: propertyAccountingLineitemLocations.optional(), item_variants: propertyAccountingLineitemItemVariants.optional(), fees: propertyAccountingLineitemFees.optional(), contact_id: z.string().optional(), project_id: z.string().optional(), exchange_rate: z.number().optional(), is_billable: z.boolean().optional()}); export const propertyAccountingBillLineitems = z.array(accountingLineitem); export const accountingAttachment = z.object({id: z.string().optional(), download_url: z.string().optional(), name: z.string().optional(), mime_type: z.string().optional()}); export const propertyAccountingBillAttachments = z.array(accountingAttachment); export const accountingPaymentReference = z.object({payment_id: z.string().optional(), amount: z.number().optional(), allocated_at: z.string().optional()}); export const propertyAccountingBillPayments = z.array(accountingPaymentReference); export const propertyAccountingBillCategoryIds = z.array(z.string()); 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 propertyAccountingBillMetadata = z.array(accountingMetadata); export const propertyAccountingBillPurchaseorderIds = z.array(z.string()); export const accountingBill = z.object({id: z.string().optional(), bill_number: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), due_at: z.string().optional(), paid_at: z.string().optional(), refunded_at: z.string().optional(), cancelled_at: z.string().optional(), posted_at: z.string().optional(), total_amount: z.number().optional(), paid_amount: z.number().optional(), refund_amount: z.number().optional(), tax_amount: z.number().optional(), discount_amount: z.number().optional(), balance_amount: z.number().optional(), contact_id: z.string().optional(), currency: z.string().optional(), notes: z.string().optional(), extended_notes: propertyAccountingBillExtendedNotes.optional(), refund_reason: z.string().optional(), lineitems: propertyAccountingBillLineitems.optional(), status: z.enum(["DRAFT", "VOIDED", "AUTHORIZED", "PAID", "PARTIALLY_PAID", "PARTIALLY_REFUNDED", "REFUNDED", "SUBMITTED", "DELETED", "OVERDUE"]).optional(), url: z.string().optional(), payment_collection_method: z.enum(["send_invoice", "charge_automatically"]).optional(), attachments: propertyAccountingBillAttachments.optional(), send: z.boolean().optional(), organization_id: z.string().optional(), term: z.enum(["ON_RECEIPT", "NET_7", "NET_10", "NET_15", "NET_20", "NET_25", "NET_30", "NET_45", "NET_60", "NET_90", "OTHER"]).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(), payments: propertyAccountingBillPayments.optional(), category_ids: propertyAccountingBillCategoryIds.optional(), project_id: z.string().optional(), metadata: propertyAccountingBillMetadata.optional(), sub_total_amount: z.number().optional(), exchange_rate: z.number().optional(), purchaseorder_ids: propertyAccountingBillPurchaseorderIds.optional(), raw: z.record(z.string(), z.unknown()).optional()}); export const accountingBills = z.array(accountingBill); 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 bills * * GET /accounting/{connection_id}/bill */ export const getApiAccountingConnectionIdBill = async (args: GetApiAccountingConnectionIdBillArgs, options?: GetApiAccountingConnectionIdBillOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = 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.offset != null) query.append("offset", String(args.offset)) if (args.updated_gte != null) query.append("updated_gte", String(args.updated_gte)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.query != null) query.append("query", String(args.query)) if (args.contact_id != null) query.append("contact_id", String(args.contact_id)) if (args.org_id != null) query.append("org_id", String(args.org_id)) if (args.start_gte != null) query.append("start_gte", String(args.start_gte)) if (args.end_lt != null) query.append("end_lt", String(args.end_lt)) 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))}/bill${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(accountingBills, await res.json()) };