import {z} from 'zod' export type GetApiV1DerivedDealerGexTickerSnapshotArgs = {ticker: string}; export type GetApiV1DerivedDealerGexTickerSnapshotOptions = {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 getApiV1DerivedDealerGexTickerSnapshotResponse = z.object({data: z.object({ticker: z.string(), snapshot_at: z.string(), session_date_et: z.string().nullable(), state: z.string().nullable(), spot_usd: z.number().nullable(), strikes: z.array(z.object({strike_usd: z.number(), net_gex_usd: z.number().nullable(), node_type: z.string().nullable(), rank: z.number().int().gte(1), relative_strength: z.number().nullable()})), summary: z.object({total_gex_usd: z.number().nullable(), king_strike_usd: z.number().nullable(), gamma_flip_usd: z.number().nullable(), call_wall_strike_usd: z.number().nullable(), put_wall_strike_usd: z.number().nullable(), major_positive_strike_usd: z.number().nullable(), major_negative_strike_usd: z.number().nullable()})}), _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 GEX (0DTE) latest read-model snapshot projection * * GET /v1/derived/dealer-gex/{ticker}/snapshot */ export const getApiV1DerivedDealerGexTickerSnapshot = async (args: GetApiV1DerivedDealerGexTickerSnapshotArgs, options?: GetApiV1DerivedDealerGexTickerSnapshotOptions) => { 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/dealer-gex/${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(getApiV1DerivedDealerGexTickerSnapshotResponse, await res.json()) };