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