import {z} from 'zod' export type GetApiApiPublicV1LiveLongShortHistoryArgs = {coin?: string | undefined, hours?: number | undefined}; export type GetApiApiPublicV1LiveLongShortHistoryOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const longShortHistoryEntry = z.object({accountRatio: z.number(), avgLongLeverage: z.number(), avgShortLeverage: z.number(), coin: z.string(), longAccounts: z.number().int(), longVolume: z.number(), netBias: z.number(), shortAccounts: z.number().int(), shortVolume: z.number(), timestamp: z.number().int(), totalUnrealizedPnl: z.number(), volumeRatio: z.number()}); export const getApiPublicV1LiveLongShortHistoryResponse = z.object({$schema: z.string().optional(), coin: z.string(), history: z.array(longShortHistoryEntry).nullable()}); 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 }; /** * Get API public v1 live long short history * * GET /api/public/v1/live/long-short/history */ export const getApiApiPublicV1LiveLongShortHistory = async (args: GetApiApiPublicV1LiveLongShortHistoryArgs, options?: GetApiApiPublicV1LiveLongShortHistoryOptions) => { const {baseUrl = "https://api.coinversa.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.coin != null) query.append("coin", String(args.coin)) if (args.hours != null) query.append("hours", String(args.hours)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/public/v1/live/long-short/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(getApiPublicV1LiveLongShortHistoryResponse, await res.json()) };