import {z} from 'zod' export type GetApiV1InstitutionsCikPortfolioArgs = {cik: string, reportDate?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1InstitutionsCikPortfolioOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const institutionPositionDto = z.object({ticker: z.string().nullable().optional(), company: z.string().nullable().optional(), shares: z.number().int().optional(), value: z.number().int().optional(), listedTicker: z.string().nullable().optional(), positionType: z.string().nullable().optional(), heldQuarters: z.number().int().nullable().optional(), heldSinceCoverageStart: z.boolean().nullable().optional(), estimatedCostPerShare: z.number().nullable().optional(), costStatus: z.string().nullable().optional()}); export const institutionPortfolioMeta = z.object({reportDate: z.string().optional(), 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 institutionPortfolioResponse = z.object({data: z.array(institutionPositionDto).nullable().optional(), meta: institutionPortfolioMeta.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 tracked stock positions reported by an institution for an actual 13F report date, * ranked by stored position value. Share counts are restated onto today's split basis by * each position's own stock's splits. Value normally derives from shares times the report-date * close, falls back to filed value when needed, and can be zero when valuation is unavailable. * * GET /v1/institutions/{cik}/portfolio */ export const getApiV1InstitutionsCikPortfolio = async (args: GetApiV1InstitutionsCikPortfolioArgs, options?: GetApiV1InstitutionsCikPortfolioOptions) => { 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 (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))}/portfolio${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(institutionPortfolioResponse, await res.json()) };