import {z} from 'zod' export type GetApiV1DerivedDealerGexTickerHistoryArgs = {ticker: string, from?: (string | string) | undefined, to?: (string | string) | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiV1DerivedDealerGexTickerHistoryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const responseMetaList = 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(), next_cursor: z.string().nullable(), resume_cursor: z.string().nullable().optional()}); export const getApiV1DerivedDealerGexTickerHistoryResponse = z.object({data: z.array(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: responseMetaList}); 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) 5-minute public history (three NYSE sessions, newest first) * * GET /v1/derived/dealer-gex/{ticker}/history */ export const getApiV1DerivedDealerGexTickerHistory = async (args: GetApiV1DerivedDealerGexTickerHistoryArgs, options?: GetApiV1DerivedDealerGexTickerHistoryOptions) => { 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 (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/derived/dealer-gex/${encodeURIComponent(String(args.ticker))}/history${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(getApiV1DerivedDealerGexTickerHistoryResponse, await res.json()) };