import {z} from 'zod' export type GetApiDisbursementRulesCurrentUsagesArgs = {linked_to: string, 'Finix-Version'?: string | undefined, after_cursor?: string | undefined, before_cursor?: string | undefined, limit?: number | undefined, 'updated_at.gte'?: string | undefined, 'updated_at.lte'?: string | undefined}; export type GetApiDisbursementRulesCurrentUsagesOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const page = z.object({limit: z.number().int().optional(), next_cursor: z.string().nullable().optional()}); export const velocity = z.object({current_volume: z.number().int().optional(), current_count: z.number().int().optional(), volume_limit: z.number().int().optional(), count_limit: z.number().int().optional(), window_reset: z.string().optional()}); export const currency = z.enum(["CAD", "USD"]); export const self = z.object({href: z.string().min(1).optional()}); export const currentUsage = z.object({ach_velocity: z.object({daily_velocity: velocity.optional(), monthly_velocity: velocity.optional()}).optional(), card_velocity: z.object({daily_velocity: velocity.optional(), monthly_velocity: velocity.optional()}).optional(), balance_adjustment_velocity: z.object({daily_velocity: velocity.optional(), monthly_velocity: velocity.optional()}).optional(), currency: currency.optional(), linked_to: z.string().optional(), linked_type: z.enum(["APPLICATION", "PLATFORM"]).optional(), type: z.literal("TOTAL").optional(), _links: z.object({self: self.optional()}).optional()}); export const next = z.object({href: z.string().optional()}); export const getApiDisbursementRulesCurrentUsagesResponse = z.object({page: page.optional(), _embedded: z.object({current_usages: z.array(currentUsage).optional()}).optional(), _links: z.object({self: self.optional(), next: next.optional()}).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 Current Usages * * GET /disbursement_rules/current_usages */ export const getApiDisbursementRulesCurrentUsages = async (args: GetApiDisbursementRulesCurrentUsagesArgs, options?: GetApiDisbursementRulesCurrentUsagesOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.linked_to != null) query.append("linked_to", String(args.linked_to)) if (args['Finix-Version'] != null) headers.set("Finix-Version", String(args['Finix-Version'])) if (args.after_cursor != null) query.append("after_cursor", String(args.after_cursor)) if (args.before_cursor != null) query.append("before_cursor", String(args.before_cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['updated_at.gte'] != null) query.append("updated_at.gte", String(args['updated_at.gte'])) if (args['updated_at.lte'] != null) query.append("updated_at.lte", String(args['updated_at.lte'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/disbursement_rules/current_usages${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(getApiDisbursementRulesCurrentUsagesResponse, await res.json()) };