import {z} from 'zod' export type GetApiDiV1ExperimentIdCopyArgs = {id: number, create_type?: string | undefined}; export type GetApiDiV1ExperimentIdCopyOptions = {baseUrl?: string | undefined, basicAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const prophecisExperimentTagPutBasicInfo = z.object({exp_id: z.number().optional(), exp_tag: z.string().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()}); export const prophecisExperimentRequest = z.object({exp_id: z.number().optional(), exp_name: z.string(), group_name: z.string().optional(), exp_desc: z.string(), create_type: z.string().optional(), tag_list: z.array(prophecisExperimentTagPutBasicInfo).optional(), dss_info: dSSInfo.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 }; /** * copyExperiment * * GET /di/v1/experiment/{id}/copy * * Also accepts auth: watsonAuthToken, watsonAuthTokenQuery */ export const getApiDiV1ExperimentIdCopy = async (args: GetApiDiV1ExperimentIdCopyArgs, options?: GetApiDiV1ExperimentIdCopyOptions) => { 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 (args.create_type != null) query.append("create_type", String(args.create_type)) if (basicAuthToken !== undefined) headers.set("Authorization", basicAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/di/v1/experiment/${encodeURIComponent(String(args.id))}/copy${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(prophecisExperimentRequest, await res.json()) };