import {z} from 'zod' export type ExperimentsCreateExperimentMetricGroupV2RequestDataAttributesMetricsItems = {metric_id: string}; export type ExperimentsCreateExperimentMetricGroupV2RequestDataAttributes = {metrics?: Array | undefined, migration_metadata: unknown, name: string}; export type ExperimentsPatchExperimentMetricGroupV2RequestDataType = 'experiment-metric-groups'; export type ExperimentsCreateExperimentMetricGroupV2RequestData = {attributes: ExperimentsCreateExperimentMetricGroupV2RequestDataAttributes, id?: string | undefined, type: ExperimentsPatchExperimentMetricGroupV2RequestDataType}; export type ExperimentsCreateExperimentMetricGroupV2Request = {data: ExperimentsCreateExperimentMetricGroupV2RequestData}; export type CreateApiApiV2ExperimentsExperimentIdMetricGroupsArgs = {experiment_id: string, body: ExperimentsCreateExperimentMetricGroupV2Request}; export type CreateApiApiV2ExperimentsExperimentIdMetricGroupsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentsExperimentMetricGroupMutationV2DataAttributesMetricsItems = z.object({is_primary: z.boolean(), metric_id: z.string(), metric_name: z.string()}); export const experimentsExperimentMetricGroupV2DTODataAttributes = z.object({is_decision: z.boolean().optional(), metrics: z.array(experimentsExperimentMetricGroupMutationV2DataAttributesMetricsItems).optional(), migration_metadata: z.unknown(), name: z.string().optional()}); export const experimentsPatchExperimentMetricGroupV2RequestDataType = z.literal("experiment-metric-groups"); export const experimentsExperimentMetricGroupMutationV2Data = z.object({attributes: experimentsExperimentMetricGroupV2DTODataAttributes.optional(), id: z.string(), type: experimentsPatchExperimentMetricGroupV2RequestDataType}); export const experimentsPatchExperimentV2MetaDTOWarningsItems = z.object({code: z.string(), detail: z.string()}); export const experimentsPatchExperimentV2MetaDTO = z.object({needs_pipeline_refresh: z.boolean(), refresh_endpoint: z.string().optional(), warnings: z.array(experimentsPatchExperimentV2MetaDTOWarningsItems).optional()}); export const experimentsExperimentMetricGroupMutationV2 = z.object({data: experimentsExperimentMetricGroupMutationV2Data, meta: experimentsPatchExperimentV2MetaDTO.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 }; /** * Create experiment metric group * * POST /api/v2/experiments/{experiment_id}/metric-groups * * Also accepts auth: AuthZ */ export const createApiApiV2ExperimentsExperimentIdMetricGroups = async (args: CreateApiApiV2ExperimentsExperimentIdMetricGroupsArgs, options?: CreateApiApiV2ExperimentsExperimentIdMetricGroupsOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/experiments/${encodeURIComponent(String(args.experiment_id))}/metric-groups${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(experimentsExperimentMetricGroupMutationV2, await res.json()) };