import {z} from 'zod' export type GetApiV1DeltasFundingsArgs = {payorId: string, updatedSince: string, page?: number | undefined, pageSize?: number | undefined}; export type GetApiV1DeltasFundingsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const linkForResponse = z.object({rel: z.string().optional(), href: z.string().optional()}); export const pageForResponse = z.object({numberOfElements: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), page: z.number().int().optional(), pageSize: z.number().int().optional()}); export const fundingPayorStatusAuditResponse = z.object({fundingId: z.string().optional(), amount: z.number().int().optional(), currency: z.string().min(3).max(3).optional(), status: z.string().optional()}); export const pageResourceFundingPayorStatusAuditResponseFundingPayorStatusAuditResponse = z.object({links: z.array(linkForResponse).optional(), page: pageForResponse.optional(), content: z.array(fundingPayorStatusAuditResponse).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 Funding Audit Delta * * GET /v1/deltas/fundings */ export const getApiV1DeltasFundings = async (args: GetApiV1DeltasFundingsArgs, options?: GetApiV1DeltasFundingsOptions) => { const {baseUrl = "https://api.sandbox.velopayments.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.payorId != null) query.append("payorId", String(args.payorId)) if (args.updatedSince != null) query.append("updatedSince", String(args.updatedSince)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/deltas/fundings${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(pageResourceFundingPayorStatusAuditResponseFundingPayorStatusAuditResponse, await res.json()) };