import {z} from 'zod' export type GetApiPaymentsArgs = {organizationId: string, cardAccountId?: string | undefined, type?: ('BILL' | 'TOPUP' | 'DIRECT_DEBIT_REVERSAL' | 'PAYOUT' | 'PAY_IN') | undefined, status?: ('PLANNED' | 'UPCOMING' | 'DEFERRED' | 'READY_FOR_PMT' | 'PENDING' | 'PAID' | 'FAILED' | 'DISCARDED' | 'BOOKED' | 'REVERSED') | undefined, fromDate?: (string | null) | undefined, toDate?: (string | null) | undefined, byDateField?: ('cutoffDate' | 'collectionDate' | 'paymentDate' | 'updatedAt' | 'createdAt' | null) | undefined, limit?: number | undefined, page?: number | undefined, sortBy?: ('organizationId' | 'status' | 'type' | 'cutoffDate' | 'collectionDate' | 'paymentDate' | 'createdAt') | undefined, sortDirection?: ('ASC' | 'DESC') | undefined}; export type GetApiPaymentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPaymentsResponse = z.object({data: z.array(z.object({paymentId: z.string().optional(), organizationId: z.string().optional(), cardAccountId: z.string().optional(), publicPaymentId: z.string().nullable().optional(), type: z.enum(["BILL", "TOPUP", "DIRECT_DEBIT_REVERSAL", "BILL_PAYMENT_REVERSAL", "PAYOUT", "INTERNAL_TRANSFER", "INTERNAL_TRANSFER_REVERSAL", "PAY_IN", "OTHER"]).optional(), internalTransferId: z.string().nullable().optional(), method: z.enum(["DIRECT_DEBIT_CORE", "DIRECT_DEBIT_B2B", "WIRE_TRANSFER"]).optional(), amount: z.object({value: z.number().optional(), currency: z.string().optional()}).optional(), status: z.enum(["PLANNED", "UPCOMING", "DEFERRED", "READY_FOR_PMT", "PENDING", "PAID", "FAILED", "DISCARDED", "BOOKED", "REVERSED"]).optional()})).optional(), hasNextPage: z.boolean().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 of Payments * * GET /payments */ export const getApiPayments = async (args: GetApiPaymentsArgs, options?: GetApiPaymentsOptions) => { const {baseUrl = "https://partner-api.getpliant.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.organizationId != null) query.append("organizationId", String(args.organizationId)) if (args.cardAccountId != null) query.append("cardAccountId", String(args.cardAccountId)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.byDateField != null) query.append("byDateField", String(args.byDateField)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiPaymentsResponse, await res.json()) };