import {z} from 'zod' export type GetApiApiV1ExperimentsArgs = {pageId?: string | undefined, name?: string | undefined, status?: (Array<'RUNNING' | 'SCHEDULED' | 'ENDED' | 'DRAFT' | 'ARCHIVED'>) | undefined}; export type GetApiApiV1ExperimentsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const errorEntity = z.object({errorCode: z.string().optional(), message: z.string().optional(), fieldName: z.string().optional()}); export const experimentVariant = z.object({id: z.string().optional(), name: z.string().optional(), weight: z.number().optional(), url: z.string().optional(), promoted: z.boolean().optional()}); export const trafficProportion = z.object({type: z.enum(["SPLIT_EVENLY", "CUSTOM_PERCENTAGES"]).optional(), variants: z.array(experimentVariant).optional()}); export const scheduling = z.object({startDate: z.string().optional(), endDate: z.string().optional()}); export const metric = z.object({name: z.string().optional(), description: z.string().optional(), category: z.enum(["DIFFERENTIATING_FEATURES", "PAID_FEATURES", "PLATFORM_SPECIFIC_CUSTOMIZATION", "SOPHISTICATED_CONTENT_ARCHITECTURE", "POSITIVE_USER_EXPERIENCE", "PLATFORM_SPECIFIC_DEVELOPMENT", "RECENT_ACTIVITY", "HISTORY_LEGACY_DEPRECATED_FEATURES"]).optional(), feature: z.enum(["CONTENTLETS", "LANGUAGES", "CONTENT_TYPES", "SITES", "URL_MAPS", "WORKFLOW", "USERS", "SITE_SEARCH", "IMAGE_API", "LAYOUT", "CONTENT_TYPE_FIELDS", "AI", "EXPERIMENTS"]).optional()}); export const goal = z.object({metric: metric.optional(), type: z.enum(["MINIMIZE", "MAXIMIZE"]).optional()}); export const goals = z.object({primary: goal.optional()}); export const targetingCondition = z.object({id: z.string().optional(), conditionKey: z.string().optional(), values: z.object({empty: z.boolean().optional()}).and(z.record(z.string(), z.string())).optional(), operator: z.enum(["AND", "OR"]).optional()}); export const runningId = z.object({id: z.string().optional(), startDate: z.string().optional(), endDate: z.string().optional()}); export const runningIds = z.object({ids: z.array(runningId).optional()}); export const experimentView = z.object({name: z.string().optional(), description: z.string().optional(), id: z.string().optional(), status: z.enum(["RUNNING", "SCHEDULED", "ENDED", "DRAFT", "ARCHIVED"]).optional(), trafficProportion: trafficProportion.optional(), scheduling: scheduling.optional(), trafficAllocation: z.number().optional(), creationDate: z.string().optional(), modDate: z.string().optional(), pageId: z.string().optional(), createdBy: z.string().optional(), lastModifiedBy: z.string().optional(), goals: goals.optional(), targetingConditions: z.array(targetingCondition).optional(), lookBackWindowExpireTime: z.number().int().optional(), runningIds: runningIds.optional(), createdByUserName: z.string().optional()}); export const messageEntity = z.object({message: z.string().optional()}); export const pagination = z.object({currentPage: z.number().int().optional(), perPage: z.number().int().optional(), totalEntries: z.number().int().optional()}); export const responseEntityExperimentView = z.object({errors: z.array(errorEntity).optional(), entity: z.array(experimentView).optional(), messages: z.array(messageEntity).optional(), i18nMessagesMap: z.record(z.string(), z.string()).optional(), permissions: z.array(z.string()).optional(), pagination: pagination.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 /api/v1/experiments */ export const getApiApiV1Experiments = async (args: GetApiApiV1ExperimentsArgs, options: GetApiApiV1ExperimentsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageId != null) query.append("pageId", String(args.pageId)) if (args.name != null) query.append("name", String(args.name)) for (const item of args.status ?? []) query.append("status", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/experiments${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(responseEntityExperimentView, await res.json()) };