import {z} from 'zod' export type PropertyAccountingPurchaseorderShippingAddress = {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 PropertyAccountingPurchaseorderBillingAddress = {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 PropertyAccountingLineitemCategoryIds = Array; export type AccountingReference = {id?: string | undefined, name?: string | undefined}; export type PropertyAccountingLineitemLocations = Array; export type PropertyAccountingLineitemItemVariants = Array; export type AccountingFee = {type: 'TAX' | 'DISCOUNT' | 'PROMOTION' | 'SHIPPING' | 'GIFT_WRAP' | 'COD' | 'SURCHARGE' | 'TIP' | 'SERVICE_CHARGE' | 'OTHER', original_type?: string | undefined, amount: number, currency?: string | undefined}; export type PropertyAccountingLineitemFees = Array; export type AccountingLineitem = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, refunded_at?: string | undefined, total_amount?: number | undefined, refund_amount?: number | undefined, discount_amount?: number | undefined, tax_amount?: number | undefined, item_id?: string | undefined, unit_amount?: number | undefined, unit_quantity?: number | undefined, item_sku?: string | undefined, item_name?: string | undefined, item_description?: string | undefined, notes?: string | undefined, taxrate_id?: string | undefined, account_id?: string | undefined, category_ids?: PropertyAccountingLineitemCategoryIds | undefined, locations?: PropertyAccountingLineitemLocations | undefined, item_variants?: PropertyAccountingLineitemItemVariants | undefined, fees?: PropertyAccountingLineitemFees | undefined, contact_id?: string | undefined, project_id?: string | undefined, exchange_rate?: number | undefined, is_billable?: boolean | undefined}; export type PropertyAccountingPurchaseorderLineitems = Array; export type PropertyAccountingPurchaseorderCategoryIds = 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 PropertyAccountingPurchaseorderMetadata = Array; export type AccountingAttachment = {id?: string | undefined, download_url?: string | undefined, name?: string | undefined, mime_type?: string | undefined}; export type PropertyAccountingPurchaseorderAttachments = Array; export type AccountingPurchaseorder = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, posted_at?: string | undefined, contact_id?: string | undefined, account_id?: string | undefined, currency?: string | undefined, total_amount?: number | undefined, shipping_address?: PropertyAccountingPurchaseorderShippingAddress | undefined, billing_address?: PropertyAccountingPurchaseorderBillingAddress | undefined, status?: ('DRAFT' | 'VOIDED' | 'AUTHORIZED' | 'PAID' | 'PARTIALLY_PAID' | 'PARTIALLY_REFUNDED' | 'REFUNDED' | 'SUBMITTED' | 'DELETED' | 'OPEN' | 'COMPLETED' | 'CANCELED') | undefined, lineitems?: PropertyAccountingPurchaseorderLineitems | undefined, organization_id?: string | undefined, category_ids?: PropertyAccountingPurchaseorderCategoryIds | undefined, metadata?: PropertyAccountingPurchaseorderMetadata | undefined, attachments?: PropertyAccountingPurchaseorderAttachments | undefined, raw?: {[key: string]: unknown} | undefined}; export type CreateApiAccountingConnectionIdPurchaseorderArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'posted_at' | 'contact_id' | 'account_id' | 'currency' | 'total_amount' | 'shipping_address' | 'billing_address' | 'status' | 'lineitems' | 'organization_id' | 'category_ids' | 'metadata' | 'attachments' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, body: AccountingPurchaseorder}; export type CreateApiAccountingConnectionIdPurchaseorderOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyAccountingPurchaseorderShippingAddress = 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 propertyAccountingPurchaseorderBillingAddress = 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 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 propertyAccountingPurchaseorderLineitems = z.array(accountingLineitem); export const propertyAccountingPurchaseorderCategoryIds = 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 propertyAccountingPurchaseorderMetadata = z.array(accountingMetadata); 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 propertyAccountingPurchaseorderAttachments = z.array(accountingAttachment); export const accountingPurchaseorder = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), posted_at: z.string().optional(), contact_id: z.string().optional(), account_id: z.string().optional(), currency: z.string().optional(), total_amount: z.number().optional(), shipping_address: propertyAccountingPurchaseorderShippingAddress.optional(), billing_address: propertyAccountingPurchaseorderBillingAddress.optional(), status: z.enum(["DRAFT", "VOIDED", "AUTHORIZED", "PAID", "PARTIALLY_PAID", "PARTIALLY_REFUNDED", "REFUNDED", "SUBMITTED", "DELETED", "OPEN", "COMPLETED", "CANCELED"]).optional(), lineitems: propertyAccountingPurchaseorderLineitems.optional(), organization_id: z.string().optional(), category_ids: propertyAccountingPurchaseorderCategoryIds.optional(), metadata: propertyAccountingPurchaseorderMetadata.optional(), attachments: propertyAccountingPurchaseorderAttachments.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 purchaseorder * * POST /accounting/{connection_id}/purchaseorder */ export const createApiAccountingConnectionIdPurchaseorder = async (args: CreateApiAccountingConnectionIdPurchaseorderArgs, options?: CreateApiAccountingConnectionIdPurchaseorderOptions) => { 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))}/purchaseorder${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(accountingPurchaseorder, await res.json()) };