@onlive.ai/common-121 0.2.32

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.
Files changed (111) hide show
  1. package/README.md +151 -0
  2. package/package.json +39 -0
  3. package/services/audio.service.cjs +75 -0
  4. package/services/audio.service.d.cts +14 -0
  5. package/services/audio.service.d.ts +14 -0
  6. package/services/audio.service.js +52 -0
  7. package/services/firebase/firebase-api.cjs +108 -0
  8. package/services/firebase/firebase-api.d.cts +54 -0
  9. package/services/firebase/firebase-api.d.ts +54 -0
  10. package/services/firebase/firebase-api.js +105 -0
  11. package/services/firebase/firebase.service.cjs +193 -0
  12. package/services/firebase/firebase.service.d.cts +40 -0
  13. package/services/firebase/firebase.service.d.ts +40 -0
  14. package/services/firebase/firebase.service.js +190 -0
  15. package/services/livekit.service.cjs +353 -0
  16. package/services/livekit.service.d.cts +131 -0
  17. package/services/livekit.service.d.ts +131 -0
  18. package/services/livekit.service.js +329 -0
  19. package/services/request.provider.cjs +48 -0
  20. package/services/request.provider.d.cts +27 -0
  21. package/services/request.provider.d.ts +27 -0
  22. package/services/request.provider.js +22 -0
  23. package/themes/dark.cjs +433 -0
  24. package/themes/dark.d.cts +5 -0
  25. package/themes/dark.d.ts +5 -0
  26. package/themes/dark.js +408 -0
  27. package/themes/light.cjs +433 -0
  28. package/themes/light.d.cts +5 -0
  29. package/themes/light.d.ts +5 -0
  30. package/themes/light.js +408 -0
  31. package/types/tracking-options.cjs +18 -0
  32. package/types/tracking-options.d.cts +27 -0
  33. package/types/tracking-options.d.ts +27 -0
  34. package/types/tracking-options.js +0 -0
  35. package/types/window-context.cjs +18 -0
  36. package/types/window-context.d.cts +8 -0
  37. package/types/window-context.d.ts +8 -0
  38. package/types/window-context.js +0 -0
  39. package/utils/adopt-styles.cjs +66 -0
  40. package/utils/adopt-styles.d.cts +5 -0
  41. package/utils/adopt-styles.d.ts +5 -0
  42. package/utils/adopt-styles.js +41 -0
  43. package/utils/browser-preferences.cjs +67 -0
  44. package/utils/browser-preferences.d.cts +18 -0
  45. package/utils/browser-preferences.d.ts +18 -0
  46. package/utils/browser-preferences.js +42 -0
  47. package/utils/classify-media.cjs +38 -0
  48. package/utils/classify-media.d.cts +9 -0
  49. package/utils/classify-media.d.ts +9 -0
  50. package/utils/classify-media.js +13 -0
  51. package/utils/decorators.cjs +53 -0
  52. package/utils/decorators.d.cts +34 -0
  53. package/utils/decorators.d.ts +34 -0
  54. package/utils/decorators.js +28 -0
  55. package/utils/detected-lang.cjs +47 -0
  56. package/utils/detected-lang.d.cts +10 -0
  57. package/utils/detected-lang.d.ts +10 -0
  58. package/utils/detected-lang.js +22 -0
  59. package/utils/fullscreen.cjs +52 -0
  60. package/utils/fullscreen.d.cts +16 -0
  61. package/utils/fullscreen.d.ts +16 -0
  62. package/utils/fullscreen.js +26 -0
  63. package/utils/insert-script.cjs +55 -0
  64. package/utils/insert-script.d.cts +29 -0
  65. package/utils/insert-script.d.ts +29 -0
  66. package/utils/insert-script.js +30 -0
  67. package/utils/markdown.cjs +103 -0
  68. package/utils/markdown.d.cts +12 -0
  69. package/utils/markdown.d.ts +12 -0
  70. package/utils/markdown.js +68 -0
  71. package/utils/merge.cjs +47 -0
  72. package/utils/merge.d.cts +23 -0
  73. package/utils/merge.d.ts +23 -0
  74. package/utils/merge.js +22 -0
  75. package/utils/multiband-track-volume.cjs +90 -0
  76. package/utils/multiband-track-volume.d.cts +27 -0
  77. package/utils/multiband-track-volume.d.ts +27 -0
  78. package/utils/multiband-track-volume.js +65 -0
  79. package/utils/onlive-url-params.cjs +38 -0
  80. package/utils/onlive-url-params.d.cts +14 -0
  81. package/utils/onlive-url-params.d.ts +14 -0
  82. package/utils/onlive-url-params.js +13 -0
  83. package/utils/pretty-distance.cjs +45 -0
  84. package/utils/pretty-distance.d.cts +46 -0
  85. package/utils/pretty-distance.d.ts +46 -0
  86. package/utils/pretty-distance.js +20 -0
  87. package/utils/random-string.cjs +37 -0
  88. package/utils/random-string.d.cts +9 -0
  89. package/utils/random-string.d.ts +9 -0
  90. package/utils/random-string.js +12 -0
  91. package/utils/resource-type.cjs +50 -0
  92. package/utils/resource-type.d.cts +16 -0
  93. package/utils/resource-type.d.ts +16 -0
  94. package/utils/resource-type.js +24 -0
  95. package/utils/safe-html.cjs +112 -0
  96. package/utils/safe-html.d.cts +12 -0
  97. package/utils/safe-html.d.ts +12 -0
  98. package/utils/safe-html.js +77 -0
  99. package/utils/sanitize.cjs +94 -0
  100. package/utils/sanitize.d.cts +11 -0
  101. package/utils/sanitize.d.ts +11 -0
  102. package/utils/sanitize.js +59 -0
  103. package/utils/spread.cjs +70 -0
  104. package/utils/spread.d.cts +14 -0
  105. package/utils/spread.d.ts +14 -0
  106. package/utils/spread.js +47 -0
  107. package/utils/watch.cjs +54 -0
  108. package/utils/watch.d.cts +28 -0
  109. package/utils/watch.d.ts +28 -0
  110. package/utils/watch.js +29 -0
  111. package/vite-env.d.ts +22 -0
package/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # Onlive Common 121
2
+
3
+ A shared utility package containing common services, utilities, types, and themes used across the Onlive 121 ecosystem. This package provides the foundational components for building interactive chat, flow, and video meeting applications.
4
+
5
+ ## 📦 Installation
6
+
7
+ ```bash
8
+ npm install @onlive.ai/common-121
9
+ ```
10
+
11
+ ## 📂 Package Structure
12
+
13
+ ### 🛠️ Services
14
+ - **AudioService**: Handles audio notifications and sound management for incoming calls and interactions
15
+ - **FirebaseService**: Manages real-time Firebase connections for chat requests and agent presence
16
+ - **LivekitService**: Provides WebRTC functionality for video/audio communication using LiveKit
17
+
18
+ ### 🔧 Utilities
19
+ - **adopt-styles**: Utility for adopting CSS styles in web components
20
+ - **browser-preferences**: Browser preference detection and management
21
+ - **classify-media**: Media file classification and type detection
22
+ - **decorators**: Custom element decorators and helpers
23
+ - **detected-lang**: Language detection utilities
24
+ - **fullscreen**: Fullscreen API helpers
25
+ - **markdown**: Markdown parsing with sanitization
26
+ - **merge**: Deep object merging utilities
27
+ - **sanitize**: HTML content sanitization
28
+ - **watch**: Reactive property watching utilities
29
+ - **And more...**
30
+
31
+ ### 🎨 Themes
32
+ - **Light Theme**: Default light mode styling
33
+ - **Dark Theme**: Dark mode color scheme
34
+ - **Custom Themes**: Brand-specific themes (Audi, Cupra, Hyundai, Magimix, Onlive)
35
+
36
+ ### 📋 Types
37
+ - **TrackingOptions**: Configuration types for analytics tracking
38
+ - **WindowContext**: Global window context definitions
39
+
40
+ ## 🚀 Usage
41
+
42
+ ### Basic Import
43
+
44
+ ```typescript
45
+ import { AudioService } from '@onlive.ai/common-121/services/audio.service';
46
+ import { markdown } from '@onlive.ai/common-121/utils/markdown';
47
+ import { merge } from '@onlive.ai/common-121/utils/merge';
48
+ ```
49
+
50
+ ### Audio Service
51
+
52
+ ```typescript
53
+ import { AudioService } from '@onlive.ai/common-121/services/audio.service';
54
+
55
+ const audioService = AudioService.getInstance();
56
+
57
+ // Play incoming call sound
58
+ audioService.playIncoming();
59
+
60
+ // Stop current sound
61
+ audioService.stopCurrentSound();
62
+ ```
63
+
64
+ ### Firebase Service
65
+
66
+ ```typescript
67
+ import { FirebaseService } from '@onlive.ai/common-121/services/firebase/firebase.service';
68
+
69
+ const firebaseService = new FirebaseService(firebaseConfig, organizationId);
70
+
71
+ // Listen for new requests
72
+ firebaseService.onNewRequest((id, request) => {
73
+ console.log('New request:', request);
74
+ });
75
+ ```
76
+
77
+ ### LiveKit Service
78
+
79
+ ```typescript
80
+ import { LivekitService } from '@onlive.ai/common-121/services/livekit.service';
81
+
82
+ const livekitService = new LivekitService();
83
+
84
+ // Join a room
85
+ await livekitService.joinRoom(roomId, timezone, language);
86
+ ```
87
+
88
+ ### Utilities
89
+
90
+ ```typescript
91
+ import { markdown } from '@onlive.ai/common-121/utils/markdown';
92
+ import { merge } from '@onlive.ai/common-121/utils/merge';
93
+ import { sanitize } from '@onlive.ai/common-121/utils/sanitize';
94
+
95
+ // Parse markdown safely
96
+ const htmlContent = markdown('# Hello **World**');
97
+
98
+ // Merge objects deeply
99
+ const merged = merge(defaultOptions, userOptions);
100
+
101
+ // Sanitize HTML content
102
+ const cleanHtml = sanitize(userInput);
103
+ ```
104
+
105
+ ### Themes
106
+
107
+ ```typescript
108
+ import { styles as lightTheme } from '@onlive.ai/common-121/themes/light';
109
+ import { styles as darkTheme } from '@onlive.ai/common-121/themes/dark';
110
+
111
+ // Apply theme to a Lit component
112
+ static styles = [lightTheme];
113
+ ```
114
+
115
+ ## 🏗️ Architecture
116
+
117
+ This package is designed to be used as a foundation for the Onlive 121 ecosystem, providing:
118
+
119
+ - **Modular Design**: Each service and utility can be imported independently
120
+ - **Type Safety**: Full TypeScript support with comprehensive type definitions
121
+ - **Performance**: Optimized for production use with efficient caching and lazy loading
122
+ - **Extensibility**: Easy to extend and customize for specific use cases
123
+
124
+ ## 🔗 Dependencies
125
+
126
+ This package integrates with several key technologies:
127
+
128
+ - **Firebase**: Real-time database and authentication
129
+ - **LiveKit**: WebRTC infrastructure for video/audio
130
+ - **Lit**: Web components framework
131
+ - **RxJS**: Reactive programming utilities
132
+ - **Marked**: Markdown parsing
133
+ - **DOMPurify**: HTML sanitization
134
+
135
+ ## 📱 Compatibility
136
+
137
+ - **Frameworks**: Lit, Web Components
138
+ - **Browsers**: Modern browsers with ES2020+ support
139
+ - **Node.js**: 18.x or higher
140
+ - **TypeScript**: 5.7.x
141
+
142
+ ## 🤝 Related Packages
143
+
144
+ - [`@onlive.ai/chat`](../chat): Chat component and messaging functionality
145
+ - [`@onlive.ai/flow`](../flow): Interactive flow components
146
+ - [`@onlive.ai/meet`](../meet): Video conferencing components
147
+ - [`@onlive.ai/flow-client`](../flow-client): Flow client SDK
148
+
149
+ ## 📄 License
150
+
151
+ This package is part of the Onlive 121 ecosystem and follows the project's licensing terms.
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@onlive.ai/common-121",
3
+ "version": "0.2.32",
4
+ "type": "module",
5
+ "files": [
6
+ "**/*.js",
7
+ "**/*.cjs",
8
+ "**/*.d.ts",
9
+ "**/*.d.cts",
10
+ "!./*.iife.js",
11
+ "!./themes/custom/**",
12
+ "!./node_modules/**"
13
+ ],
14
+ "exports": {
15
+ "./utils/*": "./utils/*",
16
+ "./types/*": "./types/*",
17
+ "./services/*": "./services/*",
18
+ "./themes/*": "./themes/*"
19
+ },
20
+ "dependencies": {
21
+ "@formatjs/intl-localematcher": "^0.5.10",
22
+ "@livekit/components-core": "^0.12.5",
23
+ "@onlive.ai/tracker": "^1.0.7",
24
+ "dompurify": "^3.2.5",
25
+ "firebase": "^11.7.3",
26
+ "lit": "^3.3.0",
27
+ "tsup": "^8.5.0",
28
+ "livekit-client": "^2.13.0",
29
+ "marked": "^15.0.11",
30
+ "rxjs": "^7.8.2",
31
+ "typescript": "~5.7.3"
32
+ },
33
+ "scripts": {
34
+ "build:package": "tsup",
35
+ "test": "vitest",
36
+ "test:coverage": "vitest --coverage",
37
+ "clean": "find . \\( -iname \"*.js\" -o -iname \"*.cjs\" -o -iname \"*.d.ts\" -o -iname \"*.d.cts\" \\) -type f -not -path \"./node_modules/*\" -delete"
38
+ }
39
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
+
22
+ // services/audio.service.ts
23
+ var audio_service_exports = {};
24
+ __export(audio_service_exports, {
25
+ AudioService: () => AudioService
26
+ });
27
+ module.exports = __toCommonJS(audio_service_exports);
28
+ var _AudioService = class _AudioService {
29
+ constructor() {
30
+ __publicField(this, "sounds", /* @__PURE__ */ new Map());
31
+ __publicField(this, "currentSound", null);
32
+ this.loadSound("ringing", "https://a.slack-edge.com/bv1-13/flitterbug-77334dc.mp3");
33
+ this.loadSound("incoming", "https://cdn.onlive.ai/widget-assets/call_phone_ring_tone.mp3");
34
+ }
35
+ static getInstance() {
36
+ if (!_AudioService.instance) {
37
+ _AudioService.instance = new _AudioService();
38
+ }
39
+ return _AudioService.instance;
40
+ }
41
+ loadSound(name, url) {
42
+ const audio = new Audio(url);
43
+ audio.preload = "auto";
44
+ this.sounds.set(name, audio);
45
+ }
46
+ playSound(name, loop = false) {
47
+ this.stopCurrentSound();
48
+ const sound = this.sounds.get(name);
49
+ if (sound) {
50
+ sound.loop = loop;
51
+ sound.currentTime = 0;
52
+ sound.play().catch((error) => console.warn("Error playing sound:", error));
53
+ this.currentSound = sound;
54
+ }
55
+ }
56
+ stopCurrentSound() {
57
+ if (this.currentSound) {
58
+ this.currentSound.pause();
59
+ this.currentSound.currentTime = 0;
60
+ this.currentSound = null;
61
+ }
62
+ }
63
+ playRinging(loop = true) {
64
+ this.playSound("ringing", loop);
65
+ }
66
+ playIncoming(loop = true) {
67
+ this.playSound("incoming", loop);
68
+ }
69
+ };
70
+ __publicField(_AudioService, "instance");
71
+ var AudioService = _AudioService;
72
+ // Annotate the CommonJS export names for ESM import in node:
73
+ 0 && (module.exports = {
74
+ AudioService
75
+ });
@@ -0,0 +1,14 @@
1
+ declare class AudioService {
2
+ private static instance;
3
+ private sounds;
4
+ private currentSound;
5
+ private constructor();
6
+ static getInstance(): AudioService;
7
+ private loadSound;
8
+ playSound(name: string, loop?: boolean): void;
9
+ stopCurrentSound(): void;
10
+ playRinging(loop?: boolean): void;
11
+ playIncoming(loop?: boolean): void;
12
+ }
13
+
14
+ export { AudioService };
@@ -0,0 +1,14 @@
1
+ declare class AudioService {
2
+ private static instance;
3
+ private sounds;
4
+ private currentSound;
5
+ private constructor();
6
+ static getInstance(): AudioService;
7
+ private loadSound;
8
+ playSound(name: string, loop?: boolean): void;
9
+ stopCurrentSound(): void;
10
+ playRinging(loop?: boolean): void;
11
+ playIncoming(loop?: boolean): void;
12
+ }
13
+
14
+ export { AudioService };
@@ -0,0 +1,52 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
5
+ // services/audio.service.ts
6
+ var _AudioService = class _AudioService {
7
+ constructor() {
8
+ __publicField(this, "sounds", /* @__PURE__ */ new Map());
9
+ __publicField(this, "currentSound", null);
10
+ this.loadSound("ringing", "https://a.slack-edge.com/bv1-13/flitterbug-77334dc.mp3");
11
+ this.loadSound("incoming", "https://cdn.onlive.ai/widget-assets/call_phone_ring_tone.mp3");
12
+ }
13
+ static getInstance() {
14
+ if (!_AudioService.instance) {
15
+ _AudioService.instance = new _AudioService();
16
+ }
17
+ return _AudioService.instance;
18
+ }
19
+ loadSound(name, url) {
20
+ const audio = new Audio(url);
21
+ audio.preload = "auto";
22
+ this.sounds.set(name, audio);
23
+ }
24
+ playSound(name, loop = false) {
25
+ this.stopCurrentSound();
26
+ const sound = this.sounds.get(name);
27
+ if (sound) {
28
+ sound.loop = loop;
29
+ sound.currentTime = 0;
30
+ sound.play().catch((error) => console.warn("Error playing sound:", error));
31
+ this.currentSound = sound;
32
+ }
33
+ }
34
+ stopCurrentSound() {
35
+ if (this.currentSound) {
36
+ this.currentSound.pause();
37
+ this.currentSound.currentTime = 0;
38
+ this.currentSound = null;
39
+ }
40
+ }
41
+ playRinging(loop = true) {
42
+ this.playSound("ringing", loop);
43
+ }
44
+ playIncoming(loop = true) {
45
+ this.playSound("incoming", loop);
46
+ }
47
+ };
48
+ __publicField(_AudioService, "instance");
49
+ var AudioService = _AudioService;
50
+ export {
51
+ AudioService
52
+ };
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
+
22
+ // services/firebase/firebase-api.ts
23
+ var firebase_api_exports = {};
24
+ __export(firebase_api_exports, {
25
+ FirebaseApi: () => FirebaseApi
26
+ });
27
+ module.exports = __toCommonJS(firebase_api_exports);
28
+ var import_app = require("firebase/app");
29
+ var import_database = require("firebase/database");
30
+ var FirebaseApi = class {
31
+ constructor(options) {
32
+ __publicField(this, "db");
33
+ /** Other listeners... */
34
+ __publicField(this, "onChildChanged", (path, cb) => (0, import_database.onChildChanged)(this.ref(path), (snap) => cb(snap.key, snap.val(), snap)));
35
+ __publicField(this, "onChildRemoved", (path, cb) => (0, import_database.onChildRemoved)(this.ref(path), (snap) => cb(snap.key, snap.val(), snap)));
36
+ const app = (0, import_app.initializeApp)(options.config);
37
+ this.db = (0, import_database.getDatabase)(app);
38
+ }
39
+ /** Get a DatabaseReference */
40
+ ref(path) {
41
+ return (0, import_database.ref)(this.db, path);
42
+ }
43
+ /** Read once */
44
+ async getOnce(path) {
45
+ const snap = await (0, import_database.get)(this.ref(path));
46
+ return snap.exists() ? snap.val() : null;
47
+ }
48
+ /** Listen to value changes */
49
+ onValue(path, cb) {
50
+ return (0, import_database.onValue)(
51
+ this.ref(path),
52
+ (snap) => cb(snap.key ?? "", snap.val(), snap),
53
+ (err) => console.error("onValue error", err)
54
+ );
55
+ }
56
+ /** Listen to child-added */
57
+ onChildAdded(path, cb) {
58
+ return (0, import_database.onChildAdded)(
59
+ this.ref(path),
60
+ (snap) => cb(snap.key, snap.val(), snap),
61
+ (err) => console.error("onChildAdded error", err)
62
+ );
63
+ }
64
+ /** Write/overwrite a node */
65
+ write(path, data) {
66
+ return (0, import_database.set)(this.ref(path), data);
67
+ }
68
+ /** Update (merge) a node */
69
+ update(path, data) {
70
+ return (0, import_database.update)(this.ref(path), data);
71
+ }
72
+ /** Remove a node */
73
+ remove(path) {
74
+ return (0, import_database.remove)(this.ref(path));
75
+ }
76
+ /** Push a child with generated key */
77
+ async push(path, data) {
78
+ const newRef = (0, import_database.push)(this.ref(path));
79
+ await (0, import_database.set)(newRef, data);
80
+ return newRef.key;
81
+ }
82
+ /** onDisconnect write */
83
+ onDisconnectUpdate(path, data) {
84
+ return (0, import_database.onDisconnect)(this.ref(path)).update(data);
85
+ }
86
+ /** Cancel onDisconnect operations */
87
+ cancelOnDisconnect(path) {
88
+ return (0, import_database.onDisconnect)(this.ref(path)).cancel();
89
+ }
90
+ /** Run a transaction */
91
+ transaction(path, updateFn) {
92
+ return (0, import_database.runTransaction)(this.ref(path), (cur) => updateFn(cur));
93
+ }
94
+ /** Build a query */
95
+ query(path, opts = {}) {
96
+ let q = this.ref(path);
97
+ if (opts.orderBy === "child" && opts.childKey) q = (0, import_database.query)(q, (0, import_database.orderByChild)(opts.childKey));
98
+ if (opts.orderBy === "key") q = (0, import_database.query)(q, (0, import_database.orderByKey)());
99
+ if (opts.orderBy === "value") q = (0, import_database.query)(q, (0, import_database.orderByValue)());
100
+ if (opts.limitFirst != null) q = (0, import_database.query)(q, (0, import_database.limitToFirst)(opts.limitFirst));
101
+ if (opts.limitLast != null) q = (0, import_database.query)(q, (0, import_database.limitToLast)(opts.limitLast));
102
+ return q;
103
+ }
104
+ };
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ FirebaseApi
108
+ });
@@ -0,0 +1,54 @@
1
+ import { DatabaseReference, DataSnapshot, Unsubscribe, TransactionResult, Query } from 'firebase/database';
2
+
3
+ type FirebaseConfig = {
4
+ apiKey: string;
5
+ authDomain: string;
6
+ databaseURL: string;
7
+ projectId: string;
8
+ storageBucket: string;
9
+ messagingSenderId: string;
10
+ appId: string;
11
+ measurementId?: string;
12
+ };
13
+ type FirebaseApiOptions = {
14
+ config: FirebaseConfig;
15
+ };
16
+ type Callback<T> = (key: string, data: T | null, snap: DataSnapshot) => void;
17
+ declare class FirebaseApi {
18
+ private db;
19
+ constructor(options: FirebaseApiOptions);
20
+ /** Get a DatabaseReference */
21
+ ref(path: string): DatabaseReference;
22
+ /** Read once */
23
+ getOnce<T = any>(path: string): Promise<T | null>;
24
+ /** Listen to value changes */
25
+ onValue<T = any>(path: string, cb: Callback<T>): Unsubscribe;
26
+ /** Listen to child-added */
27
+ onChildAdded<T = any>(path: string, cb: Callback<T>): Unsubscribe;
28
+ /** Other listeners... */
29
+ onChildChanged: <T>(path: string, cb: Callback<T>) => Unsubscribe;
30
+ onChildRemoved: <T>(path: string, cb: Callback<T>) => Unsubscribe;
31
+ /** Write/overwrite a node */
32
+ write(path: string, data: any): Promise<void>;
33
+ /** Update (merge) a node */
34
+ update(path: string, data: Partial<any>): Promise<void>;
35
+ /** Remove a node */
36
+ remove(path: string): Promise<void>;
37
+ /** Push a child with generated key */
38
+ push<T = any>(path: string, data: T): Promise<string>;
39
+ /** onDisconnect write */
40
+ onDisconnectUpdate(path: string, data: any): Promise<void>;
41
+ /** Cancel onDisconnect operations */
42
+ cancelOnDisconnect(path: string): Promise<void>;
43
+ /** Run a transaction */
44
+ transaction<T = any>(path: string, updateFn: (cur: T | null) => T): Promise<TransactionResult>;
45
+ /** Build a query */
46
+ query(path: string, opts?: {
47
+ orderBy?: "child" | "key" | "value";
48
+ childKey?: string;
49
+ limitFirst?: number;
50
+ limitLast?: number;
51
+ }): Query;
52
+ }
53
+
54
+ export { FirebaseApi, type FirebaseConfig };
@@ -0,0 +1,54 @@
1
+ import { DatabaseReference, DataSnapshot, Unsubscribe, TransactionResult, Query } from 'firebase/database';
2
+
3
+ type FirebaseConfig = {
4
+ apiKey: string;
5
+ authDomain: string;
6
+ databaseURL: string;
7
+ projectId: string;
8
+ storageBucket: string;
9
+ messagingSenderId: string;
10
+ appId: string;
11
+ measurementId?: string;
12
+ };
13
+ type FirebaseApiOptions = {
14
+ config: FirebaseConfig;
15
+ };
16
+ type Callback<T> = (key: string, data: T | null, snap: DataSnapshot) => void;
17
+ declare class FirebaseApi {
18
+ private db;
19
+ constructor(options: FirebaseApiOptions);
20
+ /** Get a DatabaseReference */
21
+ ref(path: string): DatabaseReference;
22
+ /** Read once */
23
+ getOnce<T = any>(path: string): Promise<T | null>;
24
+ /** Listen to value changes */
25
+ onValue<T = any>(path: string, cb: Callback<T>): Unsubscribe;
26
+ /** Listen to child-added */
27
+ onChildAdded<T = any>(path: string, cb: Callback<T>): Unsubscribe;
28
+ /** Other listeners... */
29
+ onChildChanged: <T>(path: string, cb: Callback<T>) => Unsubscribe;
30
+ onChildRemoved: <T>(path: string, cb: Callback<T>) => Unsubscribe;
31
+ /** Write/overwrite a node */
32
+ write(path: string, data: any): Promise<void>;
33
+ /** Update (merge) a node */
34
+ update(path: string, data: Partial<any>): Promise<void>;
35
+ /** Remove a node */
36
+ remove(path: string): Promise<void>;
37
+ /** Push a child with generated key */
38
+ push<T = any>(path: string, data: T): Promise<string>;
39
+ /** onDisconnect write */
40
+ onDisconnectUpdate(path: string, data: any): Promise<void>;
41
+ /** Cancel onDisconnect operations */
42
+ cancelOnDisconnect(path: string): Promise<void>;
43
+ /** Run a transaction */
44
+ transaction<T = any>(path: string, updateFn: (cur: T | null) => T): Promise<TransactionResult>;
45
+ /** Build a query */
46
+ query(path: string, opts?: {
47
+ orderBy?: "child" | "key" | "value";
48
+ childKey?: string;
49
+ limitFirst?: number;
50
+ limitLast?: number;
51
+ }): Query;
52
+ }
53
+
54
+ export { FirebaseApi, type FirebaseConfig };
@@ -0,0 +1,105 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
5
+ // services/firebase/firebase-api.ts
6
+ import { initializeApp } from "firebase/app";
7
+ import {
8
+ query as dbQuery,
9
+ get,
10
+ getDatabase,
11
+ limitToFirst,
12
+ limitToLast,
13
+ onChildAdded,
14
+ onChildChanged,
15
+ onChildRemoved,
16
+ onDisconnect,
17
+ onValue,
18
+ orderByChild,
19
+ orderByKey,
20
+ orderByValue,
21
+ push,
22
+ ref,
23
+ remove,
24
+ runTransaction,
25
+ set,
26
+ update
27
+ } from "firebase/database";
28
+ var FirebaseApi = class {
29
+ constructor(options) {
30
+ __publicField(this, "db");
31
+ /** Other listeners... */
32
+ __publicField(this, "onChildChanged", (path, cb) => onChildChanged(this.ref(path), (snap) => cb(snap.key, snap.val(), snap)));
33
+ __publicField(this, "onChildRemoved", (path, cb) => onChildRemoved(this.ref(path), (snap) => cb(snap.key, snap.val(), snap)));
34
+ const app = initializeApp(options.config);
35
+ this.db = getDatabase(app);
36
+ }
37
+ /** Get a DatabaseReference */
38
+ ref(path) {
39
+ return ref(this.db, path);
40
+ }
41
+ /** Read once */
42
+ async getOnce(path) {
43
+ const snap = await get(this.ref(path));
44
+ return snap.exists() ? snap.val() : null;
45
+ }
46
+ /** Listen to value changes */
47
+ onValue(path, cb) {
48
+ return onValue(
49
+ this.ref(path),
50
+ (snap) => cb(snap.key ?? "", snap.val(), snap),
51
+ (err) => console.error("onValue error", err)
52
+ );
53
+ }
54
+ /** Listen to child-added */
55
+ onChildAdded(path, cb) {
56
+ return onChildAdded(
57
+ this.ref(path),
58
+ (snap) => cb(snap.key, snap.val(), snap),
59
+ (err) => console.error("onChildAdded error", err)
60
+ );
61
+ }
62
+ /** Write/overwrite a node */
63
+ write(path, data) {
64
+ return set(this.ref(path), data);
65
+ }
66
+ /** Update (merge) a node */
67
+ update(path, data) {
68
+ return update(this.ref(path), data);
69
+ }
70
+ /** Remove a node */
71
+ remove(path) {
72
+ return remove(this.ref(path));
73
+ }
74
+ /** Push a child with generated key */
75
+ async push(path, data) {
76
+ const newRef = push(this.ref(path));
77
+ await set(newRef, data);
78
+ return newRef.key;
79
+ }
80
+ /** onDisconnect write */
81
+ onDisconnectUpdate(path, data) {
82
+ return onDisconnect(this.ref(path)).update(data);
83
+ }
84
+ /** Cancel onDisconnect operations */
85
+ cancelOnDisconnect(path) {
86
+ return onDisconnect(this.ref(path)).cancel();
87
+ }
88
+ /** Run a transaction */
89
+ transaction(path, updateFn) {
90
+ return runTransaction(this.ref(path), (cur) => updateFn(cur));
91
+ }
92
+ /** Build a query */
93
+ query(path, opts = {}) {
94
+ let q = this.ref(path);
95
+ if (opts.orderBy === "child" && opts.childKey) q = dbQuery(q, orderByChild(opts.childKey));
96
+ if (opts.orderBy === "key") q = dbQuery(q, orderByKey());
97
+ if (opts.orderBy === "value") q = dbQuery(q, orderByValue());
98
+ if (opts.limitFirst != null) q = dbQuery(q, limitToFirst(opts.limitFirst));
99
+ if (opts.limitLast != null) q = dbQuery(q, limitToLast(opts.limitLast));
100
+ return q;
101
+ }
102
+ };
103
+ export {
104
+ FirebaseApi
105
+ };