import {z} from 'zod' export type GetApiV1PaymentsArgs = {searchClientId?: string | undefined, searchRecipient?: string | undefined, searchStatus?: string | undefined, searchClientAccess?: string | undefined, sortBy?: string | undefined, sortDirection?: string | undefined, pageIndex?: number | undefined, pageSize?: number | undefined}; export type GetApiV1PaymentsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentRequestAmount = z.object({currency: z.string(), value: z.number()}); export const paymentAccountType = z.enum(["IBAN", "BBAN", "BG", "PG"]); export const paymentAccount = z.object({accountNumber: z.string(), accountType: paymentAccountType, bic: z.string().nullable().optional(), currencyCode: z.string().nullable().optional()}); export const paymentCreditor = z.object({account: paymentAccount, message: z.string().nullable().optional(), name: z.string()}); export const paymentDebtor = z.object({account: paymentAccount, message: z.string().nullable().optional(), name: z.string().nullable().optional()}); export const paymentRequestRemittanceInformationType = z.enum(["UNSTRUCTURED", "OCR"]); export const paymentRequestRemittanceInformation = z.object({value: z.string(), type: paymentRequestRemittanceInformationType}); export const paymentRequestModel = z.object({id: z.string(), clientId: z.string().nullable().optional(), ClientAccess: z.string().nullable().optional(), status: z.string().nullable().optional(), amount: paymentRequestAmount, creditor: paymentCreditor, debtor: paymentDebtor.optional(), executionDate: z.number().int().nullable().optional(), remittanceInformation: paymentRequestRemittanceInformation, region: z.string(), paymentProductId: z.string(), paymentTemplateId: z.string().nullable().optional()}); export const pagedPaymentRequestModels = z.object({items: z.array(paymentRequestModel).nullable().optional(), totalItemsCount: z.number().int().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 all Payment Requests for selected organizationId. * * GET /v1/payments */ export const getApiV1Payments = async (args: GetApiV1PaymentsArgs, options: GetApiV1PaymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.searchClientId != null) query.append("searchClientId", String(args.searchClientId)) if (args.searchRecipient != null) query.append("searchRecipient", String(args.searchRecipient)) if (args.searchStatus != null) query.append("searchStatus", String(args.searchStatus)) if (args.searchClientAccess != null) query.append("searchClientAccess", String(args.searchClientAccess)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (args.pageIndex != null) query.append("pageIndex", String(args.pageIndex)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(pagedPaymentRequestModels, await res.json()) };