import {z} from 'zod' export type GetApiLedgerCardPackagesOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const feeAmountType = z.enum(["FLAT", "PERCENT"]); export const feeType = z.enum(["RECURRING", "ONE_TIME", "AD_HOC"]); export const recurringFrequency = z.enum(["DAILY", "WEEKLY", "MONTHLY", "YEARLY"]); export const cardUsageFee = z.object({amount: z.number().gte(0), amountType: feeAmountType.optional(), frequency: feeType.optional(), recurrenceCount: z.number().int().gte(0).optional(), recurrenceFrequency: recurringFrequency.optional(), minValue: z.number().gte(0).optional(), maxValue: z.number().gte(0).optional(), fixedValue: z.number().gte(0).optional()}); export const cardFeeSchedule = z.object({packageId: z.string(), creationFee: cardUsageFee, fundingFee: cardUsageFee, withdrawalFee: cardUsageFee, fxTransactionFee: cardUsageFee, transactionFee: cardUsageFee, maintenanceFee: cardUsageFee, minFundingAmount: z.number().gte(0), minTopUpAmount: z.number().gte(0), maxTopUpAmount: z.number().gte(0), minimumBalance: z.number().gte(0)}); export const cardLifetimeLimit = z.object({limit: z.number().int().gte(1), currentCount: z.number().int().gte(0), remaining: z.number().int().gte(0)}); export const cardPackage = z.object({id: z.string(), currency: z.string(), fees: cardFeeSchedule, lifetimeLimit: cardLifetimeLimit}); export const cardPackageList = z.object({packages: z.array(cardPackage)}); 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 available card packages. * * GET /ledger/card-packages */ export const getApiLedgerCardPackages = async (options?: GetApiLedgerCardPackagesOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/card-packages${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(cardPackageList, await res.json()) };