import {z} from 'zod' export type GetApiV1HistoryArgs = {symbol: string, resolution: '1' | '2' | '3' | '5' | '15' | '30' | '60' | '240' | '480' | 'D' | 'W', from: number, to: number, countback?: number | undefined, extended?: boolean | undefined}; export type GetApiV1HistoryOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const historyOk = z.object({s: z.literal("ok"), t: z.array(z.number().int()), o: z.array(z.number()), h: z.array(z.number()), l: z.array(z.number()), c: z.array(z.number()), v: z.array(z.number()), bv: z.array(z.number()).optional(), sv: z.array(z.number()).optional(), uv: z.array(z.number()).optional()}); export const historyNoData = z.object({s: z.literal("no_data"), nextTime: z.number().int().optional()}); export const getApiV1HistoryResponse = z.union([historyOk, historyNoData]); 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 }; /** * OHLCV price bars for a symbol and resolution * * GET /v1/history */ export const getApiV1History = async (args: GetApiV1HistoryArgs, options?: GetApiV1HistoryOptions) => { const {baseUrl = "https://flow-api.skylit.ai", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.countback != null) query.append("countback", String(args.countback)) if (args.extended != null) query.append("extended", String(args.extended)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(getApiV1HistoryResponse, await res.json()) };