import {z} from 'zod' export type CreateApiApiCdpSettingsV1AppAppIdAudienceArgs = {app_id: string, body: {name: string, description: string, membership_duration_days: number, wait?: ({seconds: number} | {minutes: number} | {hours: number} | {days: number}) | undefined, definition: {operator: 'and', conditions: Array<{operator: 'or', conditions: Array<{condition_type: 'profile', column_id: string, column_meta?: ({column_name?: string | undefined, column_type?: ('number' | 'string' | 'ipv4' | 'ipv6' | 'bool' | 'int' | 'uuid' | 'hex' | 'datetime') | undefined, column_unit?: ('currency' | 'url' | 'days' | 'pixels' | 'none' | 'millisecond' | '') | undefined, column_category?: Array | undefined, analytics_column_id?: string | undefined, analytics_transformation_id?: 'sum' | undefined, value_selectors?: (Array<'first' | 'last' | 'any' | 'none_of'>) | undefined, extractions?: (Array<'first' | 'last' | 'all'>) | undefined, aggregation?: ('first' | 'last' | 'first_last' | 'list' | 'list_unique' | 'min' | 'max' | 'sum') | undefined, scope?: ('event' | 'profile') | undefined}) | undefined, value_selector?: ('first' | 'last' | 'any' | 'none' | 'none_of') | undefined, condition: {operator: 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'contains' | 'not_contains' | 'icontains' | 'not_icontains' | 'starts_with' | 'ends_with' | 'matches' | 'not_matches' | 'before_last' | 'in_the_last' | 'in_the_next' | 'after_next' | 'earlier_than' | 'later_than', value: string | number | boolean | {unit?: ('seconds' | 'minutes' | 'hours' | 'days') | undefined, value?: number | undefined} | {unit?: 'datetime' | undefined, value?: unknown | undefined}, label?: string | undefined}} | {condition_type: 'event', times: {operator: 'eq' | 'gt' | 'gte' | 'lt' | 'lte', value: number}, during?: ({seconds: number, unit: 'minutes'} | {seconds: number, unit: 'hours'} | {seconds: number, unit: 'days'}) | undefined, condition: {operator: 'and', conditions: Array<{column_id: string, column_meta?: ({column_name?: string | undefined, column_type?: ('number' | 'string' | 'ipv4' | 'ipv6' | 'bool' | 'int' | 'uuid' | 'hex' | 'datetime') | undefined, column_unit?: ('currency' | 'url' | 'days' | 'pixels' | 'none' | 'millisecond' | '') | undefined, column_category?: Array | undefined, analytics_column_id?: string | undefined, analytics_transformation_id?: 'sum' | undefined, value_selectors?: (Array<'first' | 'last' | 'any' | 'none_of'>) | undefined, extractions?: (Array<'first' | 'last' | 'all'>) | undefined, aggregation?: ('first' | 'last' | 'first_last' | 'list' | 'list_unique' | 'min' | 'max' | 'sum') | undefined, scope?: ('event' | 'profile') | undefined}) | undefined, condition: {operator: 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'contains' | 'not_contains' | 'icontains' | 'not_icontains' | 'starts_with' | 'ends_with' | 'matches' | 'not_matches' | 'before_last' | 'in_the_last' | 'in_the_next' | 'after_next' | 'earlier_than' | 'later_than', value: string | number | boolean}}>}}>}>} | {operator: 'and', conditions: Array}}}; export type CreateApiApiCdpSettingsV1AppAppIdAudienceOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiCdpSettingsV1AppAppIdAudienceResponse = z.object({id: z.string(), app_id: z.string(), created_at: z.string(), updated_at: z.string(), name: z.string(), description: z.string(), membership_duration_days: z.number().int(), wait: z.union([z.object({seconds: z.number().int().gte(0).lte(2592000)}), z.object({minutes: z.number().int().gte(0).lte(43200)}), z.object({hours: z.number().int().gte(0).lte(720)}), z.object({days: z.number().int().gte(0).lte(30)})]), definition: z.union([z.object({operator: z.literal("and"), conditions: z.array(z.object({operator: z.literal("or"), conditions: z.array(z.union([z.object({condition_type: z.literal("profile"), column_id: z.string(), column_meta: z.object({column_name: z.string().optional(), column_type: z.enum(["number", "string", "ipv4", "ipv6", "bool", "int", "uuid", "hex", "datetime"]).optional(), column_unit: z.enum(["currency", "url", "days", "pixels", "none", "millisecond", ""]).optional(), column_category: z.array(z.string()).optional(), analytics_column_id: z.string().optional(), analytics_transformation_id: z.literal("sum").optional(), value_selectors: z.array(z.enum(["first", "last", "any", "none_of"])).optional(), extractions: z.array(z.enum(["first", "last", "all"])).optional(), aggregation: z.enum(["first", "last", "first_last", "list", "list_unique", "min", "max", "sum"]).optional(), scope: z.enum(["event", "profile"]).optional()}).optional(), value_selector: z.enum(["first", "last", "any", "none", "none_of"]).optional(), condition: z.object({operator: z.enum(["eq", "neq", "gt", "gte", "lt", "lte", "contains", "not_contains", "icontains", "not_icontains", "starts_with", "ends_with", "matches", "not_matches", "before_last", "in_the_last", "in_the_next", "after_next", "earlier_than", "later_than"]), value: z.union([z.string(), z.number(), z.boolean(), z.object({unit: z.enum(["seconds", "minutes", "hours", "days"]).optional(), value: z.number().int().optional()}), z.object({unit: z.literal("datetime").optional(), value: z.string().optional()})]), label: z.string().optional()})}), z.object({condition_type: z.literal("event"), times: z.object({operator: z.enum(["eq", "gt", "gte", "lt", "lte"]), value: z.number().int().gte(0).lte(50)}), during: z.union([z.object({seconds: z.number().int().gte(60).lte(2592000).multipleOf(60), unit: z.literal("minutes")}), z.object({seconds: z.number().int().gte(3600).lte(2592000).multipleOf(3600), unit: z.literal("hours")}), z.object({seconds: z.number().int().gte(86400).lte(2592000).multipleOf(86400), unit: z.literal("days")})]).optional(), condition: z.object({operator: z.literal("and"), conditions: z.array(z.object({column_id: z.string(), column_meta: z.object({column_name: z.string().optional(), column_type: z.enum(["number", "string", "ipv4", "ipv6", "bool", "int", "uuid", "hex", "datetime"]).optional(), column_unit: z.enum(["currency", "url", "days", "pixels", "none", "millisecond", ""]).optional(), column_category: z.array(z.string()).optional(), analytics_column_id: z.string().optional(), analytics_transformation_id: z.literal("sum").optional(), value_selectors: z.array(z.enum(["first", "last", "any", "none_of"])).optional(), extractions: z.array(z.enum(["first", "last", "all"])).optional(), aggregation: z.enum(["first", "last", "first_last", "list", "list_unique", "min", "max", "sum"]).optional(), scope: z.enum(["event", "profile"]).optional()}).optional(), condition: z.object({operator: z.enum(["eq", "neq", "gt", "gte", "lt", "lte", "contains", "not_contains", "icontains", "not_icontains", "starts_with", "ends_with", "matches", "not_matches", "before_last", "in_the_last", "in_the_next", "after_next", "earlier_than", "later_than"]), value: z.union([z.string(), z.number(), z.boolean()])})}))})})]))}))}), z.object({operator: z.literal("and"), conditions: z.array(z.unknown())})]), type: z.enum(["standard", "sequential"]), author: z.object({email: z.string()}), is_author: z.boolean(), version: z.number().int().gte(1)}); 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 audience * * POST /api/cdp/settings/v1/app/{app_id}/audience */ export const createApiApiCdpSettingsV1AppAppIdAudience = async (args: CreateApiApiCdpSettingsV1AppAppIdAudienceArgs, options?: CreateApiApiCdpSettingsV1AppAppIdAudienceOptions) => { const {baseUrl = "https://example.piwik.pro", fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/cdp/settings/v1/app/${encodeURIComponent(String(args.app_id))}/audience${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(createApiApiCdpSettingsV1AppAppIdAudienceResponse, await res.json()) };