import {z} from 'zod' export type GetApiV1SecuritiesTickerOptionsSessionArgs = {ticker: string, date?: string | undefined, expiration?: string | undefined, minStrike?: number | undefined, maxStrike?: number | undefined, type?: string | undefined, limit?: number | undefined}; export type GetApiV1SecuritiesTickerOptionsSessionOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const optionSessionRowDto = z.object({occTicker: z.string().nullable().optional(), contractType: z.string().nullable().optional(), strikePrice: z.number().optional(), traded: z.boolean().optional(), open: z.number().nullable().optional(), high: z.number().nullable().optional(), low: z.number().nullable().optional(), close: z.number().nullable().optional(), volume: z.number().int().nullable().optional(), vwap: z.number().nullable().optional(), tradeCount: z.number().int().nullable().optional()}); export const optionSessionResponse = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), expiration: z.string().optional(), session: z.string().optional(), final: z.boolean().optional(), returned: z.number().int().optional(), total: z.number().int().optional(), tradedContracts: z.number().int().optional(), truncated: z.boolean().optional(), contracts: z.array(optionSessionRowDto).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 }; /** * One past session's bars across one expiration, by strike: open, high, low, close, volume, VWAP and trade count for every contract, active or expired, with `traded: false` for a contract that did not trade that session. When the expiration holds more contracts than `limit`, the strikes nearest that session's stock close are returned (`truncated` flags the cut). Narrow with `minStrike`/`maxStrike` and `type` (call/put). History begins in early 2024. Plus can read a session 30 minutes after the stock close; Pro can read the current session, with `final: false` on a read that began before it ended. The Free plan covers end-of-day data only and receives 403. Open interest, greeks and bid/ask have no history here; implied volatility history is `/options/volatility`. US listings only. * * GET /v1/securities/{ticker}/options/session */ export const getApiV1SecuritiesTickerOptionsSession = async (args: GetApiV1SecuritiesTickerOptionsSessionArgs, options?: GetApiV1SecuritiesTickerOptionsSessionOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (args.expiration != null) query.append("expiration", String(args.expiration)) if (args.minStrike != null) query.append("minStrike", String(args.minStrike)) if (args.maxStrike != null) query.append("maxStrike", String(args.maxStrike)) if (args.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/securities/${encodeURIComponent(String(args.ticker))}/options/session${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(optionSessionResponse, await res.json()) };