import {z} from 'zod' export type ExperimentsCreateExposureSQLModelV2RequestDataAttributesItemsColumnType = 'STRING' | 'INTEGER' | 'FLOAT' | 'BOOLEAN' | 'DATE' | 'TIMESTAMP'; export type ExperimentsCreateMetricSQLModelV2RequestDataAttributesMeasuresItems = {column_name: string, column_type: ExperimentsCreateExposureSQLModelV2RequestDataAttributesItemsColumnType, description?: (string | null) | undefined, migration_metadata: unknown, name?: (string | null) | undefined}; export type ExperimentsMetricSQLModelPropertyInput = {column_name: string, column_type: ExperimentsCreateExposureSQLModelV2RequestDataAttributesItemsColumnType, description?: (string | null) | undefined, migration_metadata: unknown, name: string}; export type ExperimentsCreateExposureSQLModelV2RequestDataAttributesSubjectTypesItems = {column_name: string, subject_type_id: string}; export type ExperimentsUpdateMetricSQLModelV2RequestDataAttributes = {date_partition_column?: (string | null) | undefined, description?: (string | null) | undefined, measures?: Array | undefined, migration_metadata: unknown, name: string, properties?: Array | undefined, sql: string, subject_types: Array, timestamp_column: string}; export type ExperimentsUpdateMetricSQLModelV2RequestDataType = 'metric-sql-models'; export type ExperimentsCreateMetricSQLModelV2RequestData = {attributes: ExperimentsUpdateMetricSQLModelV2RequestDataAttributes, id?: string | undefined, type: ExperimentsUpdateMetricSQLModelV2RequestDataType}; export type ExperimentsCreateMetricSQLModelV2Request = {data: ExperimentsCreateMetricSQLModelV2RequestData}; export type CreateApiApiV2ExperimentsMetricSqlModelsArgs = {body: ExperimentsCreateMetricSQLModelV2Request}; export type CreateApiApiV2ExperimentsMetricSqlModelsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentsCreateExposureSQLModelV2RequestDataAttributesItemsColumnType = z.enum(["STRING", "INTEGER", "FLOAT", "BOOLEAN", "DATE", "TIMESTAMP"]); export const experimentsMetricSQLModelV2DTODataAttributesMeasuresItems = z.object({column_name: z.string().optional(), column_type: experimentsCreateExposureSQLModelV2RequestDataAttributesItemsColumnType.optional(), description: z.string().optional(), id: z.string().optional(), migration_metadata: z.unknown(), name: z.string().optional()}); export const experimentsMetricSQLModelV2DTODataAttributesSubjectTypesItems = z.object({column_name: z.string().optional(), subject_type_id: z.string().optional(), unique_subject_count_measure_id: z.string().optional()}); export const experimentsMetricSQLModelV2DTODataAttributes = z.object({certified_at: z.string().nullable().optional(), created_at: z.string().optional(), date_partition_column: z.string().nullable().optional(), description: z.string().nullable().optional(), event_count_measure_id: z.string().optional(), experiment_count: z.number().int().nullable().optional(), is_certified: z.boolean().optional(), measures: z.array(experimentsMetricSQLModelV2DTODataAttributesMeasuresItems).optional(), metric_count: z.number().int().nullable().optional(), migration_metadata: z.unknown(), name: z.string().optional(), properties: z.array(experimentsMetricSQLModelV2DTODataAttributesMeasuresItems).optional(), sql: z.string().optional(), subject_types: z.array(experimentsMetricSQLModelV2DTODataAttributesSubjectTypesItems).optional(), timestamp_column: z.string().optional(), updated_at: z.string().optional()}); export const experimentsUpdateMetricSQLModelV2RequestDataType = z.literal("metric-sql-models"); export const experimentsMetricSQLModelV2DTOData = z.object({attributes: experimentsMetricSQLModelV2DTODataAttributes.optional(), id: z.string(), type: experimentsUpdateMetricSQLModelV2RequestDataType}); export const experimentsMetricSQLModelV2DTO = z.object({data: experimentsMetricSQLModelV2DTOData}); 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 SQL model * * POST /api/v2/experiments/metric-sql-models * * Also accepts auth: AuthZ */ export const createApiApiV2ExperimentsMetricSqlModels = async (args: CreateApiApiV2ExperimentsMetricSqlModelsArgs, options?: CreateApiApiV2ExperimentsMetricSqlModelsOptions) => { 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-sql-models${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(experimentsMetricSQLModelV2DTO, await res.json()) };