import {z} from 'zod' export type GetApiApiOfframpOrderIdArgs = {id: string}; export type GetApiApiOfframpOrderIdOptions = {baseUrl?: string | undefined, ClientIdHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const offrampPaymentType = z.enum(["CRYPTO_WALLET", "VIRTUAL_WALLET"]); export const offrampNetwork = z.enum(["AVALANCHE", "POLYGON", "CELO", "ETHEREUM"]); export const offrampAsset = z.enum(["USDC", "USDT", "CUSD", "CKES"]); export const offrampOrderStatus = z.enum(["initiated", "awaiting_transaction_confirmation", "transaction_confirmed", "transaction_failed", "offramp_success", "offramp_pending", "offramp_failed", "offramp_retry", "refunding", "refunded", "refund_failed", "expired", "cancelled", "validating_transaction", "transaction_invalid"]); export const offrampOrder = z.object({_id: z.string().optional(), offerId: z.string().optional(), paymentType: offrampPaymentType.optional(), network: offrampNetwork.optional(), asset: offrampAsset.optional(), exchangeRate: z.number().optional(), cashout: z.object({localCurrencyAmount: z.number().optional(), usdAmount: z.number().optional(), feeAmountUsd: z.number().optional(), feeAmountUsdFonbnk: z.number().optional(), feeAmountUsdPartner: z.number().optional(), feeAmountLocalCurrency: z.number().optional(), feeAmountLocalCurrencyFonbnk: z.number().optional(), feeAmountLocalCurrencyPartner: z.number().optional(), cryptoAmount: z.number().optional(), feeAmountCrypto: z.number().optional(), feeAmountCryptoFonbnk: z.number().optional(), feeAmountCryptoPartner: z.number().optional()}).optional(), fromAddress: z.string().optional(), toAddress: z.string().optional(), status: offrampOrderStatus.optional(), createdAt: z.string().optional(), expiresAt: z.string().optional(), hash: z.string().optional(), statusHistory: z.array(z.object({status: offrampOrderStatus.optional(), changedAt: z.string().optional()})).optional(), requiredFields: z.object({}).optional(), countryIsoCode: z.string().optional(), userPhoneNumber: z.string().optional(), userEmail: z.string().optional(), currencyIsoCode: z.string().optional(), offerRequiredFields: z.array(z.object({label: z.string().optional(), type: z.string().optional(), value: z.string().optional()})).optional(), orderParams: z.string().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 off-ramp order * * GET /api/offramp/order/{id} * * Also accepts auth: TimestampHeader, SignatureHeader */ export const getApiApiOfframpOrderId = async (args: GetApiApiOfframpOrderIdArgs, options?: GetApiApiOfframpOrderIdOptions) => { const {baseUrl = "https://sandbox-api.fonbnk.com", fetch: fetchFn = fetch, ClientIdHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ClientIdHeader !== undefined) headers.set("x-client-id", ClientIdHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/offramp/order/${encodeURIComponent(String(args.id))}${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(offrampOrder, await res.json()) };