@digitalaidseattle/firebase 1.0.10 → 1.0.13

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.
@@ -12,37 +12,33 @@ var app = require('firebase/app');
12
12
  var storage = require('firebase/storage');
13
13
  var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
14
14
  var firestore = require('firebase/firestore');
15
- var firebase = require('@digitalaidseattle/firebase');
16
- var ai = require('firebase/ai');
17
15
 
18
16
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
19
17
 
20
18
  var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
21
19
 
22
- var firebaseConfig = {
23
- apiKey: undefined.VITE_FIREBASE_API_KEY,
24
- authDomain: undefined.VITE_FIREBASE_AUTH_DOMAIN,
25
- projectId: undefined.VITE_FIREBASE_PROJECT_ID,
26
- storageBucket: undefined.VITE_FIREBASE_STORAGE_BUCKET,
27
- messagingSenderId: undefined.VITE_FIREBASE_MESSAGING_SENDER_ID,
28
- appId: undefined.VITE_FIREBASE_APP_ID,
29
- measurementId: undefined.VITE_FIREBASE_MEASUREMENT_ID
30
- };
31
-
32
- // Initialize Firebase
33
- var firebaseClient = app.initializeApp(firebaseConfig);
34
-
35
20
  var FirebaseAuthService = /*#__PURE__*/function () {
36
- function FirebaseAuthService() {
21
+ function FirebaseAuthService(firebaseClient) {
37
22
  var _this = this;
38
23
  _classCallCheck(this, FirebaseAuthService);
39
- _defineProperty(this, "currentUser", undefined);
40
- _defineProperty(this, "auth", auth.getAuth(firebaseClient));
41
24
  _defineProperty(this, "getUser", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
42
25
  return _regeneratorRuntime__default["default"].wrap(function (_context) {
43
26
  while (1) switch (_context.prev = _context.next) {
44
27
  case 0:
45
- return _context.abrupt("return", _this.currentUser);
28
+ return _context.abrupt("return", new Promise(function (resolve) {
29
+ var unsubscribe = auth.onAuthStateChanged(_this.auth, function (gUser) {
30
+ unsubscribe(); // stop listening after first call
31
+ var user = gUser ? {
32
+ email: gUser.email,
33
+ user_metadata: {
34
+ name: gUser.displayName,
35
+ avatar_url: gUser.photoURL,
36
+ email: gUser.email
37
+ }
38
+ } : null;
39
+ resolve(user);
40
+ });
41
+ }));
46
42
  case 1:
47
43
  case "end":
48
44
  return _context.stop();
@@ -53,19 +49,20 @@ var FirebaseAuthService = /*#__PURE__*/function () {
53
49
  return _regeneratorRuntime__default["default"].wrap(function (_context2) {
54
50
  while (1) switch (_context2.prev = _context2.next) {
55
51
  case 0:
56
- _this.currentUser = undefined;
57
- _this.auth.signOut();
52
+ _context2.next = 1;
53
+ return _this.auth.signOut();
54
+ case 1:
58
55
  return _context2.abrupt("return", {
59
56
  error: null
60
57
  });
61
- case 1:
58
+ case 2:
62
59
  case "end":
63
60
  return _context2.stop();
64
61
  }
65
62
  }, _callee2);
66
63
  })));
67
64
  _defineProperty(this, "signInWithGoogle", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
68
- var provider, resp, _t;
65
+ var provider, _t;
69
66
  return _regeneratorRuntime__default["default"].wrap(function (_context3) {
70
67
  while (1) switch (_context3.prev = _context3.next) {
71
68
  case 0:
@@ -74,15 +71,6 @@ var FirebaseAuthService = /*#__PURE__*/function () {
74
71
  _context3.next = 1;
75
72
  return auth.signInWithPopup(_this.auth, provider);
76
73
  case 1:
77
- resp = _context3.sent;
78
- _this.currentUser = {
79
- email: resp.user.email,
80
- user_metadata: {
81
- name: resp.user.displayName,
82
- avatar_url: resp.user.photoURL,
83
- email: resp.user.email
84
- }
85
- };
86
74
  return _context3.abrupt("return", {
87
75
  data: {
88
76
  url: undefined.VITE_AUTH_DOMAIN
@@ -99,18 +87,7 @@ var FirebaseAuthService = /*#__PURE__*/function () {
99
87
  }
100
88
  }, _callee3, null, [[0, 2]]);
101
89
  })));
102
- this.auth.onAuthStateChanged(function (user) {
103
- if (user) {
104
- _this.currentUser = {
105
- email: user.email,
106
- user_metadata: {
107
- name: user.displayName,
108
- avatar_url: user.photoURL,
109
- email: user.email
110
- }
111
- };
112
- }
113
- });
90
+ this.auth = auth.getAuth(firebaseClient);
114
91
  }
115
92
  return _createClass(FirebaseAuthService, [{
116
93
  key: "getProviders",
@@ -130,17 +107,34 @@ var FirebaseAuthService = /*#__PURE__*/function () {
130
107
  }, {
131
108
  key: "hasUser",
132
109
  value: function hasUser() {
133
- return Promise.resolve(this.currentUser !== null);
110
+ var _this2 = this;
111
+ return new Promise(function (resolve) {
112
+ var unsubscribe = auth.onAuthStateChanged(_this2.auth, function (user) {
113
+ unsubscribe(); // stop listening after first call
114
+ resolve(user ? true : false);
115
+ });
116
+ });
134
117
  }
135
118
  }]);
136
119
  }();
137
120
 
121
+ var firebaseConfig = {
122
+ apiKey: undefined.VITE_FIREBASE_API_KEY,
123
+ authDomain: undefined.VITE_FIREBASE_AUTH_DOMAIN,
124
+ projectId: undefined.VITE_FIREBASE_PROJECT_ID,
125
+ storageBucket: undefined.VITE_FIREBASE_STORAGE_BUCKET,
126
+ messagingSenderId: undefined.VITE_FIREBASE_MESSAGING_SENDER_ID,
127
+ appId: undefined.VITE_FIREBASE_APP_ID,
128
+ measurementId: undefined.VITE_FIREBASE_MEASUREMENT_ID
129
+ };
130
+
131
+ // Initialize Firebase
132
+ var firebaseClient = app.initializeApp(firebaseConfig);
133
+
138
134
  var FirebaseStorageService = /*#__PURE__*/function () {
139
- function FirebaseStorageService() {
135
+ function FirebaseStorageService(firebaseClient) {
140
136
  var _this = this;
141
137
  _classCallCheck(this, FirebaseStorageService);
142
- _defineProperty(this, "storage", storage.getStorage(firebaseClient));
143
- _defineProperty(this, "decoder", new TextDecoder("utf-8"));
144
138
  _defineProperty(this, "downloadFile", /*#__PURE__*/function () {
145
139
  var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee(filepath) {
146
140
  var fileRef;
@@ -161,6 +155,8 @@ var FirebaseStorageService = /*#__PURE__*/function () {
161
155
  return _ref.apply(this, arguments);
162
156
  };
163
157
  }());
158
+ this.storage = storage.getStorage(firebaseClient);
159
+ this.decoder = new TextDecoder("utf-8");
164
160
  }
165
161
  return _createClass(FirebaseStorageService, [{
166
162
  key: "getUrlAsync",
@@ -192,40 +188,136 @@ var FirebaseStorageService = /*#__PURE__*/function () {
192
188
  }
193
189
  return getUrlAsync;
194
190
  }()
195
- }, {
196
- key: "list",
197
- value: function list(filepath) {
198
- throw new Error("Method not implemented.");
199
- }
200
191
  }, {
201
192
  key: "getUrl",
202
193
  value: function getUrl(filepath) {
203
194
  throw new Error("Method not supported.");
204
195
  }
205
- }, {
206
- key: "upload",
207
- value: function upload(path, blob) {
208
- throw new Error("Method not implemented.");
209
- }
210
196
  }, {
211
197
  key: "downloadBlob",
212
198
  value: function downloadBlob(filepath) {
213
- throw new Error("Method not implemented.");
199
+ var fileRef = storage.ref(this.storage, filepath);
200
+ return storage.getBytes(fileRef).then(function (bytes) {
201
+ return new Blob([bytes]);
202
+ });
214
203
  }
215
204
  }, {
216
205
  key: "removeFile",
217
- value: function removeFile(fileName) {
218
- throw new Error("Method not implemented.");
206
+ value: function removeFile(path) {
207
+ try {
208
+ var fileRef = storage.ref(this.storage, path);
209
+ return storage.deleteObject(fileRef);
210
+ } catch (error) {
211
+ if (error.code === "storage/object-not-found") {
212
+ console.warn("File does not exist");
213
+ } else if (error.code === "storage/unauthorized") {
214
+ console.error("User not authorized to delete file");
215
+ } else {
216
+ console.error("Delete failed:", error);
217
+ }
218
+ throw error;
219
+ }
219
220
  }
221
+ }, {
222
+ key: "list",
223
+ value: function () {
224
+ var _list = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4(filepath) {
225
+ var folderRef, result, files;
226
+ return _regeneratorRuntime__default["default"].wrap(function (_context4) {
227
+ while (1) switch (_context4.prev = _context4.next) {
228
+ case 0:
229
+ if (filepath) {
230
+ _context4.next = 1;
231
+ break;
232
+ }
233
+ return _context4.abrupt("return", []);
234
+ case 1:
235
+ folderRef = storage.ref(this.storage, filepath);
236
+ _context4.next = 2;
237
+ return storage.listAll(folderRef);
238
+ case 2:
239
+ result = _context4.sent;
240
+ _context4.next = 3;
241
+ return Promise.all(result.items.map(/*#__PURE__*/function () {
242
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3(item) {
243
+ var metadata;
244
+ return _regeneratorRuntime__default["default"].wrap(function (_context3) {
245
+ while (1) switch (_context3.prev = _context3.next) {
246
+ case 0:
247
+ _context3.next = 1;
248
+ return storage.getMetadata(item);
249
+ case 1:
250
+ metadata = _context3.sent;
251
+ return _context3.abrupt("return", {
252
+ name: item.name,
253
+ fullPath: item.fullPath,
254
+ type: metadata.contentType,
255
+ size: metadata.size,
256
+ updated: metadata.updated
257
+ });
258
+ case 2:
259
+ case "end":
260
+ return _context3.stop();
261
+ }
262
+ }, _callee3);
263
+ }));
264
+ return function (_x4) {
265
+ return _ref2.apply(this, arguments);
266
+ };
267
+ }()));
268
+ case 3:
269
+ files = _context4.sent;
270
+ return _context4.abrupt("return", files);
271
+ case 4:
272
+ case "end":
273
+ return _context4.stop();
274
+ }
275
+ }, _callee4, this);
276
+ }));
277
+ function list(_x3) {
278
+ return _list.apply(this, arguments);
279
+ }
280
+ return list;
281
+ }()
282
+ }, {
283
+ key: "upload",
284
+ value: function () {
285
+ var _upload = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee5(path, file) {
286
+ var storageRef, snapshot, downloadUrl;
287
+ return _regeneratorRuntime__default["default"].wrap(function (_context5) {
288
+ while (1) switch (_context5.prev = _context5.next) {
289
+ case 0:
290
+ storageRef = storage.ref(this.storage, path); // Upload file
291
+ _context5.next = 1;
292
+ return storage.uploadBytes(storageRef, file, {
293
+ contentType: file === null || file === void 0 ? void 0 : file.type
294
+ });
295
+ case 1:
296
+ snapshot = _context5.sent;
297
+ _context5.next = 2;
298
+ return storage.getDownloadURL(snapshot.ref);
299
+ case 2:
300
+ downloadUrl = _context5.sent;
301
+ return _context5.abrupt("return", downloadUrl);
302
+ case 3:
303
+ case "end":
304
+ return _context5.stop();
305
+ }
306
+ }, _callee5, this);
307
+ }));
308
+ function upload(_x5, _x6) {
309
+ return _upload.apply(this, arguments);
310
+ }
311
+ return upload;
312
+ }()
220
313
  }]);
221
314
  }();
222
315
 
223
316
  var FirestoreService = /*#__PURE__*/function () {
224
- function FirestoreService(collectionName) {
317
+ function FirestoreService(collectionName, firebaseClient$1) {
225
318
  _classCallCheck(this, FirestoreService);
226
- _defineProperty(this, "collectionName", "player");
227
- _defineProperty(this, "db", firestore.getFirestore(firebaseClient));
228
319
  this.collectionName = collectionName;
320
+ this.db = firestore.getFirestore(firebaseClient$1 !== null && firebaseClient$1 !== void 0 ? firebaseClient$1 : firebaseClient);
229
321
  }
230
322
 
231
323
  // Get all documents from a collection
@@ -395,7 +487,6 @@ var FirestoreService = /*#__PURE__*/function () {
395
487
  return _regeneratorRuntime__default["default"].wrap(function (_context6) {
396
488
  while (1) switch (_context6.prev = _context6.next) {
397
489
  case 0:
398
- console.log('delete', entityId);
399
490
  return _context6.abrupt("return", firestore.deleteDoc(firestore.doc(this.db, this.collectionName, entityId)));
400
491
  case 1:
401
492
  case "end":
@@ -408,90 +499,32 @@ var FirestoreService = /*#__PURE__*/function () {
408
499
  }
409
500
  return _delete;
410
501
  }()
411
- }]);
412
- }();
413
-
414
- var GeminiService = /*#__PURE__*/function () {
415
- function GeminiService(modelType) {
416
- _classCallCheck(this, GeminiService);
417
- this.ai = ai.getAI(firebase.firebaseClient, {
418
- backend: new ai.GoogleAIBackend()
419
- });
420
- }
421
- return _createClass(GeminiService, [{
422
- key: "getModels",
423
- value: function getModels() {
424
- return ["gemini-2.5-flash", "gemini-2.5-pro", "gemini-2.5-flash-lite"];
425
- }
426
502
  }, {
427
- key: "calcTokenCount",
428
- value: function calcTokenCount(model, prompt) {
429
- return ai.getGenerativeModel(this.ai, {
430
- model: model
431
- }).countTokens(prompt).then(function (response) {
432
- return response.totalTokens;
433
- });
434
- }
435
-
436
- // Wrap in an async function so you can use await
437
- }, {
438
- key: "generateContent",
439
- value: function generateContent(model, prompt) {
440
- // To generate text output, call generateContent with the text input
441
- console.log('generateContent', model, prompt);
442
- return ai.getGenerativeModel(this.ai, {
443
- model: model
444
- }).generateContent(prompt).then(function (result) {
445
- return result.response.text();
446
- })["catch"](function (error) {
447
- console.error("Error querying AI: ", error);
448
- throw new Error("Failed to query AI: " + error.message);
449
- });
450
- }
451
-
452
- // Wrap in an async function so you can use await
453
- }, {
454
- key: "generateParameterizedContent",
455
- value: function generateParameterizedContent(model, prompt, schemaParams) {
456
- // Provide a JSON schema object using a standard format.
457
- // Later, pass this schema object into `responseSchema` in the generation config.
458
- var schema = ai.Schema.object({
459
- properties: {
460
- characters: ai.Schema.array({
461
- items: ai.Schema.object({
462
- properties: Object.fromEntries(schemaParams.map(function (field) {
463
- return [field, ai.Schema.string()];
464
- }))
465
- })
466
- })
467
- }
468
- });
469
-
470
- // Create a `GenerativeModel` instance with a model that supports your use case
471
- var jModel = ai.getGenerativeModel(this.ai, {
472
- model: model,
473
- // In the generation config, set the `responseMimeType` to `application/json`
474
- // and pass the JSON schema object into `responseSchema`.
475
- generationConfig: {
476
- responseMimeType: "application/json",
477
- responseSchema: schema
478
- }
479
- });
480
-
481
- // To generate text output, call generateContent with the text input
482
- return jModel.generateContent(prompt).then(function (result) {
483
- var content = result.response.text();
484
- return JSON.parse(content).characters[0];
485
- })["catch"](function (error) {
486
- console.error("Error querying AI: ", error);
487
- throw new Error("Failed to query AI: " + error.message);
488
- });
489
- }
503
+ key: "upsert",
504
+ value: function () {
505
+ var _upsert = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee7(entity) {
506
+ return _regeneratorRuntime__default["default"].wrap(function (_context7) {
507
+ while (1) switch (_context7.prev = _context7.next) {
508
+ case 0:
509
+ _context7.next = 1;
510
+ return firestore.setDoc(firestore.doc(this.db, this.collectionName, entity.id), entity);
511
+ case 1:
512
+ return _context7.abrupt("return", entity);
513
+ case 2:
514
+ case "end":
515
+ return _context7.stop();
516
+ }
517
+ }, _callee7, this);
518
+ }));
519
+ function upsert(_x19) {
520
+ return _upsert.apply(this, arguments);
521
+ }
522
+ return upsert;
523
+ }()
490
524
  }]);
491
525
  }();
492
526
 
493
527
  exports.FirebaseAuthService = FirebaseAuthService;
494
528
  exports.FirebaseStorageService = FirebaseStorageService;
495
529
  exports.FirestoreService = FirestoreService;
496
- exports.GeminiService = GeminiService;
497
530
  exports.firebaseClient = firebaseClient;