import {z} from 'zod' export type CreateApiApiAnalyticsV1ManageCustomDimensionsArgs = {body: {data: {type: 'CustomDimension', id: string, attributes: {website_id: string, name: string, description?: string | undefined, active: boolean, case_sensitive: boolean, tracking_id: number, data_type?: ('string' | 'integer' | 'float') | undefined, formatting?: ('number' | 'time' | 'percentage' | null) | undefined, extractions?: (Array<{target?: ('page_title_regex' | 'page_url_regex' | 'page_query_parameter') | undefined, pattern?: string | undefined}>) | undefined, scope: 'session' | 'event', slot: number}}}}; export type CreateApiApiAnalyticsV1ManageCustomDimensionsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiAnalyticsV1ManageCustomDimensionsResponse = z.object({data: z.object({type: z.literal("CustomDimension"), id: z.string(), attributes: z.object({website_id: z.string(), name: z.string(), description: z.string().max(300).optional(), active: z.boolean(), case_sensitive: z.boolean(), tracking_id: z.number().int().gte(1), data_type: z.enum(["string", "integer", "float"]).optional(), formatting: z.enum(["number", "time", "percentage"]).nullable().optional(), extractions: z.array(z.object({target: z.enum(["page_title_regex", "page_url_regex", "page_query_parameter"]).optional(), pattern: z.string().optional()})).optional(), scope: z.enum(["session", "event"]), slot: 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 custom dimension * * POST /api/analytics/v1/manage/custom-dimensions/ */ export const createApiApiAnalyticsV1ManageCustomDimensions = async (args: CreateApiApiAnalyticsV1ManageCustomDimensionsArgs, options?: CreateApiApiAnalyticsV1ManageCustomDimensionsOptions) => { const {baseUrl = "https://example.piwik.pro", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/analytics/v1/manage/custom-dimensions/${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(createApiApiAnalyticsV1ManageCustomDimensionsResponse, await res.json()) };