import {z} from 'zod' export type GetApiV1CandidatesTotalsArgs = {page?: number | undefined, per_page?: number | undefined, q?: Array | undefined, candidate_id?: Array | undefined, election_year?: Array | undefined, cycle?: Array | undefined, office?: (Array<'' | 'H' | 'S' | 'P'>) | undefined, election_full?: boolean | undefined, state?: Array | undefined, district?: Array | undefined, party?: Array | undefined, min_receipts?: number | undefined, max_receipts?: number | undefined, min_disbursements?: number | undefined, max_disbursements?: number | undefined, min_cash_on_hand_end_period?: number | undefined, max_cash_on_hand_end_period?: number | undefined, min_debts_owed_by_committee?: number | undefined, max_debts_owed_by_committee?: number | undefined, federal_funds_flag?: boolean | undefined, has_raised_funds?: boolean | undefined, is_active_candidate?: boolean | undefined, sort?: string | undefined, sort_hide_null?: boolean | undefined, sort_null_only?: boolean | undefined, sort_nulls_last?: boolean | undefined, api_key?: string | undefined}; export type GetApiV1CandidatesTotalsOptions = {baseUrl: string, ApiKeyHeaderAuth?: string | undefined, ApiKeyQueryAuth?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const offsetInfo = z.object({count: z.number().int().optional(), is_count_exact: z.boolean().optional(), page: z.number().int().optional(), pages: z.number().int().optional(), per_page: z.number().int().optional()}); export const candidateHistoryTotal = z.object({active_through: z.number().int().nullable().optional(), address_city: z.string().max(100).nullable().optional(), address_state: z.string().max(2).nullable().optional(), address_street_1: z.string().max(200).nullable().optional(), address_street_2: z.string().max(200).nullable().optional(), address_zip: z.string().max(10).nullable().optional(), candidate_election_year: z.number().int().nullable().optional(), candidate_first_name: z.string().max(100).nullable().optional(), candidate_id: z.string(), candidate_inactive: z.boolean().nullable().optional(), candidate_last_name: z.string().max(100).nullable().optional(), candidate_middle_name: z.string().max(100).nullable().optional(), candidate_prefix: z.string().max(100).nullable().optional(), candidate_status: z.string().max(1).nullable().optional(), candidate_suffix: z.string().max(100).nullable().optional(), cash_on_hand_end_period: z.number().nullable().optional(), coverage_end_date: z.string().nullable().optional(), coverage_start_date: z.string().nullable().optional(), cycle: z.number().int(), cycles: z.array(z.number().int()).nullable().optional(), debts_owed_by_committee: z.number().nullable().optional(), disbursements: z.number().optional(), district: z.string().max(2).nullable().optional(), district_number: z.number().int().nullable().optional(), election_districts: z.array(z.string()).nullable().optional(), election_year: z.number().int().optional(), election_years: z.array(z.number().int()).nullable().optional(), fec_cycles_in_election: z.array(z.number().int()).nullable().optional(), federal_funds_flag: z.boolean().nullable().optional(), first_file_date: z.string().nullable().optional(), flags: z.unknown(), has_raised_funds: z.boolean().nullable().optional(), incumbent_challenge: z.string().max(1).nullable().optional(), incumbent_challenge_full: z.string().max(10).nullable().optional(), individual_itemized_contributions: z.number().optional(), is_election: z.boolean(), last_f2_date: z.string().nullable().optional(), last_file_date: z.string().nullable().optional(), load_date: z.string().nullable().optional(), name: z.string().max(100).nullable().optional(), office: z.string().max(1).nullable().optional(), office_full: z.string().max(9).nullable().optional(), other_political_committee_contributions: z.number().optional(), party: z.string().max(3).nullable().optional(), party_full: z.string().max(255).nullable().optional(), receipts: z.number().optional(), rounded_election_years: z.array(z.number().int()).nullable().optional(), state: z.string().max(2).nullable().optional(), state_full: z.string().max(50).nullable().optional(), transfers_from_other_authorized_committee: z.number().optional(), two_year_period: z.number().int()}); export const candidateHistoryTotalPage = z.object({pagination: offsetInfo.optional(), results: z.array(candidateHistoryTotal).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 /v1/candidates/totals/ */ export const getApiV1CandidatesTotals = async (args: GetApiV1CandidatesTotalsArgs, options: GetApiV1CandidatesTotalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyHeaderAuth, ApiKeyQueryAuth, apiKey} = 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.per_page != null) query.append("per_page", String(args.per_page)) for (const item of args.q ?? []) query.append("q", String(item)) for (const item of args.candidate_id ?? []) query.append("candidate_id", String(item)) for (const item of args.election_year ?? []) query.append("election_year", String(item)) for (const item of args.cycle ?? []) query.append("cycle", String(item)) for (const item of args.office ?? []) query.append("office", String(item)) if (args.election_full != null) query.append("election_full", String(args.election_full)) for (const item of args.state ?? []) query.append("state", String(item)) for (const item of args.district ?? []) query.append("district", String(item)) for (const item of args.party ?? []) query.append("party", String(item)) if (args.min_receipts != null) query.append("min_receipts", String(args.min_receipts)) if (args.max_receipts != null) query.append("max_receipts", String(args.max_receipts)) if (args.min_disbursements != null) query.append("min_disbursements", String(args.min_disbursements)) if (args.max_disbursements != null) query.append("max_disbursements", String(args.max_disbursements)) if (args.min_cash_on_hand_end_period != null) query.append("min_cash_on_hand_end_period", String(args.min_cash_on_hand_end_period)) if (args.max_cash_on_hand_end_period != null) query.append("max_cash_on_hand_end_period", String(args.max_cash_on_hand_end_period)) if (args.min_debts_owed_by_committee != null) query.append("min_debts_owed_by_committee", String(args.min_debts_owed_by_committee)) if (args.max_debts_owed_by_committee != null) query.append("max_debts_owed_by_committee", String(args.max_debts_owed_by_committee)) if (args.federal_funds_flag != null) query.append("federal_funds_flag", String(args.federal_funds_flag)) if (args.has_raised_funds != null) query.append("has_raised_funds", String(args.has_raised_funds)) if (args.is_active_candidate != null) query.append("is_active_candidate", String(args.is_active_candidate)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.sort_hide_null != null) query.append("sort_hide_null", String(args.sort_hide_null)) if (args.sort_null_only != null) query.append("sort_null_only", String(args.sort_null_only)) if (args.sort_nulls_last != null) query.append("sort_nulls_last", String(args.sort_nulls_last)) if (args.api_key != null) query.append("api_key", String(args.api_key)) if (ApiKeyHeaderAuth !== undefined) headers.set("X-Api-Key", ApiKeyHeaderAuth) if (ApiKeyQueryAuth !== undefined) query.set("api_key", ApiKeyQueryAuth) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/candidates/totals/${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(candidateHistoryTotalPage, await res.json()) };