import {z} from 'zod' export type GetApiV1DarkPoolTopPrintsTickerArgs = {ticker: string, top_n?: number | undefined, lookback_days?: number | undefined, as_of_date?: string | undefined}; export type GetApiV1DarkPoolTopPrintsTickerOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const darkPoolTopPrint = z.object({timestamp: z.string(), price: z.number(), notional: z.number(), size: z.number().int().gte(0)}); export const meta = z.object({timestamp: z.string(), requestId: z.string()}); export const darkPoolTopPrintsSuccess = z.object({data: z.array(darkPoolTopPrint), 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 }; /** * Largest individual dark-pool prints for a ticker * * GET /v1/dark-pool/top-prints/{ticker} */ export const getApiV1DarkPoolTopPrintsTicker = async (args: GetApiV1DarkPoolTopPrintsTickerArgs, options?: GetApiV1DarkPoolTopPrintsTickerOptions) => { 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.top_n != null) query.append("top_n", String(args.top_n)) if (args.lookback_days != null) query.append("lookback_days", String(args.lookback_days)) if (args.as_of_date != null) query.append("as_of_date", String(args.as_of_date)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dark-pool/top-prints/${encodeURIComponent(String(args.ticker))}${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(darkPoolTopPrintsSuccess, await res.json()) };