import {z} from 'zod' export type GetApiOrdersHistoryArgs = {market?: string | undefined, status?: ('open' | 'partially_filled' | 'filled' | 'cancelled' | 'rejected') | undefined, limit?: number | undefined, page?: number | undefined, start_time?: number | undefined, end_time?: number | undefined}; export type GetApiOrdersHistoryOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, TimestampAuth?: string | undefined, SignatureAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const order = z.object({order_id: z.string().optional(), client_order_id: z.string().optional(), market: z.string().optional(), side: z.enum(["buy", "sell"]).optional(), type: z.enum(["limit", "market", "stop_limit", "stop_market"]).optional(), price: z.string().nullable().optional(), quantity: z.string().optional(), filled: z.string().optional(), remaining: z.string().optional(), status: z.enum(["open", "partially_filled", "filled", "cancelled", "rejected"]).optional(), time_in_force: z.enum(["GTC", "IOC", "FOK"]).optional(), post_only: z.boolean().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional(), cancelled_at: z.number().int().nullable().optional()}); export const getApiOrdersHistoryResponse = z.array(order); 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 order history * * GET /orders/history */ export const getApiOrdersHistory = async (args: GetApiOrdersHistoryArgs, options?: GetApiOrdersHistoryOptions) => { 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.status != null) query.append("status", String(args.status)) 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(/\/+$/, '')}/orders/history${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(getApiOrdersHistoryResponse, await res.json()) };