import {z} from 'zod' export type PartialV5RuleYamlV5IonRuleLocalDuration = {day?: number | undefined, hour?: number | undefined, minute?: number | undefined, month?: number | undefined, quarter?: number | undefined, second?: number | undefined, week?: number | undefined, year?: number | undefined}; export type PartialV5RuleYamlV5IonRuleUnit = 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second'; export type PartialV5RuleYamlV5IonRuleDuration = {year: number} | {quarter: number} | {month: number} | {week: number} | {day: number} | {hour: number} | {minute: number} | {second: number}; export type PartialV5RuleYamlV5IonRuleValueOperator = {not: boolean, rightValue: {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'GreaterEqual'} | {duration: PartialV5RuleYamlV5IonRuleDuration, from: {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, not: boolean, reverse: boolean, type: 'TimeRange'} | {from: {unit: PartialV5RuleYamlV5IonRuleUnit}, not: false, type: 'TimeRange'} | {type: 'TimeToday'} | {from: {unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'TimeThis'} | {from: {unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'TimeNext'} | {type: 'TimeWithinNext', unit: PartialV5RuleYamlV5IonRuleUnit, value: string} | {type: 'TimeWithinPast', unit: PartialV5RuleYamlV5IonRuleUnit, value: string} | {from: {next: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, not: false, to: {next: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'TimeInBetweenNext'} | {not: boolean, rightValue: {timestamp: string}, type: 'Equal'} | {not: false, rightValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'Greater'} | {not: false, rightValue: {timestamp: string}, type: 'GreaterEqual'} | {not: false, rightValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'Less'} | {not: false, rightValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'LessEqual'} | {maxValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, minValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, not: boolean, type: 'Between'} | {not: boolean, rightValue: number | string | boolean, type: 'Equal'} | {not: boolean, rightValue: number | string | boolean, type: 'IsNull'} | {not: boolean, rightValues: Array, type: 'In'} | {not: boolean, rightValue: number | string | boolean, type: 'Greater'} | {not: boolean, rightValue: number | string | boolean, type: 'Less'} | {not: boolean, rightValue: number | string | boolean, type: 'GreaterEqual'} | {not: boolean, rightValue: number | string | boolean, type: 'LessEqual'} | {maxValue: number | string, minValue: number | string, not: boolean, rightValue: number, type: 'Between'} | {not: boolean, rightValues: Array, type: 'Contain'} | {not: boolean, rightValues: Array, type: 'StartWith'} | {not: boolean, rightValues: Array, type: 'EndWith'} | {not: boolean, rightValue: string, type: 'Regexp'}; export type PartialV5RuleYamlV5IonRuleReferenceCondition = {exclude: boolean, id: string, type: 'FunnelStageReference' | 'Reference' | 'AbTestVariantReference'} | {id: string, includeConvertedCustomers: boolean, maxGrade: number, minGrade: number, type: 'PredictiveScoreReference'} | {id: string, leftValue: {attribute: 'r_quartile' | 'f_quartile' | 'm_quartile' | 'rfm_segment' | 'rfm_score' | 'rfm_cluster_rank' | 'rfm_quartile'}, operator: PartialV5RuleYamlV5IonRuleValueOperator, solutionType: 'rfm', type: 'MlAttributeReference'} | {id: string, leftValue: {attribute: 'cltv_1m' | 'cltv_1m_pctile' | 'cltv_3m' | 'cltv_3m_pctile' | 'cltv_6m' | 'cltv_6m_pctile' | 'cltv_12m' | 'cltv_12m_pctile' | 'cltv_24m' | 'cltv_24m_pctile' | 'alive_prob' | 'cltv_segment'}, operator: PartialV5RuleYamlV5IonRuleValueOperator, solutionType: 'cltv', type: 'MlAttributeReference'}; export type PartialV5RuleYamlV5IonRuleValueArrayMatching = unknown | 'all' | 'any' | {atLeast: number} | {atMost: number} | {exactly: number}; export type PartialV5RuleYamlV5IonRuleAttributeNameValue = {name: string}; export type PartialV5RuleYamlV5IonRuleCustomerGroupAttributeValue = {customerGroup: string, name: string}; export type PartialV5RuleYamlV5IonRuleTimeframeOperator = {not: boolean, rightValue: {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'GreaterEqual'} | {duration: PartialV5RuleYamlV5IonRuleDuration, from: {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, not: boolean, reverse: boolean, type: 'TimeRange'} | {from: {unit: PartialV5RuleYamlV5IonRuleUnit}, not: false, type: 'TimeRange'} | {type: 'TimeToday'} | {from: {unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'TimeThis'} | {from: {unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'TimeNext'} | {type: 'TimeWithinNext', unit: PartialV5RuleYamlV5IonRuleUnit, value: string} | {type: 'TimeWithinPast', unit: PartialV5RuleYamlV5IonRuleUnit, value: string} | {from: {next: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, not: false, to: {next: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'TimeInBetweenNext'} | {not: boolean, rightValue: {timestamp: string}, type: 'Equal'} | {not: false, rightValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'Greater'} | {not: false, rightValue: {timestamp: string}, type: 'GreaterEqual'} | {not: false, rightValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'Less'} | {not: false, rightValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, type: 'LessEqual'} | {maxValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, minValue: {timestamp: string} | {last: number | PartialV5RuleYamlV5IonRuleLocalDuration, unit: PartialV5RuleYamlV5IonRuleUnit}, not: boolean, type: 'Between'}; export type PartialV5RuleYamlV5IonRuleColumnFilter = {arrayMatching: PartialV5RuleYamlV5IonRuleValueArrayMatching, column: string, operator: PartialV5RuleYamlV5IonRuleValueOperator, type: 'Column'} | {arrayMatching: PartialV5RuleYamlV5IonRuleValueArrayMatching, column: 'timestamp', operator: PartialV5RuleYamlV5IonRuleTimeframeOperator, type: 'Column'}; export type PartialV5RuleYamlV5IonRuleOrFilter = {conditions: Array, type: 'Or'}; export type PartialV5RuleYamlV5IonRuleAndFilter = {conditions: Array, type: 'And'}; export type PartialV5RuleYamlV5IonRuleFilter = Record | null | PartialV5RuleYamlV5IonRuleAndFilter | PartialV5RuleYamlV5IonRuleOrFilter; export type PartialV5RuleYamlV5IonRuleAggregationValue = {aggregation: {column: string, groupingColumns?: Array | undefined, type: 'Sum' | 'Min' | 'Max' | 'Average'} | {groupingColumns: Array, type: 'Count'} | {columns: Array, type: 'CountDistinct'} | {column: string, groupingColumns?: Array | undefined, orderColumn: string, type: 'First' | 'Last'}, filter: PartialV5RuleYamlV5IonRuleFilter, source: {name: string}, window?: ({duration: {day?: number | undefined, hour?: number | undefined, minute?: number | undefined, second?: number | undefined}, type: 'TimeSliding'}) | undefined}; export type PartialV5RuleYamlV5IonRuleValueLeftValue = PartialV5RuleYamlV5IonRuleAttributeNameValue | PartialV5RuleYamlV5IonRuleCustomerGroupAttributeValue | PartialV5RuleYamlV5IonRuleAggregationValue; export type PartialV5RuleYamlV5IonRuleValueLimit = {number: number, orderColumns?: (Array<{column: string, order: 'ascending' | 'descending', type: 'Column'} | {order: 'ascending' | 'descending', type: 'Aggregation'}>) | undefined, type: 'Count' | 'Percent'}; export type PartialV5RuleYamlV5IonRuleValueCondition = {arrayMatching: PartialV5RuleYamlV5IonRuleValueArrayMatching, exclude: boolean, leftValue: PartialV5RuleYamlV5IonRuleValueLeftValue, limit?: PartialV5RuleYamlV5IonRuleValueLimit | undefined, operator: PartialV5RuleYamlV5IonRuleValueOperator, type: 'Value'}; export type PartialV5RuleYamlV5IonRuleValueConditionSet = {conditions: Array, description?: string | undefined, expr?: string | undefined, type: 'Or' | 'And' | 'Composite'}; export type PartialV5RuleYamlV5IonRuleIonRule = {conditions: Array, expr?: string | undefined, limit?: PartialV5RuleYamlV5IonRuleValueLimit | undefined, type: 'Or' | 'And' | 'Composite'}; export type PartialV5RuleYamlV5IonRule = PartialV5RuleYamlV5IonRuleIonRule | Record | null; export type RelationshipsFolderJsonApiResource = {data?: {id: string, type: 'folder-segment'} | undefined}; export type EntitiesSegmentUpdateRepresentation = {id?: string | undefined, type?: ('segment-batch' | 'segment-realtime' | 'segment-funnel-stage') | undefined, attributes?: ({name?: string | undefined, description?: (string | null) | undefined, rule?: PartialV5RuleYamlV5IonRule | undefined, funnel_stage?: ({funnel_id?: number | undefined, stage_id?: number | undefined}) | undefined, segmentInsightDashboardId?: (number | null) | undefined}) | undefined, relationships?: ({parentFolder?: RelationshipsFolderJsonApiResource | undefined}) | undefined}; export type CreateApiEntitiesSegmentsArgs = {body: EntitiesSegmentUpdateRepresentation}; export type CreateApiEntitiesSegmentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const partialV5RuleYamlV5IonRuleLocalDuration = z.object({day: z.number().optional(), hour: z.number().optional(), minute: z.number().optional(), month: z.number().optional(), quarter: z.number().optional(), second: z.number().optional(), week: z.number().optional(), year: z.number().optional()}); export const partialV5RuleYamlV5IonRuleUnit = z.enum(["year", "quarter", "month", "week", "day", "hour", "minute", "second"]); export const partialV5RuleYamlV5IonRuleDuration = z.union([z.object({year: z.number()}), z.object({quarter: z.number()}), z.object({month: z.number()}), z.object({week: z.number()}), z.object({day: z.number()}), z.object({hour: z.number()}), z.object({minute: z.number()}), z.object({second: z.number()})]); export const partialV5RuleYamlV5IonRuleValueOperator = z.union([z.object({not: z.boolean(), rightValue: z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("GreaterEqual")}), z.union([z.object({duration: partialV5RuleYamlV5IonRuleDuration, from: z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), not: z.boolean(), reverse: z.boolean(), type: z.literal("TimeRange")}), z.object({from: z.object({unit: partialV5RuleYamlV5IonRuleUnit}), not: z.literal(false), type: z.literal("TimeRange")}), z.object({type: z.literal("TimeToday")}), z.object({from: z.object({unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("TimeThis")}), z.object({from: z.object({unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("TimeNext")}), z.object({type: z.literal("TimeWithinNext"), unit: partialV5RuleYamlV5IonRuleUnit, value: z.string()}), z.object({type: z.literal("TimeWithinPast"), unit: partialV5RuleYamlV5IonRuleUnit, value: z.string()}), z.object({from: z.object({next: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), not: z.literal(false), to: z.object({next: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("TimeInBetweenNext")})]), z.object({not: z.boolean(), rightValue: z.object({timestamp: z.string()}), type: z.literal("Equal")}), z.object({not: z.literal(false), rightValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), type: z.literal("Greater")}), z.object({not: z.literal(false), rightValue: z.object({timestamp: z.string()}), type: z.literal("GreaterEqual")}), z.object({not: z.literal(false), rightValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), type: z.literal("Less")}), z.object({not: z.literal(false), rightValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), type: z.literal("LessEqual")}), z.object({maxValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), minValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), not: z.boolean(), type: z.literal("Between")}), z.object({not: z.boolean(), rightValue: z.union([z.number(), z.string(), z.boolean()]), type: z.literal("Equal")}), z.object({not: z.boolean(), rightValue: z.union([z.number(), z.string(), z.boolean()]), type: z.literal("IsNull")}), z.object({not: z.boolean(), rightValues: z.array(z.union([z.number(), z.string()])), type: z.literal("In")}), z.object({not: z.boolean(), rightValue: z.union([z.number(), z.string(), z.boolean()]), type: z.literal("Greater")}), z.object({not: z.boolean(), rightValue: z.union([z.number(), z.string(), z.boolean()]), type: z.literal("Less")}), z.object({not: z.boolean(), rightValue: z.union([z.number(), z.string(), z.boolean()]), type: z.literal("GreaterEqual")}), z.object({not: z.boolean(), rightValue: z.union([z.number(), z.string(), z.boolean()]), type: z.literal("LessEqual")}), z.object({maxValue: z.union([z.number(), z.string()]), minValue: z.union([z.number(), z.string()]), not: z.boolean(), rightValue: z.number(), type: z.literal("Between")}), z.object({not: z.boolean(), rightValues: z.array(z.string()), type: z.literal("Contain")}), z.object({not: z.boolean(), rightValues: z.array(z.string()), type: z.literal("StartWith")}), z.object({not: z.boolean(), rightValues: z.array(z.string()), type: z.literal("EndWith")}), z.object({not: z.boolean(), rightValue: z.string(), type: z.literal("Regexp")})]); export const partialV5RuleYamlV5IonRuleReferenceCondition = z.union([z.object({exclude: z.boolean(), id: z.string(), type: z.enum(["FunnelStageReference", "Reference", "AbTestVariantReference"])}), z.object({id: z.string(), includeConvertedCustomers: z.boolean(), maxGrade: z.number(), minGrade: z.number(), type: z.literal("PredictiveScoreReference")}), z.object({id: z.string(), leftValue: z.object({attribute: z.enum(["r_quartile", "f_quartile", "m_quartile", "rfm_segment", "rfm_score", "rfm_cluster_rank", "rfm_quartile"])}), operator: partialV5RuleYamlV5IonRuleValueOperator, solutionType: z.literal("rfm"), type: z.literal("MlAttributeReference")}), z.object({id: z.string(), leftValue: z.object({attribute: z.enum(["cltv_1m", "cltv_1m_pctile", "cltv_3m", "cltv_3m_pctile", "cltv_6m", "cltv_6m_pctile", "cltv_12m", "cltv_12m_pctile", "cltv_24m", "cltv_24m_pctile", "alive_prob", "cltv_segment"])}), operator: partialV5RuleYamlV5IonRuleValueOperator, solutionType: z.literal("cltv"), type: z.literal("MlAttributeReference")})]); export const partialV5RuleYamlV5IonRuleValueArrayMatching = z.union([z.unknown(), z.enum(["all", "any"]), z.object({atLeast: z.number()}), z.object({atMost: z.number()}), z.object({exactly: z.number()})]); export const partialV5RuleYamlV5IonRuleAttributeNameValue = z.object({name: z.string()}); export const partialV5RuleYamlV5IonRuleCustomerGroupAttributeValue = z.object({customerGroup: z.string(), name: z.string()}); export const partialV5RuleYamlV5IonRuleTimeframeOperator = z.union([z.object({not: z.boolean(), rightValue: z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("GreaterEqual")}), z.union([z.object({duration: partialV5RuleYamlV5IonRuleDuration, from: z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), not: z.boolean(), reverse: z.boolean(), type: z.literal("TimeRange")}), z.object({from: z.object({unit: partialV5RuleYamlV5IonRuleUnit}), not: z.literal(false), type: z.literal("TimeRange")}), z.object({type: z.literal("TimeToday")}), z.object({from: z.object({unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("TimeThis")}), z.object({from: z.object({unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("TimeNext")}), z.object({type: z.literal("TimeWithinNext"), unit: partialV5RuleYamlV5IonRuleUnit, value: z.string()}), z.object({type: z.literal("TimeWithinPast"), unit: partialV5RuleYamlV5IonRuleUnit, value: z.string()}), z.object({from: z.object({next: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), not: z.literal(false), to: z.object({next: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit}), type: z.literal("TimeInBetweenNext")})]), z.object({not: z.boolean(), rightValue: z.object({timestamp: z.string()}), type: z.literal("Equal")}), z.object({not: z.literal(false), rightValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), type: z.literal("Greater")}), z.object({not: z.literal(false), rightValue: z.object({timestamp: z.string()}), type: z.literal("GreaterEqual")}), z.object({not: z.literal(false), rightValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), type: z.literal("Less")}), z.object({not: z.literal(false), rightValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), type: z.literal("LessEqual")}), z.object({maxValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), minValue: z.union([z.object({timestamp: z.string()}), z.object({last: z.union([z.number(), partialV5RuleYamlV5IonRuleLocalDuration]), unit: partialV5RuleYamlV5IonRuleUnit})]), not: z.boolean(), type: z.literal("Between")})]); export const partialV5RuleYamlV5IonRuleColumnFilter = z.union([z.object({arrayMatching: partialV5RuleYamlV5IonRuleValueArrayMatching, column: z.string(), operator: partialV5RuleYamlV5IonRuleValueOperator, type: z.literal("Column")}), z.object({arrayMatching: partialV5RuleYamlV5IonRuleValueArrayMatching, column: z.literal("timestamp"), operator: partialV5RuleYamlV5IonRuleTimeframeOperator, type: z.literal("Column")})]); export const partialV5RuleYamlV5IonRuleOrFilter: z.ZodType = z.object({conditions: z.array(z.union([z.lazy(() => partialV5RuleYamlV5IonRuleAndFilter), z.lazy(() => partialV5RuleYamlV5IonRuleOrFilter), partialV5RuleYamlV5IonRuleColumnFilter])), type: z.literal("Or")}); export const partialV5RuleYamlV5IonRuleAndFilter: z.ZodType = z.object({conditions: z.array(z.union([z.lazy(() => partialV5RuleYamlV5IonRuleAndFilter), partialV5RuleYamlV5IonRuleOrFilter, partialV5RuleYamlV5IonRuleColumnFilter])), type: z.literal("And")}); export const partialV5RuleYamlV5IonRuleFilter = z.union([z.object({}).nullable(), partialV5RuleYamlV5IonRuleAndFilter, partialV5RuleYamlV5IonRuleOrFilter]); export const partialV5RuleYamlV5IonRuleAggregationValue = z.object({aggregation: z.union([z.object({column: z.string(), groupingColumns: z.array(z.string()).optional(), type: z.enum(["Sum", "Min", "Max", "Average"])}), z.object({groupingColumns: z.array(z.string()), type: z.literal("Count")}), z.object({columns: z.array(z.string()), type: z.literal("CountDistinct")}), z.object({column: z.string(), groupingColumns: z.array(z.string()).optional(), orderColumn: z.string(), type: z.enum(["First", "Last"])})]), filter: partialV5RuleYamlV5IonRuleFilter, source: z.object({name: z.string()}), window: z.object({duration: z.object({day: z.number().optional(), hour: z.number().optional(), minute: z.number().optional(), second: z.number().optional()}), type: z.literal("TimeSliding")}).optional()}); export const partialV5RuleYamlV5IonRuleValueLeftValue = z.union([partialV5RuleYamlV5IonRuleAttributeNameValue, partialV5RuleYamlV5IonRuleCustomerGroupAttributeValue, partialV5RuleYamlV5IonRuleAggregationValue]); export const partialV5RuleYamlV5IonRuleValueLimit = z.object({number: z.number(), orderColumns: z.array(z.union([z.object({column: z.string(), order: z.enum(["ascending", "descending"]), type: z.literal("Column")}), z.object({order: z.enum(["ascending", "descending"]), type: z.literal("Aggregation")})])).optional(), type: z.enum(["Count", "Percent"])}); export const partialV5RuleYamlV5IonRuleValueCondition = z.object({arrayMatching: partialV5RuleYamlV5IonRuleValueArrayMatching, exclude: z.boolean(), leftValue: partialV5RuleYamlV5IonRuleValueLeftValue, limit: partialV5RuleYamlV5IonRuleValueLimit.optional(), operator: partialV5RuleYamlV5IonRuleValueOperator, type: z.literal("Value")}); export const partialV5RuleYamlV5IonRuleValueConditionSet = z.object({conditions: z.array(z.union([partialV5RuleYamlV5IonRuleReferenceCondition, partialV5RuleYamlV5IonRuleValueCondition])), description: z.string().optional(), expr: z.string().optional(), type: z.enum(["Or", "And", "Composite"])}); export const partialV5RuleYamlV5IonRuleIonRule = z.object({conditions: z.array(partialV5RuleYamlV5IonRuleValueConditionSet), expr: z.string().optional(), limit: partialV5RuleYamlV5IonRuleValueLimit.optional(), type: z.enum(["Or", "And", "Composite"])}); export const partialV5RuleYamlV5IonRule = z.union([partialV5RuleYamlV5IonRuleIonRule, z.object({}).nullable()]); export const relationshipsFolderJsonApiResource = z.object({data: z.object({id: z.string(), type: z.literal("folder-segment")}).optional()}); export const relationshipsUserJsonApiResource = z.object({data: z.object({id: z.string(), type: z.literal("user")}).nullable().optional()}); export const batchSegmentJsonApiResource = z.object({id: z.string(), type: z.literal("segment-batch"), attributes: z.object({audienceId: z.string(), name: z.string(), description: z.string().nullable(), population: z.number().nullable(), numSyndications: z.number().int().optional(), rule: partialV5RuleYamlV5IonRule, createdAt: z.string(), updatedAt: z.string(), countPopulation: z.boolean().optional(), segmentInsightDashboardId: z.number().int().nullable().optional(), isEmptyRule: z.boolean().optional()}), relationships: z.object({parentFolder: relationshipsFolderJsonApiResource.optional(), createdBy: relationshipsUserJsonApiResource.optional(), updatedBy: relationshipsUserJsonApiResource.optional()})}); export const realtimeSegmentJsonApiResource = z.object({id: z.string(), type: z.literal("segment-realtime"), attributes: z.object({audienceId: z.string(), name: z.string(), description: z.string().nullable(), population: z.number().nullable(), numSyndications: z.number().int().nullable().optional(), rule: partialV5RuleYamlV5IonRule, createdAt: z.string(), updatedAt: z.string(), countPopulation: z.boolean().optional()}), relationships: z.object({parentFolder: relationshipsFolderJsonApiResource.optional(), createdBy: relationshipsUserJsonApiResource.optional(), updatedBy: relationshipsUserJsonApiResource.optional()})}); export const funnelStageSegmentJsonApiResource = z.object({id: z.string(), type: z.literal("segment-funnel-stage"), attributes: z.object({audienceId: z.string(), name: z.string(), description: z.string().nullable(), population: z.number().nullable(), numSyndications: z.number().int().optional(), rule: partialV5RuleYamlV5IonRule, createdAt: z.string(), updatedAt: z.string(), countPopulation: z.boolean().optional()}), relationships: z.object({parentFolder: relationshipsFolderJsonApiResource.optional(), createdBy: relationshipsUserJsonApiResource.optional(), updatedBy: relationshipsUserJsonApiResource.optional()})}); export const userJsonApiResource = z.object({id: z.string(), type: z.literal("user"), attributes: z.object({tdUserId: z.string(), name: z.string()})}); export const entitiesGetSegmentJsonApiResponse = z.object({data: z.union([batchSegmentJsonApiResource, realtimeSegmentJsonApiResource, funnelStageSegmentJsonApiResource]), included: z.array(userJsonApiResource)}); 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 segment * * POST /entities/segments */ export const createApiEntitiesSegments = async (args: CreateApiEntitiesSegmentsArgs, options?: CreateApiEntitiesSegmentsOptions) => { const {baseUrl = "https://api-cdp.treasuredata.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.treasuredata.v1+json") headers.set('accept', "application/vnd.treasuredata.v1+json") const url = `${baseUrl.replace(/\/+$/, '')}/entities/segments${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(entitiesGetSegmentJsonApiResponse, await res.json()) };