import {z} from 'zod' export type AccountingAccountTaxonomy = {type: 'CLASSIFICATION' | 'GROUP' | 'SUBGROUP' | 'SYSTEM_ROLE' | 'OTHER', original_type?: string | undefined, value: string}; export type PropertyAccountingAccountTaxonomy = 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 PropertyAccountingAccountMetadata = Array; export type AccountingAccount = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, name?: string | undefined, description?: string | undefined, type?: ('ACCOUNTS_PAYABLE' | 'ACCOUNTS_RECEIVABLE' | 'BANK' | 'CREDIT_CARD' | 'FIXED_ASSET' | 'LIABILITY' | 'EQUITY' | 'EXPENSE' | 'REVENUE' | 'OTHER') | undefined, status?: ('ACTIVE' | 'ARCHIVED') | undefined, balance?: number | undefined, currency?: string | undefined, customer_defined_code?: string | undefined, is_payable?: boolean | undefined, parent_id?: string | undefined, taxonomy?: PropertyAccountingAccountTaxonomy | undefined, organization_id?: string | undefined, metadata?: PropertyAccountingAccountMetadata | undefined, raw?: {[key: string]: unknown} | undefined}; export type UpdateApiAccountingConnectionIdAccountIdArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'name' | 'description' | 'type' | 'status' | 'balance' | 'currency' | 'customer_defined_code' | 'is_payable' | 'parent_id' | 'taxonomy' | 'organization_id' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, id: string, body: AccountingAccount}; export type UpdateApiAccountingConnectionIdAccountIdOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const accountingAccountTaxonomy = z.object({type: z.enum(["CLASSIFICATION", "GROUP", "SUBGROUP", "SYSTEM_ROLE", "OTHER"]), original_type: z.string().optional(), value: z.string()}); export const propertyAccountingAccountTaxonomy = z.array(accountingAccountTaxonomy); 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 propertyAccountingAccountMetadata = z.array(accountingMetadata); export const accountingAccount = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), description: z.string().optional(), type: z.enum(["ACCOUNTS_PAYABLE", "ACCOUNTS_RECEIVABLE", "BANK", "CREDIT_CARD", "FIXED_ASSET", "LIABILITY", "EQUITY", "EXPENSE", "REVENUE", "OTHER"]).optional(), status: z.enum(["ACTIVE", "ARCHIVED"]).optional(), balance: z.number().optional(), currency: z.string().optional(), customer_defined_code: z.string().optional(), is_payable: z.boolean().optional(), parent_id: z.string().optional(), taxonomy: propertyAccountingAccountTaxonomy.optional(), organization_id: z.string().optional(), metadata: propertyAccountingAccountMetadata.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 }; /** * Update an account * * PUT /accounting/{connection_id}/account/{id} */ export const updateApiAccountingConnectionIdAccountId = async (args: UpdateApiAccountingConnectionIdAccountIdArgs, options?: UpdateApiAccountingConnectionIdAccountIdOptions) => { 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))}/account/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(accountingAccount, await res.json()) };