import {z} from 'zod' export type GetApiAccountingConnectionIdPurchaseorderArgs = {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, 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}; export type GetApiAccountingConnectionIdPurchaseorderOptions = {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 accountingPurchaseorders = z.array(accountingPurchaseorder); 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 purchaseorders * * GET /accounting/{connection_id}/purchaseorder */ export const getApiAccountingConnectionIdPurchaseorder = async (args: GetApiAccountingConnectionIdPurchaseorderArgs, options?: GetApiAccountingConnectionIdPurchaseorderOptions) => { 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)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(accountingPurchaseorders, await res.json()) };