import {z} from 'zod' export type GetApiV1UnderlyingTopDailyArgs = {limit?: number | undefined, date?: string | undefined, min_premium?: number | undefined, min_volume?: number | undefined, order_by?: ('premium' | 'volume' | 'net_premium' | 'call_put_ratio') | undefined, order?: ('asc' | 'desc') | undefined}; export type GetApiV1UnderlyingTopDailyOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const topUnderlyingItem = z.object({ticker: z.string(), totalPremium: z.number(), totalVolume: z.number().int().gte(0), callPremium: z.number(), putPremium: z.number(), callVolume: z.number().int().gte(0), putVolume: z.number().int().gte(0), netPremium: z.number(), callPutRatio: z.number()}); export const meta = z.object({timestamp: z.string(), requestId: z.string()}); export const topUnderlyingListSuccess = z.object({data: z.array(topUnderlyingItem), 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 }; /** * Top underlyings by daily flow * * GET /v1/underlying/top/daily */ export const getApiV1UnderlyingTopDaily = async (args: GetApiV1UnderlyingTopDailyArgs, options?: GetApiV1UnderlyingTopDailyOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.date != null) query.append("date", String(args.date)) if (args.min_premium != null) query.append("min_premium", String(args.min_premium)) if (args.min_volume != null) query.append("min_volume", String(args.min_volume)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.order != null) query.append("order", String(args.order)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/underlying/top/daily${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(topUnderlyingListSuccess, await res.json()) };