import {z} from 'zod' export type GetApiApiV1RealtimeFxRatesExchangeArgs = {base: 'KES' | 'USD' | 'EUR' | 'GBP' | 'GHS' | 'NGN' | 'UGX' | 'TZS' | 'XAF' | 'XOF', currency: 'KES' | 'USD' | 'EUR' | 'GBP' | 'GHS' | 'NGN' | 'UGX' | 'TZS' | 'XAF' | 'XOF'}; export type GetApiApiV1RealtimeFxRatesExchangeOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const baseEnum = z.enum(["KES", "USD", "EUR", "GBP", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const pairEnum = z.enum(["KES", "USD", "EUR", "GBP", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const realTimeFXRatesSer = z.object({id: z.number().int(), base: baseEnum, pair: pairEnum, rate: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); 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 }; /** * Retrieve Realtime fx rates exchange * * GET /api/v1/realtime-fx-rates/exchange/ */ export const getApiApiV1RealtimeFxRatesExchange = async (args: GetApiApiV1RealtimeFxRatesExchangeArgs, options?: GetApiApiV1RealtimeFxRatesExchangeOptions) => { const {baseUrl = "https://api.intasend.com", fetch: fetchFn = fetch, IntaSendBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.base != null) query.append("base", String(args.base)) if (args.currency != null) query.append("currency", String(args.currency)) if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/realtime-fx-rates/exchange/${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(realTimeFXRatesSer, await res.json()) };