import {z} from 'zod' export type PageLimit = number; export type PageCursor = string; export type GetApiInvoicePaymentsArgs = {limit?: PageLimit | undefined, cursor?: PageCursor | undefined, 'updated.gt'?: string | undefined, sort_by?: ('created' | 'updated') | undefined}; export type GetApiInvoicePaymentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const accountCode = z.string(); export const invoicePayment = z.object({id: z.string(), status: z.enum(["SUCCESSFUL", "FAILED", "UNCLEARED"]), invoice_id: z.string(), amount: roundedMonetaryAmount, date: z.string().optional(), account_code: accountCode.optional(), updated_at: z.string()}); export const pageCursor = z.string(); export const pagination = z.object({next_cursor: pageCursor.optional()}); export const getApiInvoicePaymentsResponse = z.object({payments: z.array(invoicePayment), pagination: pagination}); 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 }; /** * Lists all invoice payments * * GET /invoice-payments */ export const getApiInvoicePayments = async (args: GetApiInvoicePaymentsArgs, options?: GetApiInvoicePaymentsOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = 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.cursor != null) query.append("cursor", String(args.cursor)) if (args['updated.gt'] != null) query.append("updated.gt", String(args['updated.gt'])) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoice-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(getApiInvoicePaymentsResponse, await res.json()) };