import {z} from 'zod' export type GetApiV1InstitutionsCikBeneficialOwnershipArgs = {cik: string, view?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1InstitutionsCikBeneficialOwnershipOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const statedPurposeCategoryDto = z.object({token: z.string().nullable().optional(), name: z.string().nullable().optional(), quote: z.string().nullable().optional()}); export const statedPurposeDto = z.object({state: z.string().nullable().optional(), summary: z.string().nullable().optional(), categories: z.array(statedPurposeCategoryDto).nullable().optional()}); export const institutionBeneficialOwnershipStakeDto = z.object({ticker: z.string().nullable().optional(), currentTicker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), filingType: z.string().nullable().optional(), isAmendment: z.boolean().optional(), eventDate: z.string().optional(), filingDate: z.string().optional(), accessionNumber: z.string().nullable().optional(), rules: z.array(z.string()).nullable().optional(), shares: z.number().int().optional(), filedShares: z.number().int().optional(), percentOfClass: z.number().nullable().optional(), changeShares: z.number().int().nullable().optional(), priorEventDate: z.string().nullable().optional(), splitUnresolved: z.boolean().optional(), hasCoverPage: z.boolean().optional(), status: z.string().nullable().optional(), delistedOn: z.string().nullable().optional(), purpose: statedPurposeDto.optional()}); export const pageMeta = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), count: z.number().int().optional(), hasMore: z.boolean().optional(), total: z.number().int().nullable().optional()}); export const institutionBeneficialOwnershipResponse = z.object({name: z.string().nullable().optional(), cik: z.string().nullable().optional(), view: z.string().nullable().optional(), hiddenCount: z.number().int().optional(), note: z.string().nullable().optional(), data: z.array(institutionBeneficialOwnershipStakeDto).nullable().optional(), meta: pageMeta.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 }; /** * An institution's Schedule 13D and 13G stakes: its latest 13D or 13G on each listed security * it ever disclosed, held stakes first and newest event first, with the event and filing * dates, the shares (restated onto today's split basis) and percent of class, the change since * the filer's previous filing on that security, a 13D's Item 4 purpose where read, and a * status: current; unconfirmed (no filing in 24 months; a 13D or 13G stays in force until * amended, so the stake is kept and never assumed sold); below_threshold (the latest filing * reports under 5%, the amendment that ends the filer's duty to amend, not necessarily the * holding); exited (no shares); or security_retired (the issuer no longer trades in the US). * The default view lists current and unconfirmed stakes; view=all adds the rest. These are * event-dated 5% stakes, never a quarter-end portfolio: the 13F book is under * v1/institutions/{cik}/portfolio. A filer that files only 13D or 13G resolves by CIK like any * other. * * GET /v1/institutions/{cik}/beneficial-ownership */ export const getApiV1InstitutionsCikBeneficialOwnership = async (args: GetApiV1InstitutionsCikBeneficialOwnershipArgs, options?: GetApiV1InstitutionsCikBeneficialOwnershipOptions) => { 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.view != null) query.append("view", String(args.view)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/institutions/${encodeURIComponent(String(args.cik))}/beneficial-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(institutionBeneficialOwnershipResponse, await res.json()) };