import {z} from 'zod' export type ExperimentsCreateMetricCollectionV2RequestDataAttributesMetricsItems = {metric_id: string}; export type ExperimentsCreateMetricCollectionV2RequestDataAttributes = {description?: (string | null) | undefined, is_guardrail?: boolean | undefined, metrics?: Array | undefined, name: string}; export type ExperimentsPatchMetricCollectionV2RequestDataType = 'metric-collections'; export type ExperimentsCreateMetricCollectionV2RequestData = {attributes: ExperimentsCreateMetricCollectionV2RequestDataAttributes, id?: string | undefined, type: ExperimentsPatchMetricCollectionV2RequestDataType}; export type ExperimentsCreateMetricCollectionV2Request = {data: ExperimentsCreateMetricCollectionV2RequestData}; export type CreateApiApiV2ExperimentsMetricCollectionsArgs = {body: ExperimentsCreateMetricCollectionV2Request}; export type CreateApiApiV2ExperimentsMetricCollectionsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentsMetricCollectionV2DTODataAttributesMetricsItems = z.object({metric_id: z.string().optional(), metric_name: z.string().optional()}); export const experimentsMetricCollectionV2DTODataAttributes = z.object({created_at: z.string().optional(), description: z.string().nullable().optional(), is_guardrail: z.boolean().optional(), metric_count: z.number().int().optional(), metrics: z.array(experimentsMetricCollectionV2DTODataAttributesMetricsItems).optional(), name: z.string().optional(), updated_at: z.string().optional()}); export const experimentsPatchMetricCollectionV2RequestDataType = z.literal("metric-collections"); export const experimentsMetricCollectionV2DTOData = z.object({attributes: experimentsMetricCollectionV2DTODataAttributes.optional(), id: z.string(), type: experimentsPatchMetricCollectionV2RequestDataType}); export const experimentsMetricCollectionV2DTO = z.object({data: experimentsMetricCollectionV2DTOData}); 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 metric collection * * POST /api/v2/experiments/metric-collections * * Also accepts auth: AuthZ */ export const createApiApiV2ExperimentsMetricCollections = async (args: CreateApiApiV2ExperimentsMetricCollectionsArgs, options?: CreateApiApiV2ExperimentsMetricCollectionsOptions) => { 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/metric-collections${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(experimentsMetricCollectionV2DTO, await res.json()) };