import {z} from 'zod' export type GetApiV113fMarketActivityArgs = {bucket: string, reportDate?: string | undefined, limit?: number | undefined}; export type GetApiV113fMarketActivityOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const marketActivityStockDto = z.object({ticker: z.string().nullable().optional(), company: z.string().nullable().optional(), deltaShares: z.number().int().nullable().optional(), deltaValue: z.number().int().nullable().optional(), filerCount: z.number().int().nullable().optional()}); export const marketActivityResponse = z.object({bucket: z.string().nullable().optional(), reportDate: z.string().optional(), previousReportDate: z.string().optional(), coverageStartDate: z.string().optional(), isWithinCoverage: z.boolean().optional(), comparisonAvailable: z.boolean().optional(), isRankingAvailable: z.boolean().optional(), comparisonUnavailableReason: z.string().nullable().optional(), isFilingWindowOpen: z.boolean().optional(), data: z.array(marketActivityStockDto).nullable().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 }; /** * Market-wide 13F leaderboards for a quarter — which stocks were most bought, most * sold, most initiated or most exited across all 13F filers versus the prior quarter. * Mover deltas are restated onto today's split basis; ordering is by change in published * position value. Values normally use report-date closing prices, may fall back to filer * values, and can be zero when unavailable; the delta therefore includes price movement, * not just net buying or selling. * * GET /v1/13f/market-activity */ export const getApiV113fMarketActivity = async (args: GetApiV113fMarketActivityArgs, options?: GetApiV113fMarketActivityOptions) => { 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.bucket != null) query.append("bucket", String(args.bucket)) if (args.reportDate != null) query.append("reportDate", String(args.reportDate)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/13f/market-activity${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(marketActivityResponse, await res.json()) };