import {z} from 'zod' export type GetApiApiInstitutionNameHoldingsArgs = {name: string, date?: string | undefined, limit?: number | undefined, order?: ('date' | 'ticker' | 'security_type' | 'put_call' | 'first_buy' | 'price_first_buy' | 'units' | 'units_change' | 'historical_units' | 'value' | 'avg_price' | 'close' | 'shares_outstanding') | undefined, order_direction?: ('desc' | 'asc') | undefined, page?: number | undefined, security_types?: Array | undefined}; export type GetApiApiInstitutionNameHoldingsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const institutionHolding = z.object({avg_price: z.string().optional(), close: z.string().optional(), date: z.string().optional(), first_buy: z.string().optional(), full_name: z.string().optional(), historical_units: z.array(z.number().int()).optional(), price_first_buy: z.string().optional(), put_call: z.string().nullable().optional(), sector: z.string().optional(), security_type: z.string().optional(), shares_outstanding: z.string().optional(), ticker: z.string().optional(), units: z.number().int().optional(), units_change: z.number().int().optional(), value: z.number().int().optional()}); export const institutionHoldingsResponse = z.object({data: z.array(institutionHolding).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 Holdings * * GET /api/institution/{name}/holdings */ export const getApiApiInstitutionNameHoldings = async (args: GetApiApiInstitutionNameHoldingsArgs, options?: GetApiApiInstitutionNameHoldingsOptions) => { 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.security_types ?? []) query.append("security_types", String(item)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/institution/${encodeURIComponent(String(args.name))}/holdings${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(institutionHoldingsResponse, await res.json()) };