import {z} from 'zod' export type GetApiV1StocksTickerOptionsExpirationsArgs = {ticker: string, expired?: boolean | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiV1StocksTickerOptionsExpirationsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const optionExpirationDto = z.object({expirationDate: z.string().optional(), contractCount: z.number().int().optional()}); export const optionExpirationsResponse = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), truncated: z.boolean().optional(), expirations: z.array(optionExpirationDto).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 }; /** * The available option expiration dates for a stock, ascending, with the number of listed contracts at each. Use one of these as the `expiration` for the option chain. This endpoint returns dates and counts, not price quotes; an empty successful response means the stock has no listed options, while an unavailable feed returns 503. Pass `expired=true` to list past expirations instead, for use with the session and bars endpoints; `startDate` and `endDate` then bound the expiration dates and default to the last 90 days. * * GET /v1/stocks/{ticker}/options/expirations */ export const getApiV1StocksTickerOptionsExpirations = async (args: GetApiV1StocksTickerOptionsExpirationsArgs, options?: GetApiV1StocksTickerOptionsExpirationsOptions) => { 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.expired != null) query.append("expired", String(args.expired)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/${encodeURIComponent(String(args.ticker))}/options/expirations${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(optionExpirationsResponse, await res.json()) };