import {z} from 'zod' export type GetApiV113fMostHeldArgs = {reportDate?: string | undefined, sort?: string | undefined, limit?: number | undefined}; export type GetApiV113fMostHeldOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mostHeldStockDto = z.object({ticker: z.string().nullable().optional(), company: z.string().nullable().optional(), filerCount: z.number().int().optional(), deltaFilerCount: z.number().int().optional(), totalValue: z.number().int().optional(), deltaValue: z.number().int().optional(), percentOfUniverse: z.number().optional(), convictionHolders: z.number().int().nullable().optional(), convictionShare: z.number().nullable().optional(), crowding: z.number().nullable().optional(), crowdingPercentile: z.number().nullable().optional(), privateFundAdviserOwnership: z.number().nullable().optional()}); export const mostHeldResponse = z.object({reportDate: z.string().optional(), previousReportDate: z.string().optional(), coverageStartDate: z.string().optional(), isWithinCoverage: z.boolean().optional(), comparisonAvailable: z.boolean().optional(), isRankingAvailable: z.boolean().optional(), comparisonUnavailableReason: z.string().nullable().optional(), isFilingWindowOpen: z.boolean().optional(), sort: z.string().nullable().optional(), universeFilerCount: z.number().int().optional(), data: z.array(mostHeldStockDto).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 }; /** * Cross-sectional ranking of stocks by institutional 13F breadth for a quarter — * stocks ranked by number of 13F filers (default), by quarter-over-quarter change in * filer count, by total published position value, by conviction (filers holding the stock * among their ten largest positions) or by crowding (shares held by high-turnover filers and * private-fund advisers as a fraction of shares outstanding), with Δ filers, Δ value and the * stock's share of the 13F filer universe. Conviction and crowding rank the primary listings of * the newest closed quarter measured across every 13F book, carry no deltas, and snap a * requested quarter not measured yet to the newest measured one before it. Values normally use * report-date closing prices, may fall back to filer values, and can be zero when unavailable. * * GET /v1/13f/most-held */ export const getApiV113fMostHeld = async (args: GetApiV113fMostHeldArgs, options?: GetApiV113fMostHeldOptions) => { 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.reportDate != null) query.append("reportDate", String(args.reportDate)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/13f/most-held${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(mostHeldResponse, await res.json()) };