import {z} from 'zod' export type GetApiOptionsTechnicalsIndicatorOptionsTickerArgs = {indicator: 'sma' | 'ema' | 'macd' | 'rsi', optionsTicker: string, timestamp?: string | undefined, 'timestamp.gt'?: string | undefined, 'timestamp.gte'?: string | undefined, 'timestamp.lt'?: string | undefined, 'timestamp.lte'?: string | undefined, timespan: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year', adjusted?: boolean | undefined, window?: number | undefined, series_type?: ('open' | 'high' | 'low' | 'close') | undefined, expand_underlying?: boolean | undefined, short_window?: number | undefined, long_window?: number | undefined, signal_window?: number | undefined, order?: ('asc' | 'desc') | undefined, limit?: number | undefined}; export type GetApiOptionsTechnicalsIndicatorOptionsTickerOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOptionsTechnicalsIndicatorOptionsTickerResponse = z.object({"request-id": z.string().optional(), status: z.string().optional(), results: z.object({underlying: z.object({aggregates: z.array(z.object({})).optional(), url: z.string().optional()}).optional(), values: z.array(z.object({timestamp: z.number().int().optional(), value: z.number().optional(), histogram: z.number().optional(), signal: z.number().optional()})).optional()}).optional(), "next-url": z.string().nullable().optional()}); 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 }; /** * Get Options Technical Indicator Data * * GET /options/technicals/{indicator}/{optionsTicker} */ export const getApiOptionsTechnicalsIndicatorOptionsTicker = async (args: GetApiOptionsTechnicalsIndicatorOptionsTickerArgs, options: GetApiOptionsTechnicalsIndicatorOptionsTickerOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.timestamp != null) query.append("timestamp", String(args.timestamp)) if (args['timestamp.gt'] != null) query.append("timestamp.gt", String(args['timestamp.gt'])) if (args['timestamp.gte'] != null) query.append("timestamp.gte", String(args['timestamp.gte'])) if (args['timestamp.lt'] != null) query.append("timestamp.lt", String(args['timestamp.lt'])) if (args['timestamp.lte'] != null) query.append("timestamp.lte", String(args['timestamp.lte'])) if (args.timespan != null) query.append("timespan", String(args.timespan)) if (args.adjusted != null) query.append("adjusted", String(args.adjusted)) if (args.window != null) query.append("window", String(args.window)) if (args.series_type != null) query.append("series_type", String(args.series_type)) if (args.expand_underlying != null) query.append("expand_underlying", String(args.expand_underlying)) if (args.short_window != null) query.append("short_window", String(args.short_window)) if (args.long_window != null) query.append("long_window", String(args.long_window)) if (args.signal_window != null) query.append("signal_window", String(args.signal_window)) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/options/technicals/${encodeURIComponent(String(args.indicator))}/${encodeURIComponent(String(args.optionsTicker))}${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(getApiOptionsTechnicalsIndicatorOptionsTickerResponse, await res.json()) };