import {z} from 'zod' export type GetApiOptionsSnapshotChainUnderlyingAssetArgs = {underlyingAsset: string, 'strike-price'?: number | undefined, 'strike_price.gt'?: number | undefined, 'strike_price.gte'?: number | undefined, 'strike_price.lt'?: number | undefined, 'strike_price.lte'?: number | undefined, 'expiration-date'?: string | undefined, 'expiration_date.gt'?: string | undefined, 'expiration_date.gte'?: string | undefined, 'expiration_date.lt'?: string | undefined, 'expiration_date.lte'?: string | undefined, 'contract-type'?: ('call' | 'put') | undefined, order?: ('asc' | 'desc') | undefined, limit?: number | undefined, sort?: string | undefined}; export type GetApiOptionsSnapshotChainUnderlyingAssetOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOptionsSnapshotChainUnderlyingAssetResponse = z.object({"request-id": z.string().optional(), status: z.string().optional(), results: z.array(z.object({"break-even-price": z.number().optional(), day: z.object({}).optional(), details: z.object({}).optional(), greeks: z.object({}).optional(), "implied-volatility": z.number().nullable().optional(), "last-quote": z.object({}).optional(), "last-trade": z.object({}).optional(), "open-interest": z.number().optional(), "underlying-asset": z.object({}).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 Option Chain Snapshot * * GET /options/snapshot/chain/{underlyingAsset} */ export const getApiOptionsSnapshotChainUnderlyingAsset = async (args: GetApiOptionsSnapshotChainUnderlyingAssetArgs, options: GetApiOptionsSnapshotChainUnderlyingAssetOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['strike-price'] != null) query.append("strike-price", String(args['strike-price'])) if (args['strike_price.gt'] != null) query.append("strike_price.gt", String(args['strike_price.gt'])) if (args['strike_price.gte'] != null) query.append("strike_price.gte", String(args['strike_price.gte'])) if (args['strike_price.lt'] != null) query.append("strike_price.lt", String(args['strike_price.lt'])) if (args['strike_price.lte'] != null) query.append("strike_price.lte", String(args['strike_price.lte'])) if (args['expiration-date'] != null) query.append("expiration-date", String(args['expiration-date'])) if (args['expiration_date.gt'] != null) query.append("expiration_date.gt", String(args['expiration_date.gt'])) if (args['expiration_date.gte'] != null) query.append("expiration_date.gte", String(args['expiration_date.gte'])) if (args['expiration_date.lt'] != null) query.append("expiration_date.lt", String(args['expiration_date.lt'])) if (args['expiration_date.lte'] != null) query.append("expiration_date.lte", String(args['expiration_date.lte'])) if (args['contract-type'] != null) query.append("contract-type", String(args['contract-type'])) if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/options/snapshot/chain/${encodeURIComponent(String(args.underlyingAsset))}${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(getApiOptionsSnapshotChainUnderlyingAssetResponse, await res.json()) };