import {z} from 'zod' export type GetApiV1CongressTradesArgs = {ticker?: string | undefined, memberId?: string | undefined, transactionType?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1CongressTradesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const listingNotice = z.object({status: z.string().nullable().optional(), ticker: z.string().nullable().optional(), delistedOn: z.string().nullable().optional(), successorTicker: z.string().nullable().optional()}); export const congressTradeDto = z.object({transactionDate: z.string().optional(), filingDate: z.string().optional(), ticker: z.string().nullable().optional(), memberId: z.string().optional(), memberName: z.string().nullable().optional(), memberPosition: z.string().nullable().optional(), transactionType: z.string().nullable().optional(), assetName: z.string().nullable().optional(), assetType: z.string().nullable().optional(), subholding: z.string().nullable().optional(), ownerType: z.string().nullable().optional(), amountFrom: z.number().int().optional(), amountTo: z.number().int().optional()}); export const pageMeta = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), count: z.number().int().optional(), hasMore: z.boolean().optional(), total: z.number().int().nullable().optional()}); export const congressTradeDtoPagedResponse = z.object({listingNotice: listingNotice.optional(), data: z.array(congressTradeDto).nullable().optional(), meta: pageMeta.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 }; /** * Congressional stock trades filtered by ticker and/or member, newest first. At least one of ticker or memberId is required; when both are given the filters combine. Amounts are the disclosed STOCK Act band bounds, never exact figures; filingDate is when the market could first know of the trade. * * GET /v1/congress/trades */ export const getApiV1CongressTrades = async (args: GetApiV1CongressTradesArgs, options?: GetApiV1CongressTradesOptions) => { 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.ticker != null) query.append("ticker", String(args.ticker)) if (args.memberId != null) query.append("memberId", String(args.memberId)) if (args.transactionType != null) query.append("transactionType", String(args.transactionType)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/congress/trades${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(congressTradeDtoPagedResponse, await res.json()) };