import {z} from 'zod' export type GetApiV1FlowTickerStrikesArgs = {ticker: string, start_time: string, end_time: string, top_n?: number | undefined, right?: ('call' | 'put') | undefined, min_premium?: number | undefined, order_by?: ('net_premium' | 'total_premium' | 'volume') | undefined, min_dte?: number | undefined, max_dte?: number | undefined}; export type GetApiV1FlowTickerStrikesOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const strikeBreakdown = z.object({strike: z.number(), right: z.enum(["C", "P"]), dominantExpiration: z.string(), tradeCount: z.number().int().gte(0), volume: z.number().int().gte(0), totalPremium: z.number(), bullishPremium: z.number(), bearishPremium: z.number(), netPremium: z.number(), askPct: z.number(), bidPct: z.number(), openInterest: z.number().int().gte(0), volOiRatio: z.number()}); export const strikesConcentration = z.object({top3StrikesShare: z.number(), top3StrikesNetPremium: z.number(), interpretation: z.string()}); export const strikesResponse = z.object({ticker: z.string(), startTime: z.string(), endTime: z.string(), byStrike: z.array(strikeBreakdown), concentration: strikesConcentration}); export const meta = z.object({timestamp: z.string(), requestId: z.string()}); export const strikesSuccess = z.object({data: strikesResponse, 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 }; /** * Strike-level flow concentration * * GET /v1/flow/{ticker}/strikes */ export const getApiV1FlowTickerStrikes = async (args: GetApiV1FlowTickerStrikesArgs, options?: GetApiV1FlowTickerStrikesOptions) => { 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.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.top_n != null) query.append("top_n", String(args.top_n)) if (args.right != null) query.append("right", String(args.right)) if (args.min_premium != null) query.append("min_premium", String(args.min_premium)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.min_dte != null) query.append("min_dte", String(args.min_dte)) if (args.max_dte != null) query.append("max_dte", String(args.max_dte)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/flow/${encodeURIComponent(String(args.ticker))}/strikes${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(strikesSuccess, await res.json()) };