import {z} from 'zod' export type GetApiApiNotificationsArgs = {pageSize: number, page: number, textSearch?: string | undefined, sortProperty?: string | undefined, sortOrder?: string | undefined, unreadOnly?: boolean | undefined, deliveryMethod?: ('WEB' | 'MOBILE_APP') | undefined}; export type GetApiApiNotificationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationRequestId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION_REQUEST")}); export const userId = z.object({id: z.string(), entityType: z.literal("USER")}); export const jsonNode = z.unknown(); export const dashboardId = z.object({id: z.string(), entityType: z.literal("DASHBOARD")}); export const entityId = z.object({id: z.string(), entityType: z.enum(["TENANT", "CUSTOMER", "USER", "DASHBOARD", "ASSET", "DEVICE", "ALARM", "RULE_CHAIN", "RULE_NODE", "ENTITY_VIEW", "WIDGETS_BUNDLE", "WIDGET_TYPE", "TENANT_PROFILE", "DEVICE_PROFILE", "ASSET_PROFILE", "API_USAGE_STATE", "TB_RESOURCE", "OTA_PACKAGE", "EDGE", "RPC", "QUEUE", "NOTIFICATION_TARGET", "NOTIFICATION_TEMPLATE", "NOTIFICATION_REQUEST", "NOTIFICATION", "NOTIFICATION_RULE", "QUEUE_STATS", "OAUTH2_CLIENT", "DOMAIN", "MOBILE_APP", "MOBILE_APP_BUNDLE", "CALCULATED_FIELD", "JOB", "ADMIN_SETTINGS", "AI_MODEL", "API_KEY"])}); export const notificationInfo = z.object({dashboardId: dashboardId.optional(), stateEntityId: entityId.optional(), type: z.string()}); export const notificationId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION")}); export const notification = z.object({requestId: notificationRequestId.optional(), recipientId: userId.optional(), type: z.enum(["GENERAL", "ALARM", "DEVICE_ACTIVITY", "ENTITY_ACTION", "ALARM_COMMENT", "RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT", "ALARM_ASSIGNMENT", "NEW_PLATFORM_VERSION", "ENTITIES_LIMIT", "ENTITIES_LIMIT_INCREASE_REQUEST", "API_USAGE_LIMIT", "RULE_NODE", "RATE_LIMITS", "EDGE_CONNECTION", "EDGE_COMMUNICATION_FAILURE", "TASK_PROCESSING_FAILURE", "RESOURCES_SHORTAGE"]).optional(), deliveryMethod: z.enum(["WEB", "EMAIL", "SMS", "SLACK", "MICROSOFT_TEAMS", "MOBILE_APP"]).optional(), subject: z.string().optional(), text: z.string().optional(), additionalConfig: jsonNode.optional(), info: notificationInfo.optional(), status: z.enum(["SENT", "READ"]).optional(), id: notificationId.optional(), createdTime: z.number().int().optional()}); export const pageDataNotification = z.object({data: z.array(notification).optional(), totalPages: z.number().int().optional(), totalElements: z.number().int().optional(), hasNext: z.boolean().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 }; /** * Get notifications (getNotifications) * * GET /api/notifications */ export const getApiApiNotifications = async (args: GetApiApiNotificationsArgs, options?: GetApiApiNotificationsOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.textSearch != null) query.append("textSearch", String(args.textSearch)) if (args.sortProperty != null) query.append("sortProperty", String(args.sortProperty)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.unreadOnly != null) query.append("unreadOnly", String(args.unreadOnly)) if (args.deliveryMethod != null) query.append("deliveryMethod", String(args.deliveryMethod)) const url = `${baseUrl.replace(/\/+$/, '')}/api/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(pageDataNotification, await res.json()) };