import {z} from 'zod' export type GetApiV1StocksTickerInstitutionalActivityArgs = {ticker: string, reportDate?: string | undefined, limit?: number | undefined}; export type GetApiV1StocksTickerInstitutionalActivityOptions = {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 continuedFilerDto = z.object({name: z.string().nullable().optional(), cik: z.string().nullable().optional()}); export const holderActivityDto = z.object({name: z.string().nullable().optional(), cik: z.string().nullable().optional(), previousShares: z.number().int().optional(), currentShares: z.number().int().optional(), deltaShares: z.number().int().optional(), deltaValue: z.number().int().optional(), isNewPosition: z.boolean().optional(), isSoldOut: z.boolean().optional(), isFirst13F: z.boolean().nullable().optional(), continuesFrom: z.array(continuedFilerDto).nullable().optional()}); export const holdingsActivityResponse = z.object({listingNotice: listingNotice.optional(), reportDate: z.string().optional(), previousReportDate: z.string().nullable().optional(), isFilingWindowOpen: z.boolean().optional(), buyers: z.array(holderActivityDto).nullable().optional(), sellers: z.array(holderActivityDto).nullable().optional(), limit: z.number().int().nullable().optional(), totalBuyers: z.number().int().nullable().optional(), totalSellers: z.number().int().nullable().optional(), note: z.string().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 }; /** * Top institutional buyers and sellers of an exact stock or ETF listing — biggest absolute share additions * and reductions versus the previous 13F report date, including new and sold-out * positions. Movement is like-for-like: only filers with an observed 13F in both quarters * rank, so a fund that has not filed yet, a first filer or a fund that stopped filing under * its CIK is never read as a trade, and note names the largest filers left out. A filer * that continues under one new CIK is compared through it (continuesFrom); one split across * several new CIKs is left out. Movement needs the immediately preceding quarter on record; * otherwise note says the comparison is unavailable. Share counts are restated onto today's * split basis. * * GET /v1/stocks/{ticker}/institutional-activity */ export const getApiV1StocksTickerInstitutionalActivity = async (args: GetApiV1StocksTickerInstitutionalActivityArgs, options?: GetApiV1StocksTickerInstitutionalActivityOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/${encodeURIComponent(String(args.ticker))}/institutional-activity${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(holdingsActivityResponse, await res.json()) };