import {z} from 'zod' export type GetApiWalletWithdrawalsArgs = {currency?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiWalletWithdrawalsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, TimestampAuth?: string | undefined, SignatureAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const withdrawal = z.object({id: z.string().optional(), currency: z.string().optional(), amount: z.string().optional(), fee_amount: z.string().optional(), amount_after_fees: z.string().optional(), status: z.string().optional(), channel_type: z.enum(["crypto", "fiat"]).optional(), network_code: z.string().nullable().optional(), address: z.string().nullable().optional(), address_tag: z.string().nullable().optional(), tx_hash: z.string().nullable().optional(), requested_at: z.number().int().nullable().optional(), completed_at: z.number().int().nullable().optional(), created_at: z.number().int().optional()}); export const cursorMeta = z.object({next_cursor: z.string().nullable().optional(), has_more: z.boolean().optional(), total: z.number().int().optional()}); export const withdrawalListResponse = z.object({data: z.array(withdrawal).optional(), meta: cursorMeta.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 }; /** * List withdrawals * * GET /wallet/withdrawals */ export const getApiWalletWithdrawals = async (args: GetApiWalletWithdrawalsArgs, options?: GetApiWalletWithdrawalsOptions) => { const {baseUrl = "https://api.altcointrader.co.za", fetch: fetchFn = fetch, ApiKeyAuth, TimestampAuth, SignatureAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.currency != null) query.append("currency", String(args.currency)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) if (TimestampAuth !== undefined) headers.set("X-TIMESTAMP", TimestampAuth) if (SignatureAuth !== undefined) headers.set("X-SIGNATURE", SignatureAuth) const url = `${baseUrl.replace(/\/+$/, '')}/wallet/withdrawals${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(withdrawalListResponse, await res.json()) };