import {z} from 'zod' export type GetApiApiV1OptionsSymbolSnapshotHistoryArgs = {symbol: string, from_date: string, to_date: string, expiry?: (string | null) | undefined, fields?: (string | null) | undefined}; export type GetApiApiV1OptionsSymbolSnapshotHistoryOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const optionsIssuerAttribution = z.object({attribution: z.string(), current_name: z.string().nullable().optional(), current_company_id: z.string().nullable().optional(), reassigned_on: z.string().nullable().optional(), note: z.string().nullable().optional()}); export const etfSnapshotHistoryEntry = z.object({snapshot_date: z.string(), fields: z.record(z.string(), z.unknown())}); export const optionsSnapshotHistoryPayload = z.object({symbol: z.string(), issuer: optionsIssuerAttribution.nullable().optional(), from_date: z.string(), to_date: z.string(), fields: z.array(z.string()), expiry_filter: z.string().nullable().optional(), snapshots: z.array(etfSnapshotHistoryEntry.nullable())}); export const sugraMeta = z.object({endpoint: z.string(), data_time: z.string(), response_time: z.string(), provider: z.string(), data_age_days: z.number().nullable().optional(), source: z.string().nullable().optional(), attribution: z.string().nullable().optional(), source_url: z.string().nullable().optional(), citation: z.string().nullable().optional(), fallback_used: z.boolean().nullable().optional(), fallback_chain: z.array(z.string()).nullable().optional(), cached: z.boolean().nullable().optional(), stale: z.boolean().nullable().optional(), stale_since: z.string().nullable().optional(), period: z.string().nullable().optional(), notes: z.string().nullable().optional()}); export const envelopeOptionsSnapshotHistoryPayload = z.object({data: optionsSnapshotHistoryPayload, meta: sugraMeta}); 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 }; /** * Sparse snapshot time-series for one underlier * * GET /api/v1/options/{symbol}/snapshot/history */ export const getApiApiV1OptionsSymbolSnapshotHistory = async (args: GetApiApiV1OptionsSymbolSnapshotHistoryArgs, options?: GetApiApiV1OptionsSymbolSnapshotHistoryOptions) => { const {baseUrl = "https://sugra.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.expiry != null) query.append("expiry", String(args.expiry)) if (args.fields != null) query.append("fields", String(args.fields)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/options/${encodeURIComponent(String(args.symbol))}/snapshot/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(envelopeOptionsSnapshotHistoryPayload, await res.json()) };