import {z} from 'zod' export type GetApiApiV1UsRulemakingCoverageOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceCoverage = z.object({name: z.string(), rowCounts: z.record(z.string(), z.unknown()).optional(), latestChange: z.string().nullable().optional(), lastRefreshAt: z.string().nullable().optional(), measuredAt: z.string().nullable().optional(), status: z.enum(["ok", "stale", "unmeasured"]).optional(), limitations: z.array(z.string()).optional()}); export const agendaEditionCoverage = z.object({editionId: z.string(), season: z.string().nullable().optional(), year: z.number().int().nullable().optional(), runDate: z.string().nullable().optional(), entryCount: z.number().int().nullable().optional()}); export const coverageResponse = z.object({processingTimeMs: z.number().optional(), creditsConsumed: z.number().int().optional(), sources: z.array(sourceCoverage), agendaEditions: z.array(agendaEditionCoverage), limitations: z.array(z.string())}); 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 }; /** * What the rulemaking store holds (free) * * GET /api/v1/us/rulemaking/coverage * * Also accepts auth: ApiKeyHeader */ export const getApiApiV1UsRulemakingCoverage = async (options?: GetApiApiV1UsRulemakingCoverageOptions) => { const {baseUrl = "https://api.vaquill.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/us/rulemaking/coverage${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(coverageResponse, await res.json()) };