import {z} from 'zod' export type ElectionReadWriteAttributes = {country_code?: (string | null) | undefined, donation_maximum_in_cents?: (number | null) | undefined, election_at?: (string | null) | undefined, election_cycle?: (string | null) | undefined, election_period?: ('primary' | 'general' | 'special' | 'recount' | 'runoff' | 'convention' | 'other' | 'unknown' | 'presidential_primary' | 'previous_campaign' | 'null' | null) | undefined, registration_ends_at?: (string | null) | undefined, state?: (string | null) | undefined}; export type ElectionUpdateRequest = {data: {id: string, type: 'elections', attributes?: ElectionReadWriteAttributes | undefined}}; export type PatchApiApiV2ElectionsIdArgs = {body: ElectionUpdateRequest}; export type PatchApiApiV2ElectionsIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resource = z.object({id: z.string(), type: z.string(), attributes: z.object({}).optional(), relationships: z.object({}).optional()}); export const included = z.array(resource); export const electionShowResponse = z.object({data: z.object({id: z.string(), type: z.literal("elections"), attributes: z.object({country_code: z.string().nullable().optional(), donation_maximum_in_cents: z.number().int().nullable().optional(), election_at: z.string().nullable().optional(), election_cycle: z.string().nullable().optional(), election_period: z.enum(["primary", "general", "special", "recount", "runoff", "convention", "other", "unknown", "presidential_primary", "previous_campaign", "null"]).nullable().optional(), registration_ends_at: z.string().nullable().optional(), state: z.string().nullable().optional(), created_at: z.string().nullable().optional(), donation_maximum: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}).optional()}), included: included.optional(), meta: z.object({}).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 }; /** * Update an existing election * * PATCH /api/v2/elections/{id} */ export const patchApiApiV2ElectionsId = async (args: PatchApiApiV2ElectionsIdArgs, options?: PatchApiApiV2ElectionsIdOptions) => { const {baseUrl = "https://yournation.nationbuilder.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/elections/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(electionShowResponse, await res.json()) };