@sofiakb/fireblaze-ts 2.0.0-dev.5 → 2.0.1

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,16 +3,19 @@ import { Auth } from "firebase/auth";
3
3
  import { FirebaseStorage } from "firebase/storage";
4
4
  import FirebaseConfig from "./firebase-config";
5
5
  export default class Firebase {
6
+ private static instance;
7
+ private static appInstance;
8
+ private static authInstance;
9
+ private static storageInstance;
6
10
  config: FirebaseConfig;
7
11
  app: FirebaseApp;
8
12
  auth?: Auth;
9
13
  storage?: FirebaseStorage;
10
14
  constructor(config: FirebaseConfig);
11
- saveGlobally(): void;
12
- withAuth(): void;
13
- withStorage(): void;
14
15
  static get(): Firebase;
15
16
  static getApp(): FirebaseApp;
16
17
  static getAuth(): Auth;
17
18
  static getStorage(): FirebaseStorage;
19
+ withAuth(): this;
20
+ withStorage(): this;
18
21
  }
@@ -1,18 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- /*
4
- * @sofiakb/fireblaze-ts
5
- *
6
- * (c) Sofiakb <contact.sofiakb@gmail.com>
7
- *
8
- * Created by WebStorm on 21/09/2022 at 10:04
9
- * File src/core/firebase
10
- */
11
3
  const app_1 = require("firebase/app");
12
4
  const firestore_1 = require("firebase/firestore");
13
5
  const auth_1 = require("firebase/auth");
14
6
  const storage_1 = require("firebase/storage");
15
7
  class Firebase {
8
+ static instance = null;
9
+ static appInstance = null;
10
+ static authInstance = null;
11
+ static storageInstance = null;
16
12
  config;
17
13
  app;
18
14
  auth;
@@ -20,41 +16,50 @@ class Firebase {
20
16
  constructor(config) {
21
17
  this.config = config;
22
18
  this.app = (0, app_1.initializeApp)(this.config);
23
- if (config.withEmulator === true)
24
- (0, firestore_1.connectFirestoreEmulator)((0, firestore_1.getFirestore)(), "localhost", config.emulators?.ports?.firestore ?? 9100);
25
- this.saveGlobally();
19
+ if (config.withEmulator === true) {
20
+ (0, firestore_1.connectFirestoreEmulator)((0, firestore_1.getFirestore)(this.app), "localhost", config.emulators?.ports?.firestore ?? 9100);
21
+ }
22
+ // Sauvegarde dans les propriétés statiques
23
+ Firebase.instance = this;
24
+ Firebase.appInstance = this.app;
26
25
  }
27
- saveGlobally() {
28
- globalThis.__sofiakb_firebase = this;
29
- globalThis.__sofiakb_firebase_app = this.app;
30
- globalThis.__sofiakb_firebase_auth = this.auth;
31
- globalThis.__sofiakb_firebase_storage = this.storage;
26
+ static get() {
27
+ if (!Firebase.instance)
28
+ throw new Error("Firebase n'est pas initialisé.");
29
+ return Firebase.instance;
30
+ }
31
+ static getApp() {
32
+ if (!Firebase.appInstance)
33
+ throw new Error("FirebaseApp n'est pas initialisé.");
34
+ return Firebase.appInstance;
35
+ }
36
+ static getAuth() {
37
+ if (!Firebase.authInstance)
38
+ throw new Error("FirebaseAuth n'est pas initialisé.");
39
+ return Firebase.authInstance;
40
+ }
41
+ static getStorage() {
42
+ if (!Firebase.storageInstance)
43
+ throw new Error("FirebaseStorage n'est pas initialisé.");
44
+ return Firebase.storageInstance;
32
45
  }
33
46
  withAuth() {
34
47
  this.auth = (0, auth_1.getAuth)(this.app);
35
- if (this.config.withEmulator === true && !!this.config.emulators?.auth?.url)
48
+ if (this.config.withEmulator === true && !!this.config.emulators?.auth?.url) {
36
49
  (0, auth_1.connectAuthEmulator)(this.auth, this.config.emulators.auth.url);
37
- this.saveGlobally();
50
+ }
51
+ Firebase.authInstance = this.auth;
52
+ return this; // Permet le chaînage optionnel : new Firebase(config).withAuth().withStorage()
38
53
  }
39
54
  withStorage() {
40
55
  this.storage = (0, storage_1.getStorage)(this.app);
41
56
  if (this.config.withEmulator === true &&
42
57
  !!this.config.emulators?.storage?.host &&
43
- !!this.config.emulators?.storage?.port)
58
+ !!this.config.emulators?.storage?.port) {
44
59
  (0, storage_1.connectStorageEmulator)(this.storage, this.config.emulators.storage.host, this.config.emulators.storage.port);
45
- this.saveGlobally();
46
- }
47
- static get() {
48
- return globalThis.__sofiakb_firebase;
49
- }
50
- static getApp() {
51
- return globalThis.__sofiakb_firebase_app;
52
- }
53
- static getAuth() {
54
- return globalThis.__sofiakb_firebase_auth;
55
- }
56
- static getStorage() {
57
- return globalThis.__sofiakb_firebase_storage;
60
+ }
61
+ Firebase.storageInstance = this.storage;
62
+ return this;
58
63
  }
59
64
  }
60
65
  exports.default = Firebase;
@@ -1 +1 @@
1
- {"version":3,"file":"firebase.js","sourceRoot":"","sources":["../../src/core/firebase.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb;;;;;;;GAOG;AAEH,sCAA0D;AAC1D,kDAA4E;AAE5E,wCAAmE;AACnE,8CAAuF;AAIvF,MAAqB,QAAQ;IAC5B,MAAM,CAAiB;IACvB,GAAG,CAAc;IAEjB,IAAI,CAAQ;IACZ,OAAO,CAAmB;IAE1B,YAAY,MAAsB;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,IAAI,CAAC,GAAG,GAAG,IAAA,mBAAa,EAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAEtC,IAAI,MAAM,CAAC,YAAY,KAAK,IAAI;YAC/B,IAAA,oCAAwB,EAAC,IAAA,wBAAY,GAAE,EAAE,WAAW,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,SAAS,IAAI,IAAI,CAAC,CAAC;QAEnG,IAAI,CAAC,YAAY,EAAE,CAAC;IACrB,CAAC;IAED,YAAY;QACV,UAAkB,CAAC,kBAAkB,GAAG,IAAI,CAAC;QAC7C,UAAkB,CAAC,sBAAsB,GAAG,IAAI,CAAC,GAAG,CAAC;QACrD,UAAkB,CAAC,uBAAuB,GAAG,IAAI,CAAC,IAAI,CAAC;QACvD,UAAkB,CAAC,0BAA0B,GAAG,IAAI,CAAC,OAAO,CAAC;IAC/D,CAAC;IAED,QAAQ;QACP,IAAI,CAAC,IAAI,GAAG,IAAA,cAAO,EAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC9B,IAAI,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"}
@@ -0,0 +1,11 @@
1
+ import { FirebaseStorage, StorageReference, UploadMetadata, UploadTask } from "firebase/storage";
2
+ export default class StorageRepository {
3
+ #private;
4
+ constructor(storage?: FirebaseStorage);
5
+ get storage(): FirebaseStorage;
6
+ ref(path: string): StorageReference;
7
+ upload(file: File | Blob | Uint8Array, path: string, metadata?: UploadMetadata): Promise<string>;
8
+ uploadResumable(file: File | Blob | Uint8Array, path: string, metadata?: UploadMetadata): UploadTask;
9
+ getUrl(path: string): Promise<string>;
10
+ delete(path: string): Promise<void>;
11
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const storage_1 = require("firebase/storage");
7
+ const firebase_1 = __importDefault(require("./firebase"));
8
+ const no_firebase_instance_exception_1 = __importDefault(require("../exceptions/no-firebase-instance-exception"));
9
+ class StorageRepository {
10
+ #storage;
11
+ constructor(storage) {
12
+ const _storage = storage ?? firebase_1.default.getStorage();
13
+ if (!_storage)
14
+ throw new no_firebase_instance_exception_1.default();
15
+ this.#storage = _storage;
16
+ }
17
+ get storage() {
18
+ return this.#storage;
19
+ }
20
+ ref(path) {
21
+ return (0, storage_1.ref)(this.#storage, path);
22
+ }
23
+ async upload(file, path, metadata) {
24
+ const storageRef = this.ref(path);
25
+ const snapshot = await (0, storage_1.uploadBytes)(storageRef, file, metadata);
26
+ return (0, storage_1.getDownloadURL)(snapshot.ref);
27
+ }
28
+ uploadResumable(file, path, metadata) {
29
+ return (0, storage_1.uploadBytesResumable)(this.ref(path), file, metadata);
30
+ }
31
+ async getUrl(path) {
32
+ return (0, storage_1.getDownloadURL)(this.ref(path));
33
+ }
34
+ async delete(path) {
35
+ return (0, storage_1.deleteObject)(this.ref(path));
36
+ }
37
+ }
38
+ exports.default = StorageRepository;
39
+ //# sourceMappingURL=storage-repository.js.map
@@ -0,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,EAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IACrC,CAAC;CACD;AApCD,oCAoCC"}
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as Firebase } from "./core/firebase";
2
2
  export { default as FirebaseConfig } from "./core/firebase-config";
3
3
  export { default as FirestoreRepository, FirestoreRepositoryAttributes } from "./core/firestore-repository";
4
+ export { default as StorageRepository } from "./core/storage-repository";
4
5
  export { default as NoFirebaseInstanceException } from "./exceptions/no-firebase-instance-exception";
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.NoFirebaseInstanceException = exports.FirestoreRepository = exports.Firebase = void 0;
6
+ exports.NoFirebaseInstanceException = exports.StorageRepository = exports.FirestoreRepository = exports.Firebase = void 0;
7
7
  /*
8
8
  * @sofiakb/fireblaze-ts
9
9
  *
@@ -16,6 +16,8 @@ var firebase_1 = require("./core/firebase");
16
16
  Object.defineProperty(exports, "Firebase", { enumerable: true, get: function () { return __importDefault(firebase_1).default; } });
17
17
  var firestore_repository_1 = require("./core/firestore-repository");
18
18
  Object.defineProperty(exports, "FirestoreRepository", { enumerable: true, get: function () { return __importDefault(firestore_repository_1).default; } });
19
+ var storage_repository_1 = require("./core/storage-repository");
20
+ Object.defineProperty(exports, "StorageRepository", { enumerable: true, get: function () { return __importDefault(storage_repository_1).default; } });
19
21
  var no_firebase_instance_exception_1 = require("./exceptions/no-firebase-instance-exception");
20
22
  Object.defineProperty(exports, "NoFirebaseInstanceException", { enumerable: true, get: function () { return __importDefault(no_firebase_instance_exception_1).default; } });
21
23
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;AAEb;;;;;;;GAOG;AAEH,4CAAsD;AAA7C,qHAAA,OAAO,OAAY;AAE5B,oEAA4G;AAAnG,4IAAA,OAAO,OAAuB;AACvC,8FAAqG;AAA5F,8JAAA,OAAO,OAA+B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;AAEb;;;;;;;GAOG;AAEH,4CAAsD;AAA7C,qHAAA,OAAO,OAAY;AAE5B,oEAA4G;AAAnG,4IAAA,OAAO,OAAuB;AACvC,gEAAyE;AAAhE,wIAAA,OAAO,OAAqB;AACrC,8FAAqG;AAA5F,8JAAA,OAAO,OAA+B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sofiakb/fireblaze-ts",
3
- "version": "2.0.0-dev.5",
3
+ "version": "2.0.1",
4
4
  "description": "A typescript library for axios API calls.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",