import {z} from 'zod' export type GetApiV1ConsensusHoldingsArgs = {institutions?: string | undefined, reportDate?: string | undefined, minFunds?: number | undefined, limit?: number | undefined}; export type GetApiV1ConsensusHoldingsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const consensusFundDto = z.object({name: z.string().nullable().optional(), cik: z.string().nullable().optional()}); export const consensusHoldingDto = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), heldByFundCount: z.number().int().optional(), totalFundCount: z.number().int().optional(), combinedValue: z.number().int().optional()}); export const consensusHoldingsResponse = z.object({reportDate: z.string().nullable().optional(), funds: z.array(consensusFundDto).nullable().optional(), unresolved: z.array(z.string()).nullable().optional(), data: z.array(consensusHoldingDto).nullable().optional(), total: z.number().int().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 consensus (combined) portfolio of 2-25 institutions for their latest common 13F report date — stocks ranked by how many funds hold them, then by combined published position value. Values normally use report-date closing prices, may fall back to filer values, and can be zero when unavailable. Answers "what do these funds agree on?". * * GET /v1/consensus-holdings */ export const getApiV1ConsensusHoldings = async (args: GetApiV1ConsensusHoldingsArgs, options?: GetApiV1ConsensusHoldingsOptions) => { 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.institutions != null) query.append("institutions", String(args.institutions)) if (args.reportDate != null) query.append("reportDate", String(args.reportDate)) if (args.minFunds != null) query.append("minFunds", String(args.minFunds)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/consensus-holdings${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(consensusHoldingsResponse, await res.json()) };