import {z} from 'zod' export type GetApiOrdersOpenArgs = {market?: string | undefined}; export type GetApiOrdersOpenOptions = {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 getApiOrdersOpenResponse = 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 open orders * * GET /orders/open */ export const getApiOrdersOpen = async (args: GetApiOrdersOpenArgs, options?: GetApiOrdersOpenOptions) => { 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 (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/open${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(getApiOrdersOpenResponse, await res.json()) };