import {z} from 'zod' export type GetApiV1FlowTickerHistoricalCompareArgs = {ticker: string, date?: string | undefined}; export type GetApiV1FlowTickerHistoricalCompareOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const currentMetrics = z.object({totalPremium: z.number(), totalVolume: z.number().int().gte(0), callPremium: z.number(), putPremium: z.number(), netPremium: z.number(), callPutRatio: z.number(), callVolume: z.number().int().gte(0), putVolume: z.number().int().gte(0)}); export const historicalMetrics = z.object({avgPremium: z.number(), avgVolume: z.number(), avgNetPremium: z.number(), avgCallPutRatio: z.number(), avgCallPremium: z.number(), avgPutPremium: z.number(), daysAnalyzed: z.number().int().gte(0)}); export const vsAverage = z.object({premiumVsAvg: z.number(), volumeVsAvg: z.number(), netPremiumVsAvg: z.number()}); export const percentileRankings = z.object({premiumPercentile: z.number().int().gte(0).lte(100), volumePercentile: z.number().int().gte(0).lte(100), netPremiumPercentile: z.number().int().gte(0).lte(100)}); export const similarDay = z.object({date: z.string(), netPremium: z.number(), totalPremium: z.number(), similarityScore: z.number()}); export const historicalCompareResponse = z.object({ticker: z.string(), date: z.string(), current: currentMetrics, historical: historicalMetrics, vsAverage: vsAverage, percentileRankings: percentileRankings, similarDays: z.array(similarDay)}); export const meta = z.object({timestamp: z.string(), requestId: z.string()}); export const historicalCompareSuccess = z.object({data: historicalCompareResponse, 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 }; /** * Today's flow vs trailing average (with similar-days lookback) * * GET /v1/flow/{ticker}/historical-compare */ export const getApiV1FlowTickerHistoricalCompare = async (args: GetApiV1FlowTickerHistoricalCompareArgs, options?: GetApiV1FlowTickerHistoricalCompareOptions) => { 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.date != null) query.append("date", String(args.date)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/flow/${encodeURIComponent(String(args.ticker))}/historical-compare${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(historicalCompareSuccess, await res.json()) };