import {z} from 'zod' export type GetApiCurrenciesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const networkFeeConfig = z.object({flat: z.string().nullable().optional(), percentage: z.string().nullable().optional(), min_fee: z.string().nullable().optional(), max_fee: z.string().nullable().optional(), fee_currency: z.string().nullable().optional()}); export const networkSummary = z.object({code: z.string().optional(), name: z.string().optional(), is_active: z.boolean().optional(), deposit_enabled: z.boolean().optional(), withdrawal_enabled: z.boolean().optional(), memo_required: z.boolean().optional(), min_amount: z.string().nullable().optional(), max_amount: z.string().nullable().optional(), confirmations: z.number().int().optional(), est_confirmation_time: z.string().nullable().optional(), address_regex: z.string().nullable().optional(), deposit_fee: networkFeeConfig.nullable().optional(), withdrawal_fee: networkFeeConfig.nullable().optional(), icon_uri: z.string().nullable().optional()}); export const currency = z.object({code: z.string(), name: z.string(), capabilities: z.object({trade: z.boolean(), deposit: z.boolean(), withdraw: z.boolean(), easy_buy: z.boolean(), easy_sell: z.boolean(), easy_save: z.boolean()}), easy_buy_sell_quote_currency: z.enum(["ZAR", "USDT"]).optional(), networks: z.array(networkSummary)}); export const getApiCurrenciesResponse = z.array(currency); 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 supported currencies * * GET /currencies */ export const getApiCurrencies = async (options?: GetApiCurrenciesOptions) => { const {baseUrl = "https://api.altcointrader.co.za", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/currencies${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(getApiCurrenciesResponse, await res.json()) };