import {z} from 'zod' export type GetApiV1CandidatesTotalsAggregatesArgs = {page?: number | undefined, per_page?: number | undefined, election_year?: Array | undefined, office?: ('' | 'H' | 'S' | 'P') | undefined, is_active_candidate?: boolean | undefined, election_full?: boolean | undefined, min_election_cycle?: number | undefined, max_election_cycle?: number | undefined, state?: Array | undefined, district?: Array | undefined, party?: ('' | 'DEM' | 'REP' | 'OTHER') | undefined, aggregate_by?: ('office' | 'office-state' | 'office-state-district' | 'office-party') | undefined, sort?: Array | undefined, sort_hide_null?: boolean | undefined, sort_null_only?: boolean | undefined, sort_nulls_last?: boolean | undefined, api_key?: string | undefined}; export type GetApiV1CandidatesTotalsAggregatesOptions = {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 candidateTotalAggregate = z.object({district: z.string().optional(), district_number: z.number().int().optional(), election_year: z.number().int().optional(), office: z.string().optional(), party: z.string().optional(), state: z.string().optional(), state_full: z.string().optional(), total_cash_on_hand_end_period: z.number().optional(), total_debts_owed_by_committee: z.number().optional(), total_disbursements: z.number().optional(), total_individual_itemized_contributions: z.number().optional(), total_other_political_committee_contributions: z.number().optional(), total_receipts: z.number().optional(), total_transfers_from_other_authorized_committee: z.number().optional()}); export const candidateTotalAggregatePage = z.object({pagination: offsetInfo.optional(), results: z.array(candidateTotalAggregate).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 }; /** * Candidate total receipts and disbursements aggregated by `aggregate_by`. * * GET /v1/candidates/totals/aggregates/ */ export const getApiV1CandidatesTotalsAggregates = async (args: GetApiV1CandidatesTotalsAggregatesArgs, options: GetApiV1CandidatesTotalsAggregatesOptions) => { 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.election_year ?? []) query.append("election_year", String(item)) if (args.office != null) query.append("office", String(args.office)) if (args.is_active_candidate != null) query.append("is_active_candidate", String(args.is_active_candidate)) if (args.election_full != null) query.append("election_full", String(args.election_full)) if (args.min_election_cycle != null) query.append("min_election_cycle", String(args.min_election_cycle)) if (args.max_election_cycle != null) query.append("max_election_cycle", String(args.max_election_cycle)) for (const item of args.state ?? []) query.append("state", String(item)) for (const item of args.district ?? []) query.append("district", String(item)) if (args.party != null) query.append("party", String(args.party)) if (args.aggregate_by != null) query.append("aggregate_by", String(args.aggregate_by)) for (const item of args.sort ?? []) query.append("sort", String(item)) 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/aggregates/${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(candidateTotalAggregatePage, await res.json()) };