import {z} from 'zod' export type GetApiV1PresidentialFinancialSummaryArgs = {page?: number | undefined, per_page?: number | undefined, election_year?: Array | undefined, candidate_id?: Array | 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 GetApiV1PresidentialFinancialSummaryOptions = {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 presidentialSummary = z.object({candidate_contributions_less_repayments: z.number().optional(), candidate_id: z.string().nullable().optional(), candidate_last_name: z.string().nullable().optional(), candidate_name: z.string().nullable().optional(), candidate_party_affiliation: z.string().nullable().optional(), cash_on_hand_end: z.number().optional(), committee_designation: z.string().nullable().optional(), committee_id: z.string().nullable().optional(), committee_name: z.string().nullable().optional(), committee_type: z.string().nullable().optional(), debts_owed_by_committee: z.number().optional(), disbursements_less_offsets: z.number().optional(), election_year: z.number().int().nullable().optional(), exempt_legal_accounting_disbursement: z.number().optional(), federal_funds: z.number().optional(), fundraising_disbursements: z.number().optional(), individual_contributions_less_refunds: z.number().optional(), net_receipts: z.number().optional(), offsets_to_operating_expenditures: z.number().optional(), operating_expenditures: z.number().optional(), other_disbursements: z.number().optional(), pac_contributions_less_refunds: z.number().optional(), party_contributions_less_refunds: z.number().optional(), repayments_loans_made_by_candidate: z.number().optional(), repayments_other_loans: z.number().optional(), rounded_net_receipts: z.number().optional(), total_contribution_refunds: z.number().optional(), total_loan_repayments_made: z.number().optional(), transfers_from_affiliated_committees: z.number().optional(), transfers_to_other_authorized_committees: z.number().optional()}); export const presidentialSummaryPage = z.object({pagination: offsetInfo.optional(), results: z.array(presidentialSummary).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/presidential/financial_summary/ */ export const getApiV1PresidentialFinancialSummary = async (args: GetApiV1PresidentialFinancialSummaryArgs, options: GetApiV1PresidentialFinancialSummaryOptions) => { 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_year ?? []) query.append("election_year", String(item)) for (const item of args.candidate_id ?? []) query.append("candidate_id", 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/presidential/financial_summary/${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(presidentialSummaryPage, await res.json()) };