import {z} from 'zod' export type GetApiV1CandidateCandidateIdCommitteesHistoryCycleArgs = {page?: number | undefined, per_page?: number | undefined, election_full?: boolean | undefined, designation?: (Array<'' | 'A' | 'J' | 'P' | 'U' | 'B' | 'D'>) | undefined, sort?: string | undefined, sort_hide_null?: boolean | undefined, sort_null_only?: boolean | undefined, sort_nulls_last?: boolean | undefined, api_key?: string | undefined, candidate_id: string, cycle: number}; export type GetApiV1CandidateCandidateIdCommitteesHistoryCycleOptions = {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 jFCCommittee = z.object({joint_committee_id: z.string().nullable().optional(), joint_committee_name: z.string().max(100).nullable().optional()}); export const committeeHistoryProfile = z.object({affiliated_committee_name: z.string().max(100).nullable().optional(), candidate_ids: z.array(z.string()).nullable().optional(), city: z.string().max(50).nullable().optional(), committee_id: z.string(), committee_label: z.string().nullable().optional(), committee_type: z.string().max(1).nullable().optional(), committee_type_full: z.string().max(50).nullable().optional(), convert_to_pac_flag: z.boolean().nullable().optional(), cycle: z.number().int(), cycles: z.array(z.number().int()).nullable().optional(), cycles_has_activity: z.array(z.number().int()).nullable().optional(), cycles_has_financial: z.array(z.number().int()).nullable().optional(), designated_agent_city: z.string().max(50).nullable().optional(), designated_agent_first_name: z.string().max(50).nullable().optional(), designated_agent_last_name: z.string().max(50).nullable().optional(), designated_agent_middle_name: z.string().max(50).nullable().optional(), designated_agent_name: z.string().max(100).nullable().optional(), designated_agent_phone_number: z.string().max(50).nullable().optional(), designated_agent_prefix: z.string().max(50).nullable().optional(), designated_agent_state: z.string().max(50).nullable().optional(), designated_agent_street1: z.string().max(50).nullable().optional(), designated_agent_street2: z.string().max(50).nullable().optional(), designated_agent_suffix: z.string().max(50).nullable().optional(), designated_agent_title: z.string().max(50).nullable().optional(), designated_agent_zip: z.string().max(9).nullable().optional(), designation: z.string().max(1).nullable().optional(), designation_full: z.string().max(25).nullable().optional(), filing_frequency: z.string().max(1).nullable().optional(), first_f1_date: z.string().nullable().optional(), first_file_date: z.string().nullable().optional(), former_candidate_election_year: z.number().int().nullable().optional(), former_candidate_id: z.string().max(9).nullable().optional(), former_candidate_name: z.string().max(90).nullable().optional(), former_committee_name: z.string().max(200).nullable().optional(), is_active: z.boolean().nullable().optional(), jfc_committee: z.array(jFCCommittee).optional(), last_cycle_has_activity: z.number().int().nullable().optional(), last_cycle_has_financial: z.number().int().nullable().optional(), last_f1_date: z.string().nullable().optional(), last_file_date: z.string().nullable().optional(), name: z.string().max(100).nullable().optional(), organization_type: z.string().max(1).nullable().optional(), organization_type_full: z.string().max(100).nullable().optional(), party: z.string().max(3).nullable().optional(), party_full: z.string().max(50).nullable().optional(), sponsor_candidate_ids: z.array(z.string()).nullable().optional(), state: z.string().max(2).nullable().optional(), state_full: z.string().max(50).nullable().optional(), street_1: z.string().max(50).nullable().optional(), street_2: z.string().max(50).nullable().optional(), treasurer_name: z.string().max(100).nullable().optional(), zip: z.string().max(9).nullable().optional()}); export const committeeHistoryProfilePage = z.object({pagination: offsetInfo.optional(), results: z.array(committeeHistoryProfile).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/candidate/{candidate_id}/committees/history/{cycle}/ */ export const getApiV1CandidateCandidateIdCommitteesHistoryCycle = async (args: GetApiV1CandidateCandidateIdCommitteesHistoryCycleArgs, options: GetApiV1CandidateCandidateIdCommitteesHistoryCycleOptions) => { 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)) if (args.election_full != null) query.append("election_full", String(args.election_full)) for (const item of args.designation ?? []) query.append("designation", String(item)) 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/candidate/${encodeURIComponent(String(args.candidate_id))}/committees/history/${encodeURIComponent(String(args.cycle))}/${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(committeeHistoryProfilePage, await res.json()) };