import {z} from 'zod' export type GetApiBillingTenantLocatorAccountingFaTransactionsLocatorArgs = {tenantLocator: string, locator: unknown}; export type GetApiBillingTenantLocatorAccountingFaTransactionsLocatorOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const accountLineItem = z.object({accountingType: z.enum(["credit", "debit"]), referenceType: z.enum(["accountCreditBalance", "invoiceCreditBalance", "cash", "creditCash", "charge", "credit", "installmentItem", "invoiceItem", "account", "policy", "accountExpenseBalance"]), referenceLocator: z.string(), amount: z.number()}); export const accountingTransactionResponse = z.object({faTransactionLocator: z.string(), faTransactionTime: z.string(), faTransactionNote: z.string(), currency: z.string(), accountLines: z.array(accountLineItem)}); 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 }; /** * GET /billing/{tenantLocator}/accounting/faTransactions/{locator} */ export const getApiBillingTenantLocatorAccountingFaTransactionsLocator = async (args: GetApiBillingTenantLocatorAccountingFaTransactionsLocatorArgs, options: GetApiBillingTenantLocatorAccountingFaTransactionsLocatorOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/billing/${encodeURIComponent(String(args.tenantLocator))}/accounting/faTransactions/${encodeURIComponent(String(args.locator))}${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(accountingTransactionResponse, await res.json()) };