import {z} from 'zod' export type GetApiV1CommunicationCostsArgs = {page?: number | undefined, per_page?: number | undefined, image_number?: Array | undefined, min_image_number?: string | undefined, max_image_number?: string | undefined, min_amount?: number | undefined, max_amount?: number | undefined, min_date?: string | undefined, max_date?: string | undefined, committee_id?: Array | undefined, candidate_id?: Array | undefined, support_oppose_indicator?: (Array<'S' | 'O'>) | 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 GetApiV1CommunicationCostsOptions = {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 communicationCost = z.object({action_code: z.string().nullable().optional(), action_code_full: 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().nullable().optional(), candidate_office: z.string().nullable().optional(), candidate_office_district: z.string().nullable().optional(), candidate_office_full: z.string().nullable().optional(), candidate_office_state: z.string().nullable().optional(), committee_id: z.string().nullable().optional(), committee_name: z.string().nullable().optional(), communication_class: z.string().nullable().optional(), communication_type: z.string().nullable().optional(), communication_type_full: z.string().nullable().optional(), cycle: z.number().int().nullable().optional(), file_number: z.number().int().nullable().optional(), form_type_code: z.string().nullable().optional(), image_number: z.string().nullable().optional(), original_sub_id: z.number().int().nullable().optional(), pdf_url: z.string().nullable().optional(), primary_general_indicator: z.string().nullable().optional(), primary_general_indicator_description: z.string().nullable().optional(), purpose: z.string().nullable().optional(), report_type: z.string().nullable().optional(), report_year: z.number().int().nullable().optional(), schedule_type: z.string().nullable().optional(), schedule_type_full: z.string().nullable().optional(), state_full: z.string().nullable().optional(), sub_id: z.number().int().optional(), support_oppose_indicator: z.string().nullable().optional(), tran_id: z.string().nullable().optional(), transaction_amount: z.number().optional(), transaction_date: z.string().nullable().optional(), transaction_type: z.string().nullable().optional()}); export const communicationCostPage = z.object({pagination: offsetInfo.optional(), results: z.array(communicationCost).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/communication_costs/ */ export const getApiV1CommunicationCosts = async (args: GetApiV1CommunicationCostsArgs, options: GetApiV1CommunicationCostsOptions) => { 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.image_number ?? []) query.append("image_number", String(item)) if (args.min_image_number != null) query.append("min_image_number", String(args.min_image_number)) if (args.max_image_number != null) query.append("max_image_number", String(args.max_image_number)) if (args.min_amount != null) query.append("min_amount", String(args.min_amount)) if (args.max_amount != null) query.append("max_amount", String(args.max_amount)) if (args.min_date != null) query.append("min_date", String(args.min_date)) if (args.max_date != null) query.append("max_date", String(args.max_date)) 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.support_oppose_indicator ?? []) query.append("support_oppose_indicator", 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/communication_costs/${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(communicationCostPage, await res.json()) };