import {z} from 'zod' export type GetApiV1BatchJobsArgs = {batchJobIds?: Array | undefined, states?: (Array<'BATCH_JOB_STATE_UNSPECIFIED' | 'BATCH_JOB_STATE_ERROR' | 'BATCH_JOB_STATE_INITIALIZED' | 'BATCH_JOB_STATE_QUEUED' | 'BATCH_JOB_STATE_PREPROCESSING' | 'BATCH_JOB_STATE_PREPROCESSED' | 'BATCH_JOB_STATE_SUBMITTED' | 'BATCH_JOB_STATE_POSTPROCESSING' | 'BATCH_JOB_STATE_POSTPROCESSED' | 'BATCH_JOB_STATE_COMPLETED' | 'BATCH_JOB_STATE_CANCELLED' | 'BATCH_JOB_STATE_EVALUATING'>) | undefined}; export type GetApiV1BatchJobsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v1BatchJobState = z.enum(["BATCH_JOB_STATE_UNSPECIFIED", "BATCH_JOB_STATE_ERROR", "BATCH_JOB_STATE_INITIALIZED", "BATCH_JOB_STATE_QUEUED", "BATCH_JOB_STATE_PREPROCESSING", "BATCH_JOB_STATE_PREPROCESSED", "BATCH_JOB_STATE_SUBMITTED", "BATCH_JOB_STATE_POSTPROCESSING", "BATCH_JOB_STATE_POSTPROCESSED", "BATCH_JOB_STATE_COMPLETED", "BATCH_JOB_STATE_CANCELLED", "BATCH_JOB_STATE_EVALUATING"]); export const v1BatchJob = z.object({batchJobId: z.string().optional(), name: z.string().optional(), state: v1BatchJobState.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), policyId: z.string().optional(), policyVersionId: z.string().optional(), outputFileUrl: z.string().optional()}); export const v1GetBatchJobsResponse = z.object({batchJobs: z.array(v1BatchJob).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 batch jobs * * GET /v1/batch-jobs */ export const getApiV1BatchJobs = async (args: GetApiV1BatchJobsArgs, options?: GetApiV1BatchJobsOptions) => { const {baseUrl = "https://gateway.app.clavata.ai:8443", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.batchJobIds ?? []) query.append("batchJobIds", String(item)) for (const item of args.states ?? []) query.append("states", String(item)) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/batch-jobs${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(v1GetBatchJobsResponse, await res.json()) };