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 CreateApiAudiencesAudienceIdSegmentsQueriesArgs = {audienceId: number, body: {columns?: Array | undefined, realtime?: boolean | undefined, kind?: number | undefined, rule?: PartialV5RuleYamlV5IonRule | undefined, funnel_stage?: ({funnel_id?: number | undefined, stage_id?: number | undefined}) | undefined, pageSize?: number | undefined, maxPage?: number | undefined, enableMv?: string | undefined, filterString?: string | undefined}}; export type CreateApiAudiencesAudienceIdSegmentsQueriesOptions = {baseUrl?: string | undefined, TdApikeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const segmentQueryStatus = z.object({id: z.string().optional(), queryId: z.string().optional(), status: z.enum(["success", "killed", "error", "running"]).optional(), count: z.number().int().nullable().optional(), maxPage: z.number().int().nullable().optional(), error: z.string().nullable().optional(), jobid: z.string().optional(), countJobid: z.string().nullable().optional(), customerGroupCount: z.number().int().nullable().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 }; /** * Retrieve number of profiles based on set of segment rules * * POST /audiences/{audienceId}/segments/queries */ export const createApiAudiencesAudienceIdSegmentsQueries = async (args: CreateApiAudiencesAudienceIdSegmentsQueriesArgs, options?: CreateApiAudiencesAudienceIdSegmentsQueriesOptions) => { const {baseUrl = "https://api-cdp.treasuredata.com", fetch: fetchFn = fetch, TdApikeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TdApikeyAuth !== undefined) headers.set("Authorization", TdApikeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/audiences/${encodeURIComponent(String(args.audienceId))}/segments/queries${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(segmentQueryStatus, await res.json()) };