import {z} from 'zod' export type GetApiV1CandidateCandidateIdArgs = {page?: number | undefined, per_page?: number | undefined, cycle?: Array | undefined, election_year?: Array | undefined, office?: (Array<'' | 'H' | 'S' | 'P'>) | undefined, state?: Array | undefined, party?: Array | undefined, year?: string | undefined, district?: Array | undefined, candidate_status?: (Array<'' | 'C' | 'F' | 'N' | 'P'>) | undefined, incumbent_challenge?: (Array<'' | 'I' | 'C' | 'O'>) | undefined, federal_funds_flag?: boolean | undefined, has_raised_funds?: boolean | undefined, name?: Array | 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}; export type GetApiV1CandidateCandidateIdOptions = {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 candidateDetail = 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_first_name: z.string().max(100).nullable().optional(), candidate_id: z.string().nullable().optional(), 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(), cycles: z.array(z.number().int()).nullable().optional(), district: z.string().max(2).nullable().optional(), district_number: z.number().int().nullable().optional(), election_districts: z.array(z.string()).nullable().optional(), election_years: z.array(z.number().int()).nullable().optional(), federal_funds_flag: z.boolean().optional(), first_file_date: z.string().nullable().optional(), flags: z.unknown(), has_raised_funds: z.boolean().optional(), incumbent_challenge: z.string().max(1).nullable().optional(), incumbent_challenge_full: z.string().max(10).nullable().optional(), 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(), party: z.string().max(3).nullable().optional(), party_full: z.string().max(255).nullable().optional(), state: z.string().max(2).nullable().optional()}); export const candidateDetailPage = z.object({pagination: offsetInfo.optional(), results: z.array(candidateDetail).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}/ */ export const getApiV1CandidateCandidateId = async (args: GetApiV1CandidateCandidateIdArgs, options: GetApiV1CandidateCandidateIdOptions) => { 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.cycle ?? []) query.append("cycle", String(item)) for (const item of args.election_year ?? []) query.append("election_year", String(item)) for (const item of args.office ?? []) query.append("office", String(item)) for (const item of args.state ?? []) query.append("state", String(item)) for (const item of args.party ?? []) query.append("party", String(item)) if (args.year != null) query.append("year", String(args.year)) for (const item of args.district ?? []) query.append("district", String(item)) for (const item of args.candidate_status ?? []) query.append("candidate_status", String(item)) for (const item of args.incumbent_challenge ?? []) query.append("incumbent_challenge", String(item)) 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)) for (const item of args.name ?? []) query.append("name", 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))}/${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(candidateDetailPage, await res.json()) };