import {z} from 'zod' export type GetApiJobCrawlerJobIdArgs = {job_id: string}; export type GetApiJobCrawlerJobIdOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const jobStatus = z.enum(["queued", "finished", "failed", "started", "deferred", "scheduled", "stopped", "canceled"]); export const language = z.enum(["auto", "de", "en", "it"]); export const processingSettings = z.object({extract_images: z.boolean(), pages_per_chunk: z.number().int(), keyword_number: z.number().int(), keyword_deduplication_threshold: z.number(), keyword_max_ngram_size: z.number().int(), language: language, model: z.string()}); export const crawlerJobInput = z.object({project_id: z.number().int(), settings: processingSettings, urls: z.array(z.string())}); export const crawlerJobOutput = z.object({crawled_data_zip: z.string()}); export const crawlerJobRead = z.object({job_id: z.string(), job_type: z.literal("crawler"), project_id: z.number().int(), status: jobStatus, status_message: z.string().nullable().optional(), current_step: z.number().int(), steps: z.array(z.string()), input: crawlerJobInput, output: crawlerJobOutput.nullable().optional(), created: z.string(), finished: z.string().nullable().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 Crawler job * * GET /job/crawler/{job_id} */ export const getApiJobCrawlerJobId = async (args: GetApiJobCrawlerJobIdArgs, options: GetApiJobCrawlerJobIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/job/crawler/${encodeURIComponent(String(args.job_id))}${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(crawlerJobRead, await res.json()) };