import {z} from 'zod' export type GetApiApiV1DashboardRecentActivityArgs = {limit?: (number | null) | undefined, entityTypes?: (Array<'stream' | 'offer' | 'pwa' | 'landing' | 'domain' | 'pixel' | 'push'> | null) | undefined}; export type GetApiApiV1DashboardRecentActivityOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardActivityContextDTO = z.object({kind: z.enum(["clicks", "streams", "status", "pixelType"]), value: z.union([z.number().int(), z.string()])}); export const dashboardRecentActivityItemDTO = z.object({id: z.string(), entityType: z.enum(["stream", "offer", "pwa", "landing", "domain", "pixel", "push"]), entityId: z.string(), action: z.literal("updated"), title: z.string().nullable().optional(), timestamp: z.string(), context: dashboardActivityContextDTO.nullable().optional()}); export const dashboardRecentActivityResponseDTO = z.object({items: z.array(dashboardRecentActivityItemDTO)}); 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 }; /** * Merged recent activity feed * * GET /api/v1/dashboard/recent-activity */ export const getApiApiV1DashboardRecentActivity = async (args: GetApiApiV1DashboardRecentActivityArgs, options: GetApiApiV1DashboardRecentActivityOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.entityTypes ?? []) query.append("entityTypes", String(item)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/dashboard/recent-activity${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(dashboardRecentActivityResponseDTO, await res.json()) };