import {z} from 'zod' export type GetApiApiCdpSettingsV1AppAppIdAudienceArgs = {app_id: string}; export type GetApiApiCdpSettingsV1AppAppIdAudienceOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiCdpSettingsV1AppAppIdAudienceResponse = z.array(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)})]), 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 }; /** * List audiences * * GET /api/cdp/settings/v1/app/{app_id}/audience */ export const getApiApiCdpSettingsV1AppAppIdAudience = async (args: GetApiApiCdpSettingsV1AppAppIdAudienceArgs, options?: GetApiApiCdpSettingsV1AppAppIdAudienceOptions) => { const {baseUrl = "https://example.piwik.pro", fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiCdpSettingsV1AppAppIdAudienceResponse, await res.json()) };