import {z} from 'zod' export type GetApi10InvoicesInvoiceIdPaymentsArgs = {invoiceId: number, pageSize?: number | undefined, pageToken?: string | undefined}; export type GetApi10InvoicesInvoiceIdPaymentsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentRecordPublicAPIResponseEntity = z.object({paymentId: z.number().int().optional(), paymentRecordType: z.enum(["PAID", "WRITE_OFF", "CREDIT_NOTE"]).optional(), currency: z.enum(["AFN", "ALL", "DZD", "ARS", "AMD", "AUD", "AZN", "BHD", "BDT", "BYN", "BZD", "BOB", "BAM", "BWP", "BRL", "GBP", "BND", "BGN", "BIF", "KHR", "CAD", "CVE", "XAF", "CLP", "CNY", "COP", "KMF", "CDF", "CRC", "HRK", "CZK", "DKK", "DJF", "DOP", "EGP", "ERN", "EEK", "ETB", "EUR", "GEL", "GHS", "GTQ", "GNF", "HNL", "HKD", "HUF", "ISK", "INR", "IDR", "IRR", "IQD", "ILS", "JMD", "JPY", "JOD", "KZT", "KES", "KWD", "LVL", "LBP", "LYD", "LTL", "MOP", "MKD", "MGA", "MYR", "MUR", "MXN", "MDL", "MAD", "MZN", "MMK", "NAD", "NPR", "TWD", "NZD", "NIO", "NGN", "NOK", "OMR", "PKR", "PAB", "PYG", "PEN", "PHP", "PLN", "QAR", "RON", "RUB", "RWF", "SAR", "RSD", "SGD", "SOS", "ZAR", "KRW", "LKR", "SDG", "SEK", "CHF", "SYP", "TZS", "THB", "TOP", "TTD", "TND", "TRY", "USD", "UGX", "UAH", "AED", "UYU", "UZS", "VEF", "VND", "XOF", "YER", "ZMK", "ZWL"]).optional(), paymentDate: z.string().optional(), amount: z.number().optional(), notes: z.string().optional()}); export const examplePaginationInvoicePayments = z.object({pageSize: z.number().int().optional(), hasMore: z.boolean().optional(), totalRecordCount: z.number().int().optional(), nextPage: z.string().optional(), nextPageToken: z.string().optional()}); export const exampleAPIResponseInvoicePayments = z.object({data: z.array(paymentRecordPublicAPIResponseEntity).optional(), pagination: examplePaginationInvoicePayments.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 payments by invoice Id * * GET /1.0/invoices/{invoiceId}/payments */ export const getApi10InvoicesInvoiceIdPayments = async (args: GetApi10InvoicesInvoiceIdPaymentsArgs, options?: GetApi10InvoicesInvoiceIdPaymentsOptions) => { const {baseUrl = "https://api.rocketlane.com/api", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/invoices/${encodeURIComponent(String(args.invoiceId))}/payments${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(exampleAPIResponseInvoicePayments, await res.json()) };