import {z} from 'zod' export type ProphecisExperimentTagPutBasicInfo = {exp_id?: number | undefined, exp_tag?: string | undefined}; export type DSSInfo = {flow_name?: string | undefined, flow_version?: string | undefined, flow_id?: number | undefined, project_name?: string | undefined, project_id?: number | undefined}; export type ProphecisExperimentRequest = {exp_id?: number | undefined, exp_name: string, group_name?: string | undefined, exp_desc: string, create_type?: string | undefined, tag_list?: Array | undefined, dss_info?: DSSInfo | undefined}; export type CreateApiDiV1ExperimentArgs = {body: ProphecisExperimentRequest}; export type CreateApiDiV1ExperimentOptions = {baseUrl?: string | undefined, basicAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const prophecisExperimentIDResponse = z.object({id: z.number(), exp_name: 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 }; /** * createExperiment * * POST /di/v1/experiment * * Also accepts auth: watsonAuthToken, watsonAuthTokenQuery */ export const createApiDiV1Experiment = async (args: CreateApiDiV1ExperimentArgs, options?: CreateApiDiV1ExperimentOptions) => { const {baseUrl = "https://di.prophecis.com", fetch: fetchFn = fetch, basicAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuthToken !== undefined) headers.set("Authorization", basicAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/di/v1/experiment${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(prophecisExperimentIDResponse, await res.json()) };