import {z} from 'zod' export type GetApiV1EfileForm1Args = {page?: number | undefined, per_page?: number | undefined, file_number?: Array | undefined, committee_id?: Array | undefined, candidate_id?: Array | undefined, election_state?: Array | undefined, candidate_office?: (Array<'' | 'H' | 'S' | 'P'>) | undefined, candidate_district?: Array | undefined, candidate_party?: Array | undefined, image_number?: Array | undefined, min_load_timestamp?: string | undefined, max_load_timestamp?: string | undefined, committee_type?: Array | undefined, organization_type?: (Array<'' | 'C' | 'L' | 'M' | 'T' | 'V' | 'W' | 'H' | 'I'>) | 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 GetApiV1EfileForm1Options = {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 form1 = z.object({affiliated_candidate_id: z.string().nullable().optional(), affiliated_committee_city: z.string().nullable().optional(), affiliated_committee_id: z.string().nullable().optional(), affiliated_committee_name: z.string().nullable().optional(), affiliated_committee_state: z.string().nullable().optional(), affiliated_committee_str1: z.string().nullable().optional(), affiliated_committee_str2: z.string().nullable().optional(), affiliated_committee_zip: z.string().nullable().optional(), affiliated_relationship_code: z.string().nullable().optional(), candidate_district: z.string().nullable().optional(), candidate_first_name: z.string().nullable().optional(), candidate_id: z.string().nullable().optional(), candidate_last_name: z.string().nullable().optional(), candidate_middle_name: z.string().nullable().optional(), candidate_name: z.string().optional(), candidate_office: z.string().nullable().optional(), candidate_party: z.string().nullable().optional(), city: z.string().nullable().optional(), committee_city: z.string().nullable().optional(), committee_id: z.string().nullable().optional(), committee_name: z.string().nullable().optional(), committee_state: z.string().nullable().optional(), committee_str1: z.string().nullable().optional(), committee_str2: z.string().nullable().optional(), committee_type: z.string().nullable().optional(), committee_zip: z.string().nullable().optional(), derived_committee_type: z.string().nullable().optional(), election_state: z.string().nullable().optional(), email: z.string().nullable().optional(), file_number: z.number().int().optional(), image_number: z.string().optional(), load_timestamp: z.string().optional(), organization_type: z.string().nullable().optional(), pdf_url: z.string().optional(), state: z.string().nullable().optional(), street_1: z.string().nullable().optional(), street_2: z.string().nullable().optional(), treasurer_city: z.string().nullable().optional(), treasurer_first_name: z.string().nullable().optional(), treasurer_last_name: z.string().nullable().optional(), treasurer_middle_name: z.string().nullable().optional(), treasurer_state: z.string().nullable().optional(), treasurer_str1: z.string().nullable().optional(), treasurer_str2: z.string().nullable().optional(), treasurer_zip: z.string().nullable().optional(), zip: z.string().nullable().optional()}); export const form1Page = z.object({pagination: offsetInfo.optional(), results: z.array(form1).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 }; /** * Basic information about electronic files coming into the FEC, posted as they are received. * * GET /v1/efile/form1/ */ export const getApiV1EfileForm1 = async (args: GetApiV1EfileForm1Args, options: GetApiV1EfileForm1Options) => { 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.file_number ?? []) query.append("file_number", String(item)) for (const item of args.committee_id ?? []) query.append("committee_id", String(item)) for (const item of args.candidate_id ?? []) query.append("candidate_id", String(item)) for (const item of args.election_state ?? []) query.append("election_state", String(item)) for (const item of args.candidate_office ?? []) query.append("candidate_office", String(item)) for (const item of args.candidate_district ?? []) query.append("candidate_district", String(item)) for (const item of args.candidate_party ?? []) query.append("candidate_party", String(item)) for (const item of args.image_number ?? []) query.append("image_number", String(item)) if (args.min_load_timestamp != null) query.append("min_load_timestamp", String(args.min_load_timestamp)) if (args.max_load_timestamp != null) query.append("max_load_timestamp", String(args.max_load_timestamp)) for (const item of args.committee_type ?? []) query.append("committee_type", String(item)) for (const item of args.organization_type ?? []) query.append("organization_type", 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/efile/form1/${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(form1Page, await res.json()) };