import {z} from 'zod' export type GetApiV1InstitutionsArgs = {query?: string | undefined, limit?: number | undefined, holderTypes?: string | undefined}; export type GetApiV1InstitutionsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const institutionDto = z.object({name: z.string().nullable().optional(), cik: z.string().nullable().optional(), latestReportDate: z.string().nullable().optional(), reportedAum: z.number().int().nullable().optional(), positionCount: z.number().int().nullable().optional(), city: z.string().nullable().optional(), stateOrCountry: z.string().nullable().optional(), holderType: z.string().nullable().optional()}); export const institutionSearchMeta = z.object({limit: z.number().int().optional(), count: z.number().int().optional(), totalMatches: z.number().int().optional(), hasMore: z.boolean().optional()}); export const institutionSearchResponse = z.object({data: z.array(institutionDto).nullable().optional(), meta: institutionSearchMeta.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 }; /** * Search the tracked 13F filer set by name or SEC CIK. Every punctuation-independent * query word is required first; the search broadens to any word only when no strict * row matches. CIK-prefix matches and verified flagship aliases are included. * Results rank recently-active, larger filers first and include their latest 13F * report date, published tracked portfolio value and position count so namesakes can be * distinguished. The legacy reportedAum field normally uses report-date closing prices, * may fall back to filer values, and can include zero for unavailable valuations. * The returned CIK is the identifier every v1/institutions/{cik}/* endpoint accepts; * meta reports the total tracked match count so truncation is visible. Each filer carries * its entity type from regulatory records, never from its name. * * GET /v1/institutions */ export const getApiV1Institutions = async (args: GetApiV1InstitutionsArgs, options?: GetApiV1InstitutionsOptions) => { 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.query != null) query.append("query", String(args.query)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.holderTypes != null) query.append("holderTypes", String(args.holderTypes)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/institutions${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(institutionSearchResponse, await res.json()) };