import {z} from 'zod' export type GetApiApiCongressCongressTraderArgs = {date?: string | undefined, limit?: number | undefined, name?: string | undefined}; export type GetApiApiCongressCongressTraderOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const congressTraderTransaction = z.object({amounts: z.string().optional(), filed_at_date: z.string().optional(), issuer: z.string().optional(), member_type: z.enum(["senate", "house", "other"]).optional(), notes: z.string().optional(), reporter: z.string().optional(), ticker: z.string().optional(), transaction_date: z.string().optional(), txn_type: z.enum(["Buy", "Sell (partial)", "Purchase", "Sale (Partial)", "Receive", "Sale (Full)", "Sell (PARTIAL)", "Sell", "Exchange"]).optional()}); export const congressTraderResponse = z.object({data: z.array(congressTraderTransaction).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 }; /** * Get Trades by Congress Member * * GET /api/congress/congress-trader */ export const getApiApiCongressCongressTrader = async (args: GetApiApiCongressCongressTraderArgs, options?: GetApiApiCongressCongressTraderOptions) => { 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.name != null) query.append("name", String(args.name)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/congress/congress-trader${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(congressTraderResponse, await res.json()) };