import {z} from 'zod' export type GetApiV1SecuritiesTickerInstitutionalOwnershipArgs = {ticker: string, periods?: number | undefined}; export type GetApiV1SecuritiesTickerInstitutionalOwnershipOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const listingNotice = z.object({status: z.string().nullable().optional(), ticker: z.string().nullable().optional(), delistedOn: z.string().nullable().optional(), successorTicker: z.string().nullable().optional()}); export const ownershipPeriodDto = z.object({reportDate: z.string().optional(), institutions: z.number().int().optional(), totalShares: z.number().int().optional(), totalValue: z.number().int().optional(), changePercent: z.number().nullable().optional(), isCombinedQuarter: z.boolean().optional()}); export const ownershipPeriodDtoListResponse = z.object({listingNotice: listingNotice.optional(), data: z.array(ownershipPeriodDto).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 }; /** * Aggregate reported 13F exposure history for an exact stock or ETF listing across quarterly report dates, * oldest first. The legacy totalShares field sums common-share, put/call * notional-underlying, and any tracked principal-denominated rows, so it is not pure * share ownership. Non-ETF primary stocks use the combined view while the newest quarter's * filing window is open; ETF listings remain exact and as-filed because carry-forward is * filer-wide. Share totals are restated onto today's split basis before the change. * * GET /v1/securities/{ticker}/institutional-ownership */ export const getApiV1SecuritiesTickerInstitutionalOwnership = async (args: GetApiV1SecuritiesTickerInstitutionalOwnershipArgs, options?: GetApiV1SecuritiesTickerInstitutionalOwnershipOptions) => { 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.periods != null) query.append("periods", String(args.periods)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/securities/${encodeURIComponent(String(args.ticker))}/institutional-ownership${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(ownershipPeriodDtoListResponse, await res.json()) };