import {z} from 'zod' export type GetApiV1CongressMarketActivityArgs = {days?: number | undefined, direction?: string | undefined, chamber?: string | undefined, limit?: number | undefined}; export type GetApiV1CongressMarketActivityOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const congressionalActivityDto = z.object({ticker: z.string().nullable().optional(), distinctBuyers: z.number().int().optional(), distinctSellers: z.number().int().optional(), buyTrades: z.number().int().optional(), sellTrades: z.number().int().optional(), estimatedBuyFlowUsd: z.number().optional(), estimatedSellFlowUsd: z.number().optional(), estimatedNetFlowUsd: z.number().optional(), topBuyers: z.array(z.string()).nullable().optional(), topSellers: z.array(z.string()).nullable().optional(), latestFilingDate: z.string().optional(), latestTransactionDate: z.string().optional()}); export const congressionalActivityResponse = z.object({windowDays: z.number().int().optional(), direction: z.string().nullable().optional(), chamber: z.string().nullable().optional(), data: z.array(congressionalActivityDto).nullable().optional(), total: z.number().int().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 congressional trading activity: per-stock aggregates of every trade disclosed in the trailing window across all members and tickers, ranked by estimated net flow. Flows are estimates — each trade counts as the midpoint of its disclosed amount range. The window is anchored on the disclosure (filing) date because STOCK Act filings lag the trade by up to ~45 days. * * GET /v1/congress/market-activity */ export const getApiV1CongressMarketActivity = async (args: GetApiV1CongressMarketActivityArgs, options?: GetApiV1CongressMarketActivityOptions) => { 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.days != null) query.append("days", String(args.days)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.chamber != null) query.append("chamber", String(args.chamber)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/congress/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(congressionalActivityResponse, await res.json()) };