@digitalaidseattle/firebase 1.0.9 → 1.0.12

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.
@@ -3,36 +3,36 @@ import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
3
3
  import _createClass from '@babel/runtime/helpers/esm/createClass';
4
4
  import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
5
5
  import _regeneratorRuntime from '@babel/runtime/regenerator';
6
- import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth';
6
+ import { onAuthStateChanged, GoogleAuthProvider, signInWithPopup, getAuth } from 'firebase/auth';
7
7
  import { initializeApp } from 'firebase/app';
8
- import { ref, getDownloadURL, getStorage, getBytes } from 'firebase/storage';
8
+ import { ref, getDownloadURL, getBytes, deleteObject, getMetadata, listAll, uploadBytes, getStorage } from 'firebase/storage';
9
9
  import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
10
- import { getDocs, collection, getDoc, doc, addDoc, updateDoc, deleteDoc, getFirestore } from 'firebase/firestore';
11
-
12
- var firebaseConfig = {
13
- apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
14
- authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
15
- projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
16
- storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
17
- messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
18
- appId: import.meta.env.VITE_FIREBASE_APP_ID,
19
- measurementId: import.meta.env.VITE_FIREBASE_MEASUREMENT_ID
20
- };
21
-
22
- // Initialize Firebase
23
- var firebaseClient = initializeApp(firebaseConfig);
10
+ import { getDocs, collection, getDoc, doc, addDoc, updateDoc, deleteDoc, setDoc, getFirestore } from 'firebase/firestore';
11
+ import { firebaseClient as firebaseClient$1 } from '@digitalaidseattle/firebase';
12
+ import { getGenerativeModel, Schema, getAI, GoogleAIBackend } from 'firebase/ai';
24
13
 
25
14
  var FirebaseAuthService = /*#__PURE__*/function () {
26
- function FirebaseAuthService() {
15
+ function FirebaseAuthService(firebaseClient) {
27
16
  var _this = this;
28
17
  _classCallCheck(this, FirebaseAuthService);
29
- _defineProperty(this, "currentUser", undefined);
30
- _defineProperty(this, "auth", getAuth(firebaseClient));
31
18
  _defineProperty(this, "getUser", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
32
19
  return _regeneratorRuntime.wrap(function (_context) {
33
20
  while (1) switch (_context.prev = _context.next) {
34
21
  case 0:
35
- return _context.abrupt("return", _this.currentUser);
22
+ return _context.abrupt("return", new Promise(function (resolve) {
23
+ var unsubscribe = onAuthStateChanged(_this.auth, function (gUser) {
24
+ unsubscribe(); // stop listening after first call
25
+ var user = gUser ? {
26
+ email: gUser.email,
27
+ user_metadata: {
28
+ name: gUser.displayName,
29
+ avatar_url: gUser.photoURL,
30
+ email: gUser.email
31
+ }
32
+ } : null;
33
+ resolve(user);
34
+ });
35
+ }));
36
36
  case 1:
37
37
  case "end":
38
38
  return _context.stop();
@@ -43,19 +43,20 @@ var FirebaseAuthService = /*#__PURE__*/function () {
43
43
  return _regeneratorRuntime.wrap(function (_context2) {
44
44
  while (1) switch (_context2.prev = _context2.next) {
45
45
  case 0:
46
- _this.currentUser = undefined;
47
- _this.auth.signOut();
46
+ _context2.next = 1;
47
+ return _this.auth.signOut();
48
+ case 1:
48
49
  return _context2.abrupt("return", {
49
50
  error: null
50
51
  });
51
- case 1:
52
+ case 2:
52
53
  case "end":
53
54
  return _context2.stop();
54
55
  }
55
56
  }, _callee2);
56
57
  })));
57
58
  _defineProperty(this, "signInWithGoogle", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
58
- var provider, resp, _t;
59
+ var provider, _t;
59
60
  return _regeneratorRuntime.wrap(function (_context3) {
60
61
  while (1) switch (_context3.prev = _context3.next) {
61
62
  case 0:
@@ -64,15 +65,6 @@ var FirebaseAuthService = /*#__PURE__*/function () {
64
65
  _context3.next = 1;
65
66
  return signInWithPopup(_this.auth, provider);
66
67
  case 1:
67
- resp = _context3.sent;
68
- _this.currentUser = {
69
- email: resp.user.email,
70
- user_metadata: {
71
- name: resp.user.displayName,
72
- avatar_url: resp.user.photoURL,
73
- email: resp.user.email
74
- }
75
- };
76
68
  return _context3.abrupt("return", {
77
69
  data: {
78
70
  url: import.meta.env.VITE_AUTH_DOMAIN
@@ -81,25 +73,15 @@ var FirebaseAuthService = /*#__PURE__*/function () {
81
73
  case 2:
82
74
  _context3.prev = 2;
83
75
  _t = _context3["catch"](0);
84
- console.error(_t);
76
+ console.error('signInWithGoogle', _t);
77
+ throw _t;
85
78
  case 3:
86
79
  case "end":
87
80
  return _context3.stop();
88
81
  }
89
82
  }, _callee3, null, [[0, 2]]);
90
83
  })));
91
- this.auth.onAuthStateChanged(function (user) {
92
- if (user) {
93
- _this.currentUser = {
94
- email: user.email,
95
- user_metadata: {
96
- name: user.displayName,
97
- avatar_url: user.photoURL,
98
- email: user.email
99
- }
100
- };
101
- }
102
- });
84
+ this.auth = getAuth(firebaseClient);
103
85
  }
104
86
  return _createClass(FirebaseAuthService, [{
105
87
  key: "getProviders",
@@ -119,17 +101,34 @@ var FirebaseAuthService = /*#__PURE__*/function () {
119
101
  }, {
120
102
  key: "hasUser",
121
103
  value: function hasUser() {
122
- return Promise.resolve(this.currentUser !== null);
104
+ var _this2 = this;
105
+ return new Promise(function (resolve) {
106
+ var unsubscribe = onAuthStateChanged(_this2.auth, function (user) {
107
+ unsubscribe(); // stop listening after first call
108
+ resolve(user ? true : false);
109
+ });
110
+ });
123
111
  }
124
112
  }]);
125
113
  }();
126
114
 
115
+ var firebaseConfig = {
116
+ apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
117
+ authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
118
+ projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
119
+ storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
120
+ messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
121
+ appId: import.meta.env.VITE_FIREBASE_APP_ID,
122
+ measurementId: import.meta.env.VITE_FIREBASE_MEASUREMENT_ID
123
+ };
124
+
125
+ // Initialize Firebase
126
+ var firebaseClient = initializeApp(firebaseConfig);
127
+
127
128
  var FirebaseStorageService = /*#__PURE__*/function () {
128
- function FirebaseStorageService() {
129
+ function FirebaseStorageService(firebaseClient) {
129
130
  var _this = this;
130
131
  _classCallCheck(this, FirebaseStorageService);
131
- _defineProperty(this, "storage", getStorage(firebaseClient));
132
- _defineProperty(this, "decoder", new TextDecoder("utf-8"));
133
132
  _defineProperty(this, "downloadFile", /*#__PURE__*/function () {
134
133
  var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(filepath) {
135
134
  var fileRef;
@@ -150,6 +149,8 @@ var FirebaseStorageService = /*#__PURE__*/function () {
150
149
  return _ref.apply(this, arguments);
151
150
  };
152
151
  }());
152
+ this.storage = getStorage(firebaseClient);
153
+ this.decoder = new TextDecoder("utf-8");
153
154
  }
154
155
  return _createClass(FirebaseStorageService, [{
155
156
  key: "getUrlAsync",
@@ -181,31 +182,128 @@ var FirebaseStorageService = /*#__PURE__*/function () {
181
182
  }
182
183
  return getUrlAsync;
183
184
  }()
184
- }, {
185
- key: "list",
186
- value: function list(filepath) {
187
- throw new Error("Method not implemented.");
188
- }
189
185
  }, {
190
186
  key: "getUrl",
191
187
  value: function getUrl(filepath) {
192
188
  throw new Error("Method not supported.");
193
189
  }
194
- }, {
195
- key: "upload",
196
- value: function upload(path, blob) {
197
- throw new Error("Method not implemented.");
198
- }
199
190
  }, {
200
191
  key: "downloadBlob",
201
192
  value: function downloadBlob(filepath) {
202
- throw new Error("Method not implemented.");
193
+ var fileRef = ref(this.storage, filepath);
194
+ return getBytes(fileRef).then(function (bytes) {
195
+ return new Blob([bytes]);
196
+ });
203
197
  }
204
198
  }, {
205
199
  key: "removeFile",
206
- value: function removeFile(fileName) {
207
- throw new Error("Method not implemented.");
200
+ value: function removeFile(path) {
201
+ try {
202
+ var fileRef = ref(this.storage, path);
203
+ return deleteObject(fileRef);
204
+ } catch (error) {
205
+ if (error.code === "storage/object-not-found") {
206
+ console.warn("File does not exist");
207
+ } else if (error.code === "storage/unauthorized") {
208
+ console.error("User not authorized to delete file");
209
+ } else {
210
+ console.error("Delete failed:", error);
211
+ }
212
+ throw error;
213
+ }
208
214
  }
215
+ }, {
216
+ key: "list",
217
+ value: function () {
218
+ var _list = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee4(filepath) {
219
+ var folderRef, result, files;
220
+ return _regeneratorRuntime.wrap(function (_context4) {
221
+ while (1) switch (_context4.prev = _context4.next) {
222
+ case 0:
223
+ if (filepath) {
224
+ _context4.next = 1;
225
+ break;
226
+ }
227
+ return _context4.abrupt("return", []);
228
+ case 1:
229
+ folderRef = ref(this.storage, filepath);
230
+ _context4.next = 2;
231
+ return listAll(folderRef);
232
+ case 2:
233
+ result = _context4.sent;
234
+ _context4.next = 3;
235
+ return Promise.all(result.items.map(/*#__PURE__*/function () {
236
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(item) {
237
+ var metadata;
238
+ return _regeneratorRuntime.wrap(function (_context3) {
239
+ while (1) switch (_context3.prev = _context3.next) {
240
+ case 0:
241
+ _context3.next = 1;
242
+ return getMetadata(item);
243
+ case 1:
244
+ metadata = _context3.sent;
245
+ return _context3.abrupt("return", {
246
+ name: item.name,
247
+ fullPath: item.fullPath,
248
+ type: metadata.contentType,
249
+ size: metadata.size,
250
+ updated: metadata.updated
251
+ });
252
+ case 2:
253
+ case "end":
254
+ return _context3.stop();
255
+ }
256
+ }, _callee3);
257
+ }));
258
+ return function (_x4) {
259
+ return _ref2.apply(this, arguments);
260
+ };
261
+ }()));
262
+ case 3:
263
+ files = _context4.sent;
264
+ return _context4.abrupt("return", files);
265
+ case 4:
266
+ case "end":
267
+ return _context4.stop();
268
+ }
269
+ }, _callee4, this);
270
+ }));
271
+ function list(_x3) {
272
+ return _list.apply(this, arguments);
273
+ }
274
+ return list;
275
+ }()
276
+ }, {
277
+ key: "upload",
278
+ value: function () {
279
+ var _upload = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5(path, file) {
280
+ var storageRef, snapshot, downloadUrl;
281
+ return _regeneratorRuntime.wrap(function (_context5) {
282
+ while (1) switch (_context5.prev = _context5.next) {
283
+ case 0:
284
+ storageRef = ref(this.storage, path); // Upload file
285
+ _context5.next = 1;
286
+ return uploadBytes(storageRef, file, {
287
+ contentType: file === null || file === void 0 ? void 0 : file.type
288
+ });
289
+ case 1:
290
+ snapshot = _context5.sent;
291
+ _context5.next = 2;
292
+ return getDownloadURL(snapshot.ref);
293
+ case 2:
294
+ downloadUrl = _context5.sent;
295
+ return _context5.abrupt("return", downloadUrl);
296
+ case 3:
297
+ case "end":
298
+ return _context5.stop();
299
+ }
300
+ }, _callee5, this);
301
+ }));
302
+ function upload(_x5, _x6) {
303
+ return _upload.apply(this, arguments);
304
+ }
305
+ return upload;
306
+ }()
209
307
  }]);
210
308
  }();
211
309
 
@@ -397,7 +495,108 @@ var FirestoreService = /*#__PURE__*/function () {
397
495
  }
398
496
  return _delete;
399
497
  }()
498
+ }, {
499
+ key: "upsert",
500
+ value: function () {
501
+ var _upsert = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee7(entity) {
502
+ return _regeneratorRuntime.wrap(function (_context7) {
503
+ while (1) switch (_context7.prev = _context7.next) {
504
+ case 0:
505
+ _context7.next = 1;
506
+ return setDoc(doc(this.db, this.collectionName, entity.id), entity);
507
+ case 1:
508
+ return _context7.abrupt("return", entity);
509
+ case 2:
510
+ case "end":
511
+ return _context7.stop();
512
+ }
513
+ }, _callee7, this);
514
+ }));
515
+ function upsert(_x19) {
516
+ return _upsert.apply(this, arguments);
517
+ }
518
+ return upsert;
519
+ }()
520
+ }]);
521
+ }();
522
+
523
+ var GeminiService = /*#__PURE__*/function () {
524
+ function GeminiService(modelType) {
525
+ _classCallCheck(this, GeminiService);
526
+ this.ai = getAI(firebaseClient$1, {
527
+ backend: new GoogleAIBackend()
528
+ });
529
+ }
530
+ return _createClass(GeminiService, [{
531
+ key: "getModels",
532
+ value: function getModels() {
533
+ return ["gemini-2.5-flash", "gemini-2.5-pro", "gemini-2.5-flash-lite"];
534
+ }
535
+ }, {
536
+ key: "calcTokenCount",
537
+ value: function calcTokenCount(model, prompt) {
538
+ return getGenerativeModel(this.ai, {
539
+ model: model
540
+ }).countTokens(prompt).then(function (response) {
541
+ return response.totalTokens;
542
+ });
543
+ }
544
+
545
+ // Wrap in an async function so you can use await
546
+ }, {
547
+ key: "generateContent",
548
+ value: function generateContent(model, prompt) {
549
+ // To generate text output, call generateContent with the text input
550
+ console.log('generateContent', model, prompt);
551
+ return getGenerativeModel(this.ai, {
552
+ model: model
553
+ }).generateContent(prompt).then(function (result) {
554
+ return result.response.text();
555
+ })["catch"](function (error) {
556
+ console.error("Error querying AI: ", error);
557
+ throw new Error("Failed to query AI: " + error.message);
558
+ });
559
+ }
560
+
561
+ // Wrap in an async function so you can use await
562
+ }, {
563
+ key: "generateParameterizedContent",
564
+ value: function generateParameterizedContent(model, prompt, schemaParams) {
565
+ // Provide a JSON schema object using a standard format.
566
+ // Later, pass this schema object into `responseSchema` in the generation config.
567
+ var schema = Schema.object({
568
+ properties: {
569
+ characters: Schema.array({
570
+ items: Schema.object({
571
+ properties: Object.fromEntries(schemaParams.map(function (field) {
572
+ return [field, Schema.string()];
573
+ }))
574
+ })
575
+ })
576
+ }
577
+ });
578
+
579
+ // Create a `GenerativeModel` instance with a model that supports your use case
580
+ var jModel = getGenerativeModel(this.ai, {
581
+ model: model,
582
+ // In the generation config, set the `responseMimeType` to `application/json`
583
+ // and pass the JSON schema object into `responseSchema`.
584
+ generationConfig: {
585
+ responseMimeType: "application/json",
586
+ responseSchema: schema
587
+ }
588
+ });
589
+
590
+ // To generate text output, call generateContent with the text input
591
+ return jModel.generateContent(prompt).then(function (result) {
592
+ var content = result.response.text();
593
+ return JSON.parse(content).characters[0];
594
+ })["catch"](function (error) {
595
+ console.error("Error querying AI: ", error);
596
+ throw new Error("Failed to query AI: " + error.message);
597
+ });
598
+ }
400
599
  }]);
401
600
  }();
402
601
 
403
- export { FirebaseAuthService, FirebaseStorageService, FirestoreService, firebaseClient };
602
+ export { FirebaseAuthService, FirebaseStorageService, FirestoreService, GeminiService, firebaseClient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitalaidseattle/firebase",
3
- "version": "1.0.9",
3
+ "version": "1.0.12",
4
4
  "description": "Wrapper for firebase that works with DAS Component library",
5
5
  "repository": "null//github.com/null/github.com/tree/master/packages/firebase",
6
6
  "main": "dist/digitalaidseattle-firebase.cjs.js",
@@ -8,10 +8,10 @@
8
8
  "types": "dist/declarations/src/index.d.ts",
9
9
  "dependencies": {
10
10
  "@babel/runtime": "^7.25.0",
11
- "@digitalaidseattle/core": "1.0.14",
11
+ "@digitalaidseattle/core": "1.0.17",
12
12
  "firebase": "^11.2.0",
13
13
  "react": "^18.3.1",
14
- "uuid": "^11.0.5"
14
+ "uuid": "^13.0.0"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@testing-library/react": "^14.2.1",