import {z} from 'zod' export type TimeStamp = number; export type GetApiCustomersCustomerIdDebtsDebtIdPaymentsArgs = {startTime?: TimeStamp | undefined, endTime?: TimeStamp | undefined}; export type GetApiCustomersCustomerIdDebtsDebtIdPaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timeStamp = z.number().int(); export const monetaryAmount = z.object({amount: z.number().int().optional(), currency: z.string().optional()}); export const payment = z.object({meta: z.object({id: z.string().optional(), timeCreated: timeStamp.optional()}).optional(), amount: monetaryAmount.optional(), payee: z.enum(["CREDITOR", "TRUEACCORD"]).optional(), transactionReference: z.string().optional(), transactionType: z.enum(["PAYMENT", "RETURNED_PAYMENT", "REFUND"]).optional(), note: z.string().optional(), paymentTimestamp: z.number().int().optional()}); export const getApiCustomersCustomerIdDebtsDebtIdPaymentsResponse = z.object({payments: z.array(payment).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 }; /** * List payments to a debt * * GET /customers/{customerId}/debts/{debtId}/payments/ */ export const getApiCustomersCustomerIdDebtsDebtIdPayments = async (args: GetApiCustomersCustomerIdDebtsDebtIdPaymentsArgs, options?: GetApiCustomersCustomerIdDebtsDebtIdPaymentsOptions) => { const {baseUrl = "https://api.trueaccord.com/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customerId))}/debts/${encodeURIComponent(String(args.debtId))}/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(getApiCustomersCustomerIdDebtsDebtIdPaymentsResponse, await res.json()) };