import {z} from 'zod' export type CreateApiApiCdpSettingsV1AppAppIdAttributeBulkArgs = {app_id: string, body: Array<{name: string, event_data_key: string, data_type: 'number' | 'bool' | 'ip' | 'datetime' | 'string', aggregation: 'first' | 'last' | 'first_last' | 'list' | 'list_unique' | 'min' | 'max' | 'sum'}>}; export type CreateApiApiCdpSettingsV1AppAppIdAttributeBulkOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiCdpSettingsV1AppAppIdAttributeBulkResponse = z.array(z.object({column_id: z.string(), data_type: z.enum(["number", "bool", "ip", "datetime", "string"]), aggregation: z.enum(["first", "last", "first_last", "list", "list_unique", "min", "max", "sum"]), event_data_key: z.string(), immutable: z.boolean(), publicly_accessible: z.boolean(), 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()})})); 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 custom attribute bulk * * POST /api/cdp/settings/v1/app/{app_id}/attribute/bulk */ export const createApiApiCdpSettingsV1AppAppIdAttributeBulk = async (args: CreateApiApiCdpSettingsV1AppAppIdAttributeBulkArgs, options?: CreateApiApiCdpSettingsV1AppAppIdAttributeBulkOptions) => { 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))}/attribute/bulk${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(createApiApiCdpSettingsV1AppAppIdAttributeBulkResponse, await res.json()) };