import {z} from 'zod' export type V1CreateBatchJobRequest = {name?: string | undefined, policyId?: string | undefined, webhookUrl?: string | undefined}; export type CreateApiV1BatchJobsArgs = {body: V1CreateBatchJobRequest}; export type CreateApiV1BatchJobsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v1BatchJobErrorCode = z.enum(["BATCH_JOB_ERROR_UNSPECIFIED", "BATCH_JOB_ERROR_INVALID_INPUT_FILE", "BATCH_JOB_ERROR_INVALID_POLICY", "BATCH_JOB_ERROR_INVALID_POLICY_VERSION", "BATCH_JOB_ERROR_INVALID_PRIORITY"]); export const v1BatchJobError = z.object({errorCode: v1BatchJobErrorCode.optional(), errorMessage: z.string().optional()}); export const v1CreateBatchJobResponse = z.object({batchJobId: z.string().optional(), name: z.string().optional(), error: v1BatchJobError.optional(), inputFileUploadUrl: z.string().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 }; /** * Create a new batch job * * POST /v1/batch-jobs */ export const createApiV1BatchJobs = async (args: CreateApiV1BatchJobsArgs, options?: CreateApiV1BatchJobsOptions) => { const {baseUrl = "https://gateway.app.clavata.ai:8443", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(v1CreateBatchJobResponse, await res.json()) };