import {z} from 'zod' export type GetApiApiV4PublicMarketsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, PayloadAuth?: string | undefined, SignatureAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV4PublicMarketsResponse = z.array(z.object({name: z.string(), stock: z.string(), money: z.string(), stockPrec: z.string(), moneyPrec: z.string(), feePrec: z.string(), makerFee: z.string(), takerFee: z.string(), minAmount: z.string(), minTotal: z.string(), maxTotal: z.string(), tradesEnabled: z.boolean(), isCollateral: z.boolean(), type: z.enum(["spot", "futures", "tradfiFutures"]), isTradFiFutures: z.boolean()})); 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 }; /** * Market Info * * GET /api/v4/public/markets */ export const getApiApiV4PublicMarkets = async (options?: GetApiApiV4PublicMarketsOptions) => { const {baseUrl = "https://whitebit.com", fetch: fetchFn = fetch, ApiKeyAuth, PayloadAuth, SignatureAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-TXC-APIKEY", ApiKeyAuth) if (PayloadAuth !== undefined) headers.set("X-TXC-PAYLOAD", PayloadAuth) if (SignatureAuth !== undefined) headers.set("X-TXC-SIGNATURE", SignatureAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/public/markets${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(getApiApiV4PublicMarketsResponse, await res.json()) };