import {z} from 'zod' export type GetApiApiV1GovUsaCongressTradesReportsArgs = {page?: number | undefined, pageSize?: number | undefined, date_from?: number | undefined, date_to?: number | undefined, updated_since?: number | undefined, date?: number | undefined, chamber?: ('"House"' | '"Senate"') | undefined, fields?: string | undefined, search_keys_type?: ('"report_id"' | '"ticker"') | undefined, search_keys?: string | undefined}; export type GetApiApiV1GovUsaCongressTradesReportsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiSwaggerGovtTradeReportFilerInfo = z.object({chamber: z.string().optional(), committees: z.record(z.string(), z.string()).optional(), display_name: z.string().optional(), district: z.string().optional(), headshot: z.string().optional(), id: z.string().optional(), leadership_positions: z.array(z.string()).optional(), member_id: z.string().optional(), member_name: z.string().optional(), party: z.string().optional(), state: z.string().optional(), status: z.string().optional(), updated: z.number().int().optional(), website: z.string().optional()}); export const apiSwaggerGovtTradeReportSecurity = z.object({name: z.string().optional(), ticker: z.string().optional(), type: z.string().optional()}); export const apiSwaggerGovtTradeReportTransaction = z.object({amendment_number: z.number().int().optional(), amount: z.string().optional(), chamber: z.string().optional(), description: z.string().optional(), disclosure_url: z.string().optional(), id: z.string().optional(), notification_date: z.string().optional(), ownership: z.string().optional(), report_date: z.string().optional(), report_id: z.string().optional(), security: apiSwaggerGovtTradeReportSecurity.optional(), transaction_date: z.string().optional(), transaction_id: z.string().optional(), transaction_type: z.string().optional(), updated: z.number().int().optional()}); export const apiSwaggerGovtTradeReport = z.object({amendment_number: z.number().int().optional(), disclosure_url: z.string().optional(), filer_info: apiSwaggerGovtTradeReportFilerInfo.optional(), id: z.string().optional(), latest: z.boolean().optional(), report_date: z.string().optional(), report_id: z.string().optional(), transactions: z.array(apiSwaggerGovtTradeReportTransaction).optional(), updated: z.number().int().optional()}); export const apiGovernmentTradeReportsResponse = z.object({data: z.array(apiSwaggerGovtTradeReport).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 }; /** * Government Trade Reports * * GET /api/v1/gov/usa/congress/trades/reports */ export const getApiApiV1GovUsaCongressTradesReports = async (args: GetApiApiV1GovUsaCongressTradesReportsArgs, options?: GetApiApiV1GovUsaCongressTradesReportsOptions) => { const {baseUrl = "https://api.benzinga.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.updated_since != null) query.append("updated_since", String(args.updated_since)) if (args.date != null) query.append("date", String(args.date)) if (args.chamber != null) query.append("chamber", String(args.chamber)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.search_keys_type != null) query.append("search_keys_type", String(args.search_keys_type)) if (args.search_keys != null) query.append("search_keys", String(args.search_keys)) if (ApiKeyAuth !== undefined) headers.set("Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/gov/usa/congress/trades/reports${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(apiGovernmentTradeReportsResponse, await res.json()) };