import {z} from 'zod' export type GetApiTradesArgs = {market?: string | undefined, limit?: number | undefined, page?: number | undefined, start_time?: number | undefined, end_time?: number | undefined}; export type GetApiTradesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, TimestampAuth?: string | undefined, SignatureAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const userTrade = z.object({trade_id: z.string().optional(), order_id: z.string().optional(), client_order_id: z.string().optional(), market: z.string().optional(), side: z.enum(["buy", "sell"]).optional(), price: z.string().optional(), quantity: z.string().optional(), total_value: z.string().optional(), fee: z.string().optional(), timestamp: z.number().int().optional()}); export const getApiTradesResponse = z.array(userTrade); 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 trade history * * GET /trades */ export const getApiTrades = async (args: GetApiTradesArgs, options?: GetApiTradesOptions) => { 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.market != null) query.append("market", String(args.market)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) 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(/\/+$/, '')}/trades${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(getApiTradesResponse, await res.json()) };