import {z} from 'zod' export type GetApiV1DerivedHeatmapTickerSnapshotArgs = {ticker: string}; export type GetApiV1DerivedHeatmapTickerSnapshotOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const responseMeta = z.object({request_id: z.string().nullable(), cache_hit: z.boolean(), data_freshness_seconds: z.number().int().gte(0).nullable(), truncated: z.unknown(), notice: z.string().optional(), rate_limit_remaining: z.number().int().gte(0).optional(), quota_remaining_pct: z.number().gte(0).lte(100).optional(), data_source: z.literal("internal").optional()}); export const getApiV1DerivedHeatmapTickerSnapshotResponse = z.object({data: z.object({ticker: z.string(), generated_at: z.string(), session_date_et: z.string(), spot_usd: z.number().nullable(), expirations: z.array(z.string()), cells: z.array(z.object({strike_usd: z.number(), expiration: z.string(), net_dealer_gex_usd: z.number()}))}), _meta: responseMeta}); 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 }; /** * Dealer Heatmap latest completed 5-minute signed Top-node snapshot * * GET /v1/derived/heatmap/{ticker}/snapshot */ export const getApiV1DerivedHeatmapTickerSnapshot = async (args: GetApiV1DerivedHeatmapTickerSnapshotArgs, options?: GetApiV1DerivedHeatmapTickerSnapshotOptions) => { const {baseUrl = "https://api.yehangshe.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/derived/heatmap/${encodeURIComponent(String(args.ticker))}/snapshot${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(getApiV1DerivedHeatmapTickerSnapshotResponse, await res.json()) };