@moshaver/cmb-notifications 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mobin Karam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # `@moshaver/cmb-notifications`
2
+
3
+ Framework-neutral notification pagination, cursor, and public-state mechanics. Persistence, student recipient resolution, realtime/push delivery, and API error localization remain adapters.
4
+
5
+ ## Install and use
6
+
7
+ ```bash
8
+ npm install @moshaver/cmb-notifications
9
+ ```
10
+
11
+ ```js
12
+ const { encodeCursor, decodeCursor, normalizePageLimit } = require("@moshaver/cmb-notifications");
13
+ const limit = normalizePageLimit(50);
14
+ ```
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@moshaver/cmb-notifications",
3
+ "version": "0.1.0",
4
+ "description": "Composable Modular Backend notifications primitives.",
5
+ "type": "commonjs",
6
+ "main": "./src/index.js",
7
+ "types": "./src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/index.d.ts",
11
+ "require": "./src/index.js",
12
+ "default": "./src/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "src",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "keywords": [
21
+ "cmb",
22
+ "modular-backend",
23
+ "backend",
24
+ "framework-agnostic",
25
+ "notifications"
26
+ ],
27
+ "engines": {
28
+ "node": ">=22.13.0 <23"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/Mobin-Karam/moshaver.git",
33
+ "directory": "packages/cmb/notifications"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/Mobin-Karam/moshaver/issues"
37
+ },
38
+ "homepage": "https://github.com/Mobin-Karam/moshaver/tree/main/packages/cmb",
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "license": "MIT",
43
+ "sideEffects": false,
44
+ "dependencies": {
45
+ "@moshaver/cmb-kernel": "^0.1.0"
46
+ }
47
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export const NOTIFICATIONS_MODULE: Readonly<{ id: "notifications"; version: string; kind: "platform"; dependencies: readonly string[] }>;
2
+ export class InvalidCursorError extends Error {}
3
+ export function normalizePageLimit(value: unknown, fallback?: number, maximum?: number): number;
4
+ export function encodeCursor(value: { createdAt: Date | string; id: string }): string;
5
+ export function decodeCursor(value: string): { createdAt: string; id: string };
6
+ export function projectNotification<T>(notification: T & { id: string; type: unknown; category: string; title: string; body: string; url?: string | null; data?: unknown; priority: string; readAt?: Date | null; createdAt: Date; expiresAt?: Date | null }): { id: string; type: unknown; category: string; title: string; body: string; message: string; url: string | null; data: unknown; priority: string; isRead: boolean; readAt: Date | null; createdAt: Date; expiresAt: Date | null };
7
+ export interface NotificationRepository<T> { save(notification: T): Promise<T>; list(recipientId?: string): Promise<T[]>; }
8
+ export interface NotificationProvider<T> { id: string; deliver(notification: T): Promise<void>; }
9
+ export class InMemoryNotificationRepository<T extends { recipientId?: string }> implements NotificationRepository<T> { save(notification: T): Promise<T>; list(recipientId?: string): Promise<T[]>; }
10
+ export class NotificationService<TInput extends object, TNotification extends TInput & { id: string; createdAt: Date; readAt: null }> {
11
+ constructor(options: { repository: NotificationRepository<TNotification>; providers?: NotificationProvider<TNotification>[]; events?: { publish(event: { type: string; data: TNotification }): Promise<void> }; clock?: () => Date; id?: () => string });
12
+ create(input: TInput & { createdAt?: Date }): Promise<{ notification: TNotification; deliveries: Array<{ provider: string; status: "delivered" | "failed"; error?: string }> }>;
13
+ }
package/src/index.js ADDED
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ const { defineModule } = require("@moshaver/cmb-kernel");
3
+ const NOTIFICATIONS_MODULE = defineModule({ id: "notifications", version: "0.1.0", kind: "platform", dependencies: ["kernel"], optionalDependencies: ["realtime"], provides: ["notifications.service"], publishes: ["notification.created"] });
4
+ class InvalidCursorError extends Error { constructor() { super("Invalid notification cursor."); this.name = "InvalidCursorError"; } }
5
+ function normalizePageLimit(value, fallback = 20, maximum = 100) { return Math.min(Math.max(Number(value) || fallback, 1), maximum); }
6
+ function encodeCursor(value) { return Buffer.from(JSON.stringify(value)).toString("base64url"); }
7
+ function decodeCursor(value) { try { const decoded = JSON.parse(Buffer.from(value, "base64url").toString()); if (!decoded || typeof decoded !== "object" || !decoded.createdAt || !decoded.id) throw new Error(); return decoded; } catch { throw new InvalidCursorError(); } }
8
+ function projectNotification(n) { return { id: n.id, type: n.type, category: n.category, title: n.title, body: n.body, message: n.body, url: n.url || null, data: n.data || null, priority: n.priority, isRead: Boolean(n.readAt), readAt: n.readAt || null, createdAt: n.createdAt, expiresAt: n.expiresAt || null }; }
9
+ class InMemoryNotificationRepository {
10
+ constructor() { this.notifications = []; }
11
+ async save(notification) { const stored = Object.freeze({ ...notification }); this.notifications.push(stored); return stored; }
12
+ async list(recipientId) { return this.notifications.filter((item) => !recipientId || item.recipientId === recipientId); }
13
+ }
14
+ class NotificationService {
15
+ constructor(options = {}) {
16
+ if (!options.repository || typeof options.repository.save !== "function") throw new TypeError("Notification repository with save() is required.");
17
+ this.repository = options.repository;
18
+ this.providers = Array.from(options.providers ?? []);
19
+ this.events = options.events ?? { publish: async () => undefined };
20
+ this.clock = options.clock ?? (() => new Date());
21
+ this.id = options.id ?? (() => require("node:crypto").randomUUID());
22
+ for (const provider of this.providers) if (!provider?.id || typeof provider.deliver !== "function") throw new TypeError("Notification providers require id and deliver().");
23
+ }
24
+ async create(input) {
25
+ const notification = await this.repository.save({ id: this.id(), ...input, createdAt: input.createdAt ?? this.clock(), readAt: null });
26
+ await this.events.publish({ type: "notification.created", data: notification });
27
+ const deliveries = [];
28
+ for (const provider of this.providers) {
29
+ try { await provider.deliver(notification); deliveries.push({ provider: provider.id, status: "delivered" }); }
30
+ catch (error) { deliveries.push({ provider: provider.id, status: "failed", error: error instanceof Error ? error.message : "Delivery failed" }); }
31
+ }
32
+ return { notification, deliveries };
33
+ }
34
+ }
35
+ module.exports = { NOTIFICATIONS_MODULE, InvalidCursorError, normalizePageLimit, encodeCursor, decodeCursor, projectNotification, InMemoryNotificationRepository, NotificationService };