import {z} from 'zod' export type GetApiV1FlowMarketBreadthArgs = {date?: string | undefined, fir_threshold?: number | undefined}; export type GetApiV1FlowMarketBreadthOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const indexSentiment = z.object({ticker: z.string(), bullishPremium: z.number(), bearishPremium: z.number(), netPremium: z.number(), fir: z.number(), sentiment: z.enum(["strong_bullish", "bullish", "slightly_bullish", "neutral", "slightly_bearish", "bearish", "strong_bearish"])}); export const aggregateSentiment = z.object({combinedBullish: z.number(), combinedBearish: z.number(), combinedNetPremium: z.number(), combinedFir: z.number(), sentiment: z.enum(["strong_bullish", "bullish", "slightly_bullish", "neutral", "slightly_bearish", "bearish", "strong_bearish"])}); export const advanceDecline = z.object({advancing: z.number().int().gte(0), declining: z.number().int().gte(0), unchanged: z.number().int().gte(0), total: z.number().int().gte(0), ratio: z.number(), breadthPct: z.number(), marketAvgFir: z.number()}); export const sectorRotation = z.object({sector: z.string(), etf: z.string(), fir: z.number(), netPremium: z.number(), signal: z.enum(["strong_inflow", "inflow", "neutral", "outflow", "strong_outflow"])}); export const marketBreadthResponse = z.object({date: z.string(), majorIndices: z.array(indexSentiment), aggregateSentiment: aggregateSentiment, advanceDecline: advanceDecline, sectorRotation: z.array(sectorRotation)}); export const meta = z.object({timestamp: z.string(), requestId: z.string()}); export const marketBreadthSuccess = z.object({data: marketBreadthResponse, 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 }; /** * Market-wide breadth, advance/decline, and sector rotation * * GET /v1/flow/market-breadth */ export const getApiV1FlowMarketBreadth = async (args: GetApiV1FlowMarketBreadthArgs, options?: GetApiV1FlowMarketBreadthOptions) => { 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 (args.fir_threshold != null) query.append("fir_threshold", String(args.fir_threshold)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/flow/market-breadth${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(marketBreadthSuccess, await res.json()) };