import {z} from 'zod' export type GetApiV1HistoricalArgs = {symbols: string, at: string, metric?: ('gamma' | 'vanna') | undefined, maxStrikes?: number | undefined}; export type GetApiV1HistoricalOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const strikeNode = z.object({strike: z.number(), value: z.number(), nodeType: z.enum(["king", "gatekeeper", "pika", "barney", "significant", "normal"]), velocityPct: z.number().optional()}); export const symbolHeatmap = z.object({symbol: z.string(), asOf: z.string(), spot: z.number(), previousClose: z.number(), priceChange: z.number(), priceChangePercent: z.number(), expirations: z.array(z.string()), strikes: z.array(strikeNode)}); export const meta = z.object({timestamp: z.string(), requestId: z.string()}); export const getApiV1HistoricalResponse = z.object({data: z.object({symbols: z.array(symbolHeatmap)}), meta: meta}); 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 }; /** * Replay per-strike heatmap at a past instant (one or more symbols) * * GET /v1/historical */ export const getApiV1Historical = async (args: GetApiV1HistoricalArgs, options?: GetApiV1HistoricalOptions) => { 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.symbols != null) query.append("symbols", String(args.symbols)) if (args.at != null) query.append("at", String(args.at)) if (args.metric != null) query.append("metric", String(args.metric)) if (args.maxStrikes != null) query.append("maxStrikes", String(args.maxStrikes)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/historical${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(getApiV1HistoricalResponse, await res.json()) };