import {z} from 'zod' export type GetApiV1ElectionDatesArgs = {page?: number | undefined, per_page?: number | undefined, election_state?: Array | undefined, election_district?: Array | undefined, election_party?: Array | undefined, office_sought?: (Array<'H' | 'S' | 'P'>) | undefined, min_election_date?: string | undefined, max_election_date?: string | undefined, election_type_id?: Array | undefined, min_create_date?: string | undefined, max_create_date?: string | undefined, min_update_date?: string | undefined, max_update_date?: string | undefined, election_year?: Array | undefined, min_primary_general_date?: string | undefined, max_primary_general_date?: string | 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 GetApiV1ElectionDatesOptions = {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 electionDates = z.object({active_election: z.boolean().optional(), create_date: z.string().nullable().optional(), election_date: z.string().nullable().optional(), election_district: z.string().nullable().optional(), election_notes: z.string().nullable().optional(), election_party: z.string().nullable().optional(), election_state: z.string().nullable().optional(), election_type_full: z.string().optional(), election_type_id: z.string().nullable().optional(), election_year: z.number().int().nullable().optional(), office_sought: z.string().nullable().optional(), primary_general_date: z.string().nullable().optional(), update_date: z.string().nullable().optional()}); export const electionDatesPage = z.object({pagination: offsetInfo.optional(), results: z.array(electionDates).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/election-dates/ */ export const getApiV1ElectionDates = async (args: GetApiV1ElectionDatesArgs, options: GetApiV1ElectionDatesOptions) => { 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_state ?? []) query.append("election_state", String(item)) for (const item of args.election_district ?? []) query.append("election_district", String(item)) for (const item of args.election_party ?? []) query.append("election_party", String(item)) for (const item of args.office_sought ?? []) query.append("office_sought", String(item)) if (args.min_election_date != null) query.append("min_election_date", String(args.min_election_date)) if (args.max_election_date != null) query.append("max_election_date", String(args.max_election_date)) for (const item of args.election_type_id ?? []) query.append("election_type_id", String(item)) if (args.min_create_date != null) query.append("min_create_date", String(args.min_create_date)) if (args.max_create_date != null) query.append("max_create_date", String(args.max_create_date)) if (args.min_update_date != null) query.append("min_update_date", String(args.min_update_date)) if (args.max_update_date != null) query.append("max_update_date", String(args.max_update_date)) for (const item of args.election_year ?? []) query.append("election_year", String(item)) if (args.min_primary_general_date != null) query.append("min_primary_general_date", String(args.min_primary_general_date)) if (args.max_primary_general_date != null) query.append("max_primary_general_date", String(args.max_primary_general_date)) 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/election-dates/${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(electionDatesPage, await res.json()) };