import {z} from 'zod' export type GetApiDiV2ExperimentExpIdArgs = {exp_id: string}; export type GetApiDiV2ExperimentExpIdOptions = {baseUrl?: string | undefined, basicAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const deploySetting = z.object({can_deploy_as_offline_service: z.boolean().optional()}); export const dSSInfo = z.object({flow_name: z.string().optional(), flow_version: z.string().optional(), flow_id: z.number().optional(), project_name: z.string().optional(), project_id: z.number().optional(), workspace_name: z.string().optional(), workspace_id: z.number().int().optional()}); export const experimentWithoutPipeline = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), tags: z.array(z.string()).optional(), group_id: z.string().optional(), group_name: z.string().optional(), version_name: z.string().optional(), deploy_setting: deploySetting.optional(), dss_info: dSSInfo.optional(), source_system: z.enum(["MLSS", "DSS", "WTSS"]).optional(), create_user: z.string().optional(), create_time: z.string().optional(), update_user: z.string().optional(), update_time: z.string().optional(), latest_execute_time: 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 }; /** * GetExperiment * * GET /di/v2/experiment/{exp_id} * * Also accepts auth: watsonAuthToken, watsonAuthTokenQuery */ export const getApiDiV2ExperimentExpId = async (args: GetApiDiV2ExperimentExpIdArgs, options?: GetApiDiV2ExperimentExpIdOptions) => { const {baseUrl = "https://di.prophecis.com", fetch: fetchFn = fetch, basicAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuthToken !== undefined) headers.set("Authorization", basicAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/di/v2/experiment/${encodeURIComponent(String(args.exp_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(experimentWithoutPipeline, await res.json()) };