import {z} from 'zod' export type GetApiV1RulemakingSearchArgs = {q?: string | undefined, q_exclude?: string | undefined, from_hit?: number | undefined, hits_returned?: number | undefined, rm_no?: Array | undefined, rm_name?: Array | undefined, rm_year?: number | undefined, doc_category_id?: (Array<'' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8'>) | undefined, doc_id?: number | undefined, min_federal_registry_publish_date?: string | undefined, max_federal_registry_publish_date?: string | undefined, min_hearing_date?: string | undefined, max_hearing_date?: string | undefined, min_vote_date?: string | undefined, max_vote_date?: string | undefined, is_key_document?: boolean | undefined, is_open_for_comment?: boolean | undefined, entity_name?: string | undefined, entity_role_type?: (Array<'' | '1' | '2' | '3' | '4' | '5'>) | undefined, filename?: string | undefined, sort?: string | undefined, q_proximity?: Array | undefined, max_gaps?: number | undefined, proximity_preserve_order?: boolean | undefined, proximity_filter?: ('after' | 'before') | undefined, proximity_filter_term?: string | undefined, api_key?: string | undefined}; export type GetApiV1RulemakingSearchOptions = {baseUrl: string, ApiKeyHeaderAuth?: string | undefined, ApiKeyQueryAuth?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1RulemakingSearchResponse = z.object({rulemakings: z.array(z.object({admin_close_date: z.string().optional(), calculated_comment_close_date: z.string().optional(), comment_close_date: z.string().optional(), commenter_names: z.array(z.string()).optional(), counsel_names: z.array(z.string()).optional(), descriptions: z.string().optional(), document_highlights: z.object({}).optional(), documents: z.array(z.object({doc_category_id: z.string().optional(), doc_category_label: z.string().optional(), doc_date: z.string().optional(), doc_description: z.string().optional(), doc_entities: z.object({name: z.string().optional(), role: z.string().optional()}).optional(), doc_id: z.number().int().optional(), doc_type_id: z.number().int().optional(), doc_type_label: z.string().optional(), filename: z.string().optional(), is_key_document: z.boolean().optional(), level_1: z.number().int().optional(), level_1_label: z.string().optional(), level_2: z.number().int().optional(), level_2_label: z.string().optional(), level_2_labels: z.object({level_2: z.number().int().optional(), level_2_docs: z.object({doc_category_id: z.number().int().optional(), doc_category_label: z.string().optional(), doc_description: z.string().optional(), doc_entities: z.object({name: z.string().optional(), role: z.string().optional()}).optional(), doc_type_id: z.number().int().optional(), doc_type_label: z.string().optional(), document_date: z.string().optional(), document_id: z.number().int().optional(), filename: z.string().optional(), is_key_document: z.boolean().optional(), level_1: z.number().int().optional(), level_1_label: z.string().optional(), level_2: z.number().int().optional(), level_2_label: z.string().optional(), sort_order: z.number().int().optional(), url: z.string().optional()}).optional(), level_2_label: z.string().optional()}).optional(), sort_order: z.number().int().optional(), url: z.string().optional()})).optional(), fr_publication_dates: z.array(z.string()).optional(), hearing_dates: z.array(z.string()).optional(), is_open_for_comment: z.boolean().optional(), key_documents: z.array(z.object({doc_date: z.string().optional(), doc_description: z.string().optional(), doc_id: z.number().int().optional(), doc_type_id: z.number().int().optional(), doc_type_label: z.string().optional(), filename: z.string().optional(), url: z.string().optional()})).optional(), last_updated_date: z.string().optional(), petitioner_names: z.array(z.string()).optional(), representative_names: z.array(z.string()).optional(), rm_entities: z.array(z.object({name: z.string().optional(), role: z.string().optional(), type: z.string().optional()})).optional(), rm_id: z.number().int().optional(), rm_name: z.string().optional(), rm_no: z.string().optional(), rm_serial: z.number().int().optional(), rm_year: z.number().int().optional(), sort1: z.number().int().optional(), sort2: z.number().int().optional(), sync_status: z.string().optional(), title: z.string().optional(), type: z.string().optional(), vote_dates: z.string().optional(), witness: z.string().optional()})).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/rulemaking/search/ */ export const getApiV1RulemakingSearch = async (args: GetApiV1RulemakingSearchArgs, options: GetApiV1RulemakingSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyHeaderAuth, ApiKeyQueryAuth, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.q_exclude != null) query.append("q_exclude", String(args.q_exclude)) if (args.from_hit != null) query.append("from_hit", String(args.from_hit)) if (args.hits_returned != null) query.append("hits_returned", String(args.hits_returned)) for (const item of args.rm_no ?? []) query.append("rm_no", String(item)) for (const item of args.rm_name ?? []) query.append("rm_name", String(item)) if (args.rm_year != null) query.append("rm_year", String(args.rm_year)) for (const item of args.doc_category_id ?? []) query.append("doc_category_id", String(item)) if (args.doc_id != null) query.append("doc_id", String(args.doc_id)) if (args.min_federal_registry_publish_date != null) query.append("min_federal_registry_publish_date", String(args.min_federal_registry_publish_date)) if (args.max_federal_registry_publish_date != null) query.append("max_federal_registry_publish_date", String(args.max_federal_registry_publish_date)) if (args.min_hearing_date != null) query.append("min_hearing_date", String(args.min_hearing_date)) if (args.max_hearing_date != null) query.append("max_hearing_date", String(args.max_hearing_date)) if (args.min_vote_date != null) query.append("min_vote_date", String(args.min_vote_date)) if (args.max_vote_date != null) query.append("max_vote_date", String(args.max_vote_date)) if (args.is_key_document != null) query.append("is_key_document", String(args.is_key_document)) if (args.is_open_for_comment != null) query.append("is_open_for_comment", String(args.is_open_for_comment)) if (args.entity_name != null) query.append("entity_name", String(args.entity_name)) for (const item of args.entity_role_type ?? []) query.append("entity_role_type", String(item)) if (args.filename != null) query.append("filename", String(args.filename)) if (args.sort != null) query.append("sort", String(args.sort)) for (const item of args.q_proximity ?? []) query.append("q_proximity", String(item)) if (args.max_gaps != null) query.append("max_gaps", String(args.max_gaps)) if (args.proximity_preserve_order != null) query.append("proximity_preserve_order", String(args.proximity_preserve_order)) if (args.proximity_filter != null) query.append("proximity_filter", String(args.proximity_filter)) if (args.proximity_filter_term != null) query.append("proximity_filter_term", String(args.proximity_filter_term)) 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/rulemaking/search/${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(getApiV1RulemakingSearchResponse, await res.json()) };