import {z} from 'zod' export type GetApiJobExportJobIdArgs = {job_id: string}; export type GetApiJobExportJobIdOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const jobStatus = z.enum(["queued", "finished", "failed", "started", "deferred", "scheduled", "stopped", "canceled"]); export const exportJobType = z.enum(["ALL_DATA", "ALL_USERS", "ALL_SDOCS", "ALL_CODES", "ALL_TAGS", "ALL_FOLDERS", "ALL_SPAN_ANNOTATIONS", "ALL_SENTENCE_ANNOTATIONS", "ALL_BBOX_ANNOTATIONS", "ALL_MEMOS", "ALL_PROJECT_METADATA", "ALL_WHITEBOARDS", "ALL_TIMELINE_ANALYSES", "ALL_COTA", "ALL_PROMPTS", "ALL_EXPERIMENTS", "SELECTED_SDOCS", "SELECTED_SPAN_ANNOTATIONS", "SELECTED_SENTENCE_ANNOTATIONS", "SELECTED_BBOX_ANNOTATIONS", "SELECTED_MEMOS", "SELECTED_WHITEBOARDS", "SELECTED_TIMELINE_ANALYSES", "SELECTED_COTA"]); export const exportSelectedSdocsParams = z.object({export_job_type: z.literal("SELECTED_SDOCS"), sdoc_ids: z.array(z.number().int())}); export const exportSelectedSpanAnnotationsParams = z.object({export_job_type: z.literal("SELECTED_SPAN_ANNOTATIONS"), span_annotation_ids: z.array(z.number().int())}); export const exportSelectedSentenceAnnotationsParams = z.object({export_job_type: z.literal("SELECTED_SENTENCE_ANNOTATIONS"), sentence_annotation_ids: z.array(z.number().int())}); export const exportSelectedBboxAnnotationsParams = z.object({export_job_type: z.literal("SELECTED_BBOX_ANNOTATIONS"), bbox_annotation_ids: z.array(z.number().int())}); export const exportSelectedMemosParams = z.object({export_job_type: z.literal("SELECTED_MEMOS"), memo_ids: z.array(z.number().int())}); export const exportSelectedWhiteboardsParams = z.object({export_job_type: z.literal("SELECTED_WHITEBOARDS"), whiteboard_ids: z.array(z.number().int())}); export const exportSelectedTimelineAnalysesParams = z.object({export_job_type: z.literal("SELECTED_TIMELINE_ANALYSES"), timeline_analysis_ids: z.array(z.number().int())}); export const exportSelectedCotaParams = z.object({export_job_type: z.literal("SELECTED_COTA"), cota_ids: z.array(z.number().int())}); export const exportJobInput = z.object({project_id: z.number().int(), export_job_type: exportJobType, specific_export_job_parameters: z.union([exportSelectedSdocsParams, exportSelectedSpanAnnotationsParams, exportSelectedSentenceAnnotationsParams, exportSelectedBboxAnnotationsParams, exportSelectedMemosParams, exportSelectedWhiteboardsParams, exportSelectedTimelineAnalysesParams, exportSelectedCotaParams]).nullable()}); export const exportJobOutput = z.object({results_url: z.string().nullable().optional()}); export const exportJobRead = z.object({job_id: z.string(), job_type: z.literal("export"), project_id: z.number().int(), status: jobStatus, status_message: z.string().nullable().optional(), current_step: z.number().int(), steps: z.array(z.string()), input: exportJobInput, output: exportJobOutput.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 Export job * * GET /job/export/{job_id} */ export const getApiJobExportJobId = async (args: GetApiJobExportJobIdArgs, options: GetApiJobExportJobIdOptions) => { 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/export/${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(exportJobRead, await res.json()) };