import {z} from 'zod' export type ApiNotificationResourceType = 'dashboardComments' | 'aiReview'; export type GetApiApiV1NotificationsArgs = {type: ApiNotificationResourceType}; export type GetApiApiV1NotificationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const notificationDashboardTileCommentMetadata = z.object({dashboardUuid: z.string(), dashboardName: z.string(), dashboardTileUuid: z.string(), dashboardTileName: z.string()}).and(z.record(z.string(), z.unknown())); export const apiNotificationResourceTypeDashboardComments = z.literal("dashboardComments"); export const notificationDashboardComment = z.object({url: z.string().optional(), message: z.string().optional(), resourceUuid: z.string().optional(), viewed: z.boolean(), createdAt: z.string(), notificationId: z.string(), metadata: notificationDashboardTileCommentMetadata.optional(), resourceType: apiNotificationResourceTypeDashboardComments}); export const aiReviewNotificationEvent = z.enum(["needs_review", "assigned"]); export const apiNotificationResourceTypeAiReview = z.literal("aiReview"); export const notificationAiReview = z.object({url: z.string().optional(), message: z.string().optional(), resourceUuid: z.string().optional(), viewed: z.boolean(), createdAt: z.string(), notificationId: z.string(), metadata: z.object({searchParams: z.string(), count: z.number(), projectUuid: z.string(), rootCause: z.string(), title: z.string(), event: aiReviewNotificationEvent, fingerprint: z.string()}), resourceType: apiNotificationResourceTypeAiReview}); export const notification = z.union([notificationDashboardComment, notificationAiReview]); export const apiNotificationsResults = z.array(notification); export const apiGetNotifications = z.object({results: apiNotificationsResults, status: z.literal("ok")}); 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 }; /** * Get notifications * * GET /api/v1/notifications */ export const getApiApiV1Notifications = async (args: GetApiApiV1NotificationsArgs, options: GetApiApiV1NotificationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/notifications${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(apiGetNotifications, await res.json()) };