import {z} from 'zod' export type GetApiEntitiesParentSegmentsParentSegmentIdRealtimeAttributesArgs = {'page[size]'?: number | undefined, 'page[after]'?: string | undefined, sorted_by?: string | undefined, types?: string | undefined, q?: string | undefined}; export type GetApiEntitiesParentSegmentsParentSegmentIdRealtimeAttributesOptions = {baseUrl?: string | undefined, TdApikeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const partialRealtimeJourneyYamlRealtimeAttributeDurationSetting = z.object({value: z.number().int(), unit: z.enum(["hour", "day", "minute"])}); export const partialRealtimeJourneyYamlRealtimeAttributeBackfillSetting = z.object({attributeIdentifier: z.string(), timestampIdentifier: z.string()}).nullable(); export const partialRealtimeJourneyYamlParentSegmentRealtimeSingleAttributeAttributes = z.object({name: z.string(), identifier: z.string(), description: z.string().nullable().optional(), type: z.literal("single"), notation: z.string(), realtimeKeyEventId: z.string(), sensitive: z.boolean().optional(), filterRule: z.object({}), valueColumn: z.string(), dataType: z.enum(["number", "string"]), duration: partialRealtimeJourneyYamlRealtimeAttributeDurationSetting.optional(), backfill: partialRealtimeJourneyYamlRealtimeAttributeBackfillSetting.optional()}); export const partialRealtimeJourneyYamlRealtimeListAttributeFields = z.array(z.string().min(1)); export const partialRealtimeJourneyYamlRealtimeListAttributeActions = z.array(z.object({action: z.enum(["delete_item", "clear"]), realtimeKeyEventId: z.string(), filterRule: z.object({})})); export const partialRealtimeJourneyYamlParentSegmentRealtimeListAttributeAttributes = z.object({name: z.string(), identifier: z.string(), description: z.string().nullable().optional(), type: z.literal("list"), notation: z.string().optional(), realtimeKeyEventId: z.string(), filterRule: z.object({}), idColumn: z.string(), fields: partialRealtimeJourneyYamlRealtimeListAttributeFields, duration: partialRealtimeJourneyYamlRealtimeAttributeDurationSetting, maxItems: z.number(), actions: partialRealtimeJourneyYamlRealtimeListAttributeActions, aggregations: z.array(z.object({name: z.string().optional(), identifier: z.string().optional(), aggregationType: z.enum(["first", "last", "sum", "min", "max", "distinct_list"]).optional(), column: z.string().optional(), notation: z.string()}))}); export const partialRealtimeJourneyYamlRealtimeCounterAttributeIncrementSettingConst = z.object({type: z.literal("const"), value: z.number().gte(1)}); export const partialRealtimeJourneyYamlRealtimeCounterAttributeIncrementSettingField = z.object({type: z.literal("field"), value: z.string()}); export const partialRealtimeJourneyYamlRealtimeCounterAttributeIncrementSetting = z.discriminatedUnion("type", [partialRealtimeJourneyYamlRealtimeCounterAttributeIncrementSettingConst, partialRealtimeJourneyYamlRealtimeCounterAttributeIncrementSettingField]); export const partialRealtimeJourneyYamlParentSegmentRealtimeCounterAttributeAttributes = z.object({name: z.string(), identifier: z.string(), description: z.string().nullable().optional(), type: z.literal("counter"), counterType: z.enum(["sliding", "total"]), increment: partialRealtimeJourneyYamlRealtimeCounterAttributeIncrementSetting, notation: z.string(), realtimeKeyEventId: z.string(), filterRule: z.object({}), duration: partialRealtimeJourneyYamlRealtimeAttributeDurationSetting, backfill: partialRealtimeJourneyYamlRealtimeAttributeBackfillSetting, subDurations: z.array(z.object({value: z.number().int(), unit: z.enum(["hour", "day", "minute"]), name: z.string(), identifier: z.string(), notation: z.string()}))}); export const columnType = z.enum(["string", "number", "timestamp", "date", "string_array", "number_array", "boolean"]); export const partialRealtimeJourneyYamlParentSegmentRealtimeImportedAttributeAttributes = z.object({name: z.string().nullable().optional(), description: z.string().nullable().optional(), realtimeAttributeKind: z.literal("batch"), type: z.literal("imported").optional(), notation: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), column: z.string(), identifier: z.string().nullable().optional(), dataType: z.union([columnType, z.string().nullable()]), sensitive: z.boolean().optional()}); export const partialRealtimeJourneyYamlParentSegmentRealtimeLookupCatalogAttributeAttributes = z.object({name: z.string(), identifier: z.string(), description: z.string().nullable().optional(), type: z.literal("lookupCatalog"), notation: z.string(), lookupCatalogTableName: z.string(), lookupCatalogColumns: z.array(z.object({name: z.string(), dataType: z.string()})), lookupKeys: z.array(z.object({id: z.string(), type: z.string()}))}); export const partialRealtimeJourneyYamlParentSegmentRealtimeAttributeAttributes = z.discriminatedUnion("type", [partialRealtimeJourneyYamlParentSegmentRealtimeSingleAttributeAttributes, partialRealtimeJourneyYamlParentSegmentRealtimeListAttributeAttributes, partialRealtimeJourneyYamlParentSegmentRealtimeCounterAttributeAttributes, partialRealtimeJourneyYamlParentSegmentRealtimeImportedAttributeAttributes, partialRealtimeJourneyYamlParentSegmentRealtimeLookupCatalogAttributeAttributes]); export const getApiEntitiesParentSegmentsParentSegmentIdRealtimeAttributesResponse = z.object({data: z.array(z.object({id: z.string().optional(), type: z.literal("realtime-attribute").optional(), attributes: partialRealtimeJourneyYamlParentSegmentRealtimeAttributeAttributes.optional()})), pagination: z.object({nextPage: z.string().nullable()})}); 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 }; /** * List available realtime attributes * * GET /entities/parent_segments/{parentSegmentId}/realtime_attributes */ export const getApiEntitiesParentSegmentsParentSegmentIdRealtimeAttributes = async (args: GetApiEntitiesParentSegmentsParentSegmentIdRealtimeAttributesArgs, options?: GetApiEntitiesParentSegmentsParentSegmentIdRealtimeAttributesOptions) => { const {baseUrl = "https://api-cdp.treasuredata.com", fetch: fetchFn = fetch, TdApikeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args.sorted_by != null) query.append("sorted_by", String(args.sorted_by)) if (args.types != null) query.append("types", String(args.types)) if (args.q != null) query.append("q", String(args.q)) if (TdApikeyAuth !== undefined) headers.set("Authorization", TdApikeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/entities/parent_segments/${encodeURIComponent(String(args.parentSegmentId))}/realtime_attributes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiEntitiesParentSegmentsParentSegmentIdRealtimeAttributesResponse, await res.json()) };