import {z} from 'zod' export type WebhookAuthRequest = {scheme?: ('none' | 'bearer' | 'basic' | 'header') | undefined, secret?: (string | null) | undefined, headerName?: (string | null) | undefined}; export type CreateWatchRequest = {corpusType: 'agency_adjudication' | 'agency_guidance' | 'agency_manuals' | 'cfr' | 'federal_local_rules' | 'federal_primary_law' | 'federal_register' | 'federal_register_notice' | 'federal_rules' | 'session_law' | 'state' | 'state_ag_opinion' | 'state_constitution' | 'state_insurance_bulletins' | 'state_regulation' | 'state_rules' | 'statute_compilation' | 'usc', state?: ('federal' | 'al' | 'ak' | 'as' | 'az' | 'ar' | 'ca' | 'co' | 'ct' | 'de' | 'dc' | 'fl' | 'ga' | 'gu' | 'hi' | 'id' | 'il' | 'in' | 'ia' | 'ks' | 'ky' | 'la' | 'me' | 'md' | 'ma' | 'mi' | 'mn' | 'ms' | 'mo' | 'mt' | 'ne' | 'nv' | 'nh' | 'nj' | 'nm' | 'ny' | 'nc' | 'nd' | 'mp' | 'oh' | 'ok' | 'or' | 'pa' | 'pr' | 'ri' | 'sc' | 'sd' | 'tn' | 'tx' | 'ut' | 'vt' | 'vi' | 'va' | 'wa' | 'wv' | 'wi' | 'wy' | null) | undefined, channel?: ('webhook' | 'email' | 'both') | undefined, webhookUrl?: (string | null) | undefined, webhookSecret?: (string | null) | undefined, emailAddress?: (string | null) | undefined, scope?: ({[key: string]: string} | null) | undefined, webhookAuth?: (WebhookAuthRequest | null) | undefined}; export type CreateApiApiV1WatchesArgs = {body: CreateWatchRequest}; export type CreateApiApiV1WatchesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookAuthResponse = z.object({scheme: z.string(), headerName: z.string().nullable().optional(), hasSecret: z.boolean().optional()}); export const watchResponse = z.object({id: z.string(), corpusType: z.string(), state: z.string().nullable().optional(), channel: z.string(), webhookUrl: z.string().nullable().optional(), emailAddress: z.string().nullable().optional(), scope: z.record(z.string(), z.string()).nullable().optional(), isActive: z.boolean(), createdAt: z.string(), updatedAt: z.string().nullable().optional(), lastNotifiedAt: z.string().nullable().optional(), lastStatus: z.number().int().nullable().optional(), webhookAuth: webhookAuthResponse.nullable().optional(), cadence: z.string().nullable().optional(), lastRetrievedAt: z.string().nullable().optional(), changeSignal: z.string().optional()}); export const responseMeta = z.object({processingTimeMs: z.number(), creditsConsumed: z.number(), cursor: z.number().int().nullable().optional(), nextBeforeId: z.number().int().nullable().optional(), hasMore: z.boolean().nullable().optional()}); export const watchEnvelope = z.object({data: watchResponse, meta: responseMeta}); 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 }; /** * Create a board watch * * POST /api/v1/watches * * Also accepts auth: ApiKeyHeader */ export const createApiApiV1Watches = async (args: CreateApiApiV1WatchesArgs, options?: CreateApiApiV1WatchesOptions) => { const {baseUrl = "https://api.vaquill.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/watches${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(watchEnvelope, await res.json()) };