import {z} from 'zod' export type GetApiApiInstitutionTickerOwnershipArgs = {ticker: string, date?: string | undefined, limit?: number | undefined, order?: ('name' | 'short_name' | 'first_buy' | 'units' | 'units_change' | 'units_changed' | 'value' | 'avg_price' | 'perc_outstanding' | 'perc_units_changed' | 'activity' | 'perc_inst_value' | 'perc_share_value') | undefined, order_direction?: ('desc' | 'asc') | undefined, page?: number | undefined, tags?: Array | undefined}; export type GetApiApiInstitutionTickerOwnershipOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const institutionOwnership = z.object({avg_price: z.string().optional(), filing_date: z.string().optional(), first_buy: z.string().optional(), historical_units: z.array(z.number().int()).optional(), inst_share_value: z.string().optional(), inst_value: z.string().optional(), name: z.string().optional(), people: z.array(z.string()).optional(), report_date: z.string().optional(), shares_outstanding: z.string().optional(), short_name: z.string().optional(), tags: z.array(z.string()).optional(), units: z.number().int().optional(), units_change: z.number().int().optional(), value: z.number().int().optional()}); export const institutionOwnershipResponse = z.object({data: z.array(institutionOwnership).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 }; /** * Retrieve Institutional Ownership * * GET /api/institution/{ticker}/ownership */ export const getApiApiInstitutionTickerOwnership = async (args: GetApiApiInstitutionTickerOwnershipArgs, options?: GetApiApiInstitutionTickerOwnershipOptions) => { const {baseUrl = "https://api.unusualwhales.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.order_direction != null) query.append("order_direction", String(args.order_direction)) if (args.page != null) query.append("page", String(args.page)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/institution/${encodeURIComponent(String(args.ticker))}/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(institutionOwnershipResponse, await res.json()) };