import {z} from 'zod' export type CurrencyEnum = 'KES' | 'GHS' | 'NGN' | 'UGX' | 'TZS' | 'XAF' | 'XOF'; export type ActionEnum = 'QUOTE' | 'EXCHANGE'; export type ExchangeSer = {currency: CurrencyEnum, amount: unknown, action: ActionEnum}; export type CreateApiApiV1WalletsIdExchangeArgs = {id: string, body: ExchangeSer}; export type CreateApiApiV1WalletsIdExchangeOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyEnum = z.enum(["KES", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const exchangeResponseSer = z.object({rate: z.string(), fxe_amount: z.string(), status: z.string(), currency: currencyEnum, narrative: z.string()}); 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 }; /** * Forex Exchange API * * POST /api/v1/wallets/{id}/exchange/ */ export const createApiApiV1WalletsIdExchange = async (args: CreateApiApiV1WalletsIdExchangeArgs, options?: CreateApiApiV1WalletsIdExchangeOptions) => { const {baseUrl = "https://api.intasend.com", fetch: fetchFn = fetch, IntaSendBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/wallets/${encodeURIComponent(String(args.id))}/exchange/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(exchangeResponseSer, await res.json()) };