import {z} from 'zod' export type GetApiApiV1Sec13fConsensusCusipAvgEntryPriceArgs = {cusip: string, ticker: string, from_quarter?: (string | null) | undefined, to_quarter?: (string | null) | undefined}; export type GetApiApiV1Sec13fConsensusCusipAvgEntryPriceOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const sec13fComparison = z.object({from_period_end: z.string().nullable().optional(), to_period_end: z.string().nullable().optional(), quarters_apart: z.number().int().nullable().optional(), calendar_adjacent: z.boolean().nullable().optional(), skipped_quarters: z.array(z.string()).nullable().optional(), note: z.string().nullable().optional()}); export const sec13fQuarterAvgPrice = z.object({stem: z.string(), quarter_start: z.string(), quarter_end: z.string(), avg_close_usd: z.number().nullable().optional(), sample_days: z.number().int().optional(), available: z.boolean()}); export const sec13fAvgEntryPortfolio = z.object({total_acquired_shares: z.number().int(), weighted_avg_entry_price_usd: z.number().nullable().optional(), confidence: z.string()}); export const sec13fAvgEntryFund = z.object({cik: z.string(), manager_name: z.string(), current_shares: z.number().int(), total_acquired_shares: z.number().int(), quarters_acquired: z.number().int(), weighted_avg_entry_price_usd: z.number().nullable().optional(), confidence: z.string()}); export const sec13fAvgEntryPrice = z.object({comparison: sec13fComparison.nullable().optional(), cusip: z.string(), ticker: z.string(), issuer: z.string(), from_quarter: z.string(), to_quarter: z.string(), quarters: z.array(sec13fQuarterAvgPrice), methodology: z.string(), portfolio: sec13fAvgEntryPortfolio, funds: z.array(sec13fAvgEntryFund)}); 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 envelopeSec13fAvgEntryPrice = z.object({data: sec13fAvgEntryPrice, 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 }; /** * Approximate weighted-average entry price for a CUSIP across 13F holders * * GET /api/v1/sec/13f/consensus/{cusip}/avg-entry-price */ export const getApiApiV1Sec13fConsensusCusipAvgEntryPrice = async (args: GetApiApiV1Sec13fConsensusCusipAvgEntryPriceArgs, options?: GetApiApiV1Sec13fConsensusCusipAvgEntryPriceOptions) => { 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.ticker != null) query.append("ticker", String(args.ticker)) if (args.from_quarter != null) query.append("from_quarter", String(args.from_quarter)) if (args.to_quarter != null) query.append("to_quarter", String(args.to_quarter)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sec/13f/consensus/${encodeURIComponent(String(args.cusip))}/avg-entry-price${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(envelopeSec13fAvgEntryPrice, await res.json()) };