import {z} from 'zod' export type GetApiInvoicesPaymentsFormDataArgs = {organization_id: string, invoice_payment_id?: (string | null) | undefined, client_id?: number | undefined}; export type GetApiInvoicesPaymentsFormDataOptions = {baseUrl?: string | undefined, passport?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiInvoicesPaymentsFormDataResponse = z.object({message: z.string().optional(), invoice_payment_form_data: z.object({asset_accounts: z.array(z.object({id: z.number().int().optional(), parent_id: z.number().int().nullable().optional(), name: z.string().optional(), account_label: z.string().optional(), currency: z.string().optional(), currency_id: z.number().int().optional(), type_id: z.number().int().optional()})).optional(), trust_accounts: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), account_label: z.string().optional(), currency: z.string().optional(), currency_id: z.number().int().optional(), balance: z.number().optional()})).optional()}).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 }; /** * Get invoice payment form data * * GET /invoices/payments/form-data */ export const getApiInvoicesPaymentsFormData = async (args: GetApiInvoicesPaymentsFormDataArgs, options?: GetApiInvoicesPaymentsFormDataOptions) => { const {baseUrl = "http://localhost/app4legal/api/v2/money", fetch: fetchFn = fetch, passport} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.organization_id != null) query.append("organization_id", String(args.organization_id)) if (args.invoice_payment_id != null) query.append("invoice_payment_id", String(args.invoice_payment_id)) if (args.client_id != null) query.append("client_id", String(args.client_id)) if (passport !== undefined) headers.set('authorization', `Bearer ${passport}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoices/payments/form-data${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(getApiInvoicesPaymentsFormDataResponse, await res.json()) };