import {z} from 'zod' export type GetApiApiInstitutionsArgs = {limit?: number | undefined, max_share_value?: string | undefined, max_total_value?: string | undefined, min_share_value?: string | undefined, min_total_value?: string | undefined, name?: string | undefined, order?: ('name' | 'call_value' | 'put_value' | 'share_value' | 'call_holdings' | 'put_holdings' | 'share_holdings' | 'total_value' | 'warrant_value' | 'fund_value' | 'pfd_value' | 'debt_value' | 'total_holdings' | 'warrant_holdings' | 'fund_holdings' | 'pfd_holdings' | 'debt_holdings' | 'percent_of_total' | 'date' | 'buy_value' | 'sell_value') | undefined, order_direction?: ('desc' | 'asc') | undefined, page?: number | undefined, tags?: Array | undefined}; export type GetApiApiInstitutionsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const institution = z.object({buy_value: z.string().optional(), call_holdings: z.string().optional(), call_value: z.string().optional(), cik: z.string().optional(), date: z.string().optional(), debt_holdings: z.string().optional(), debt_value: z.string().optional(), filing_date: z.string().optional(), fund_holdings: z.string().optional(), fund_value: z.string().optional(), is_hedge_fund: z.boolean().optional(), name: z.string().optional(), people: z.array(z.string()).optional(), pfd_holdings: z.string().optional(), pfd_value: z.string().optional(), put_holdings: z.string().optional(), put_value: z.string().optional(), sell_value: z.string().optional(), share_value: z.string().optional(), short_name: z.string().optional(), tags: z.array(z.string()).optional(), total_value: z.string().optional(), warrant_holdings: z.string().optional(), warrant_value: z.string().optional()}); export const institutionsResponse = z.object({data: z.array(institution).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 List of Institutions * * GET /api/institutions */ export const getApiApiInstitutions = async (args: GetApiApiInstitutionsArgs, options?: GetApiApiInstitutionsOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.max_share_value != null) query.append("max_share_value", String(args.max_share_value)) if (args.max_total_value != null) query.append("max_total_value", String(args.max_total_value)) if (args.min_share_value != null) query.append("min_share_value", String(args.min_share_value)) if (args.min_total_value != null) query.append("min_total_value", String(args.min_total_value)) if (args.name != null) query.append("name", String(args.name)) 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/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(institutionsResponse, await res.json()) };