import {z} from 'zod' export type JourneyJumpTarget = {journeyId: string, journeyStageId: string, journeyBundleId?: (string | null) | undefined, stageOrderIndex?: (number | null) | undefined, jumpReferenceId?: (string | null) | undefined}; export type JourneyCriteria = {name: string, description?: string | undefined, segmentId: string, target?: JourneyJumpTarget | undefined} | null; export type JourneyWaitStepConditionPath = {id: string, name?: string | undefined, segmentId?: (string | null) | undefined, timedOutPath?: boolean | undefined, moveAfter?: number | undefined, unit?: ('day' | 'week') | undefined, next?: string | undefined}; export type JourneyWaitStep = {type: 'WaitStep', name: string, waitStepType: 'Duration', waitStep: number, waitStepUnit: 'day' | 'week', next?: string | undefined} | {type: 'WaitStep', name: string, waitStepType: 'Date', waitUntilDate: string, timezone: string, next?: string | undefined} | {type: 'WaitStep', name: string, waitStepType: 'DaysOfTheWeek', waitUntilDaysOfTheWeek: Array, timezone: string, next?: string | undefined} | {type: 'WaitStep', waitStepType: 'Condition', conditions: Array, description?: (string | null) | undefined}; export type JourneyActivationStep = {type: 'Activation', name: string, journeyActivationStepId: string, next?: string | undefined}; export type JourneyDecisionPointBranch = {name: string, segmentId: string, excludedPath?: boolean | undefined, next?: string | undefined}; export type JourneyDecisionPointStep = {type: 'DecisionPoint', name: string, branches: Array}; export type JourneyMergeStep = {type: 'Merge', name?: string | undefined, next?: string | undefined}; export type JourneyJumpStep = {type: 'Jump', name: string, target: JourneyJumpTarget}; export type JourneyEndStep = {type?: 'End' | undefined, name?: string | undefined} | null; export type JourneyAbTestVariant = {id: string, name: string, percentage: number, next?: string | undefined, isControlGroup?: boolean | undefined, cappingAt?: number | undefined}; export type JourneyAbTestStep = {type: 'AbTest', name: string, customizedSplit: boolean, uniqueId?: string | undefined, randomizationMode?: ('sticky' | 'reentry' | 'periodic' | null) | undefined, periodicCadence?: ('weekly' | 'monthly' | 'yearly') | undefined, periodicBoundaryMonth?: number | undefined, periodicBoundaryDay?: number | undefined, periodicBoundaryDayOfWeek?: number | undefined, enabledCapping?: boolean | undefined, winnerVariantId?: (string | null) | undefined, variants: Array}; export type JourneyStep = {uuid?: (JourneyWaitStep | JourneyActivationStep | JourneyDecisionPointStep | JourneyMergeStep | JourneyJumpStep | JourneyEndStep | JourneyAbTestStep) | undefined} | null; export type JourneyStageJsonApiResource = {id?: string | undefined, name: string, description?: string | undefined, steps?: JourneyStep | undefined, rootStep?: string | undefined, entryCriteria?: JourneyCriteria | undefined, milestone?: JourneyCriteria | undefined, exitCriterias?: Array | undefined}; export type EntitiesJourneyUpdateRepresentation = {type?: 'journey' | undefined, attributes?: ({name: string, description?: (string | null) | undefined, state?: ('draft' | 'simulation' | 'launched') | undefined, goal?: JourneyCriteria | undefined, journeyStages?: Array | undefined, allowReentry?: boolean | undefined, reentryMode?: ('no_reentry' | 'reentry_unless_goal_achieved' | 'reentry_always') | undefined, journeyBundleId?: (string | null) | undefined, journeyBundleName?: (string | null) | undefined, journeyBundleDescription?: (string | null) | undefined}) | undefined}; export type CreateApiEntitiesJourneysJourneyIdAvailableBehaviorsForStepArgs = {journeyId: number, stepId?: string | undefined, body: EntitiesJourneyUpdateRepresentation}; export type CreateApiEntitiesJourneysJourneyIdAvailableBehaviorsForStepOptions = {baseUrl?: string | undefined, TdApikeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const columnType = z.enum(["string", "number", "timestamp", "date", "string_array", "number_array", "boolean"]); export const audienceBehaviorSchemaWithVisibility = z.object({name: z.string(), type: columnType, parentColumn: z.string().nullable(), matrixColumnName: z.string().optional(), matrixColumnDescription: z.string().nullable().optional(), visibility: z.enum(["clear", "pii", "non_pii", "blocked"]).optional()}); export const audienceBehaviorWithVisibilityJsonApiResource = z.object({id: z.string(), type: z.literal("audience-behavior"), attributes: z.object({audienceId: z.string(), name: z.string(), parentDatabaseName: z.string(), parentTableName: z.string(), parentKey: z.string(), foreignKey: z.string(), matrixDatabaseName: z.string(), matrixTableName: z.string(), allColumns: z.boolean(), defaultTimeFilterEnabled: z.boolean(), isRealtime: z.boolean(), schema: z.array(audienceBehaviorSchemaWithVisibility)})}); export const entitiesAudienceBehaviorIndexJsonApiResponse = z.object({data: z.array(audienceBehaviorWithVisibilityJsonApiResource)}); 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 a list of available behavior table of specified step * * POST /entities/journeys/{journeyId}/available_behaviors_for_step */ export const createApiEntitiesJourneysJourneyIdAvailableBehaviorsForStep = async (args: CreateApiEntitiesJourneysJourneyIdAvailableBehaviorsForStepArgs, options?: CreateApiEntitiesJourneysJourneyIdAvailableBehaviorsForStepOptions) => { 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/vnd.treasuredata.v1+json") headers.set('accept', "application/json") if (args.stepId != null) query.append("stepId", String(args.stepId)) if (TdApikeyAuth !== undefined) headers.set("Authorization", TdApikeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/entities/journeys/${encodeURIComponent(String(args.journeyId))}/available_behaviors_for_step${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(entitiesAudienceBehaviorIndexJsonApiResponse, await res.json()) };