Skip to content

Commit

Permalink
feat: Added @deprecated methods back in
Browse files Browse the repository at this point in the history
  • Loading branch information
benwinding committed Mar 14, 2023
1 parent 1a03111 commit 3a82d4d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/misc/translate-from-firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const recursivelyMapStorageUrls = async (
if (isFileField) {
try {
const src = await getDownloadURL(
ref(fireWrapper.fireStorage(), fieldValue.src)
ref(fireWrapper.storage(), fieldValue.src)
);
return {
...fieldValue,
Expand Down
51 changes: 27 additions & 24 deletions src/providers/database/firebase/FirebaseWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,35 @@ import { RAFirebaseOptions } from '../../options';
import { IFirebaseWrapper } from './IFirebaseWrapper';

export class FirebaseWrapper implements IFirebaseWrapper {
private readonly app: FireApp;
private readonly firestore: FireStore;
private readonly storage: FireStorage;
private readonly auth: FireAuth;
private readonly _app: FireApp;
private readonly _firestore: FireStore;
private readonly _storage: FireStorage;
private readonly _auth: FireAuth;
public options: RAFirebaseOptions;

constructor(inputOptions: RAFirebaseOptions | undefined, firebaseConfig: {}) {
const optionsSafe = inputOptions || {};
this.options = optionsSafe;
this.app = (window as any)['_app'] = ObtainFirebaseApp(
this._app = (window as any)['_app'] = ObtainFirebaseApp(
firebaseConfig,
optionsSafe
);
this.firestore = getFirestore(this.app);
this.storage = getStorage(this.app);
this.auth = getAuth(this.app);
this._firestore = getFirestore(this._app);
this._storage = getStorage(this._app);
this._auth = getAuth(this._app);
}
dbGetCollection(absolutePath: string): FireStoreCollectionRef {
return collection(this.firestore, absolutePath);
return collection(this._firestore, absolutePath);
}
dbCreateBatch(): FireStoreBatch {
return writeBatch(this.firestore);
return writeBatch(this._firestore);
}
dbMakeNewId(): string {
return doc(collection(this.firestore, 'collections')).id;
return doc(collection(this._firestore, 'collections')).id;
}

public OnUserLogout(callBack: (u: FireUser | null) => any) {
this.auth.onAuthStateChanged((user) => {
this._auth.onAuthStateChanged((user) => {
const isLoggedOut = !user;
log('FirebaseWrapper.OnUserLogout', { user, isLoggedOut });
if (isLoggedOut) {
Expand All @@ -76,7 +76,7 @@ export class FirebaseWrapper implements IFirebaseWrapper {
});
}
putFile(storagePath: string, rawFile: any): FireStoragePutFileResult {
const task = uploadBytesResumable(ref(this.storage, storagePath), rawFile);
const task = uploadBytesResumable(ref(this._storage, storagePath), rawFile);
const taskResult = new Promise<FireUploadTaskSnapshot>((res, rej) =>
task.then(res).catch(rej)
);
Expand All @@ -92,7 +92,7 @@ export class FirebaseWrapper implements IFirebaseWrapper {
};
}
async getStorageDownloadUrl(fieldSrc: string): Promise<string> {
return getDownloadURL(ref(this.storage, fieldSrc));
return getDownloadURL(ref(this._storage, fieldSrc));
}
public serverTimestamp() {
// This line doesn't work for some reason, might be firebase sdk.
Expand All @@ -116,25 +116,25 @@ export class FirebaseWrapper implements IFirebaseWrapper {

log('setPersistence', { persistenceInput, persistenceResolved });

return this.auth
return this._auth
.setPersistence(persistenceResolved)
.catch((error) => console.error(error));
}
async authSigninEmailPassword(
email: string,
password: string
): Promise<FireAuthUserCredentials> {
const user = await signInWithEmailAndPassword(this.auth, email, password);
const user = await signInWithEmailAndPassword(this._auth, email, password);
return user;
}
async authSignOut(): Promise<void> {
return signOut(this.auth);
return signOut(this._auth);
}
async authGetUserLoggedIn(): Promise<FireUser> {
return new Promise((resolve, reject) => {
const auth = this.auth;
const auth = this._auth;
if (auth.currentUser) return resolve(auth.currentUser);
const unsubscribe = onAuthStateChanged(this.auth, (user) => {
const unsubscribe = onAuthStateChanged(this._auth, (user) => {
unsubscribe();
if (user) {
resolve(user);
Expand All @@ -149,17 +149,20 @@ export class FirebaseWrapper implements IFirebaseWrapper {
}

/** @deprecated */
public fireStorage(): FireStorage {
return this.storage;
public auth(): FireAuth {
return this._auth;
}
/** @deprecated */
public storage(): FireStorage {
return this._storage;
}

/** @deprecated */
public GetApp(): FireApp {
return this.app;
return this._app;
}
/** @deprecated */
public db(): FireStore {
return this.firestore;
return this._firestore;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/providers/database/firebase/IFirebaseWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import firebase from 'firebase/compat';
import {
FireApp,
FireAuth,
FireAuthUserCredentials,
FireStorage,
FireStoragePutFileResult,
Expand Down Expand Up @@ -35,8 +36,9 @@ export interface IFirebaseWrapper {

// Deprecated methods
/** @deprecated */
fireStorage(): FireStorage | firebase.storage.Storage;

auth(): FireAuth;
/** @deprecated */
storage(): FireStorage;
/** @deprecated */
db(): FireStore | firebase.firestore.Firestore;
/** @deprecated */
Expand Down
26 changes: 16 additions & 10 deletions tests/integration-tests/utils/FirebaseWrapperStub.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { getAuth } from 'firebase/auth';
import { collection, doc, writeBatch } from 'firebase/firestore';
import { getDownloadURL, ref, uploadBytesResumable } from 'firebase/storage';
import { RAFirebaseOptions } from '../../../src';
import {
FireApp,
FireAuth,
FireAuthUserCredentials,
FireStorage,
FireStoragePutFileResult,
Expand All @@ -16,8 +18,8 @@ import { IFirebaseWrapper } from '../../../src/providers/database';

export class FirebaseWrapperStub implements IFirebaseWrapper {
constructor(
private firestore: FireStore | any,
private storage: FireStorage,
private _firestore: FireStore | any,
private _storage: FireStorage,
public options: RAFirebaseOptions
) {}

Expand All @@ -26,13 +28,13 @@ export class FirebaseWrapperStub implements IFirebaseWrapper {
}

dbGetCollection(absolutePath: string): FireStoreCollectionRef {
return collection(this.firestore, absolutePath);
return collection(this._firestore, absolutePath);
}
dbCreateBatch(): FireStoreBatch {
return writeBatch(this.firestore);
return writeBatch(this._firestore);
}
dbMakeNewId(): string {
return doc(collection(this.firestore, 'collections')).id;
return doc(collection(this._firestore, 'collections')).id;
}

// tslint:disable-next-line:no-empty
Expand All @@ -41,7 +43,7 @@ export class FirebaseWrapperStub implements IFirebaseWrapper {
storagePath: string,
rawFile: any
): Promise<FireStoragePutFileResult> => {
const task = uploadBytesResumable(ref(this.storage, storagePath), rawFile);
const task = uploadBytesResumable(ref(this._storage, storagePath), rawFile);
const taskResult = new Promise<FireUploadTaskSnapshot>((res, rej) =>
task.then(res).catch(rej)
);
Expand All @@ -55,7 +57,7 @@ export class FirebaseWrapperStub implements IFirebaseWrapper {
};
};
async getStorageDownloadUrl(fieldSrc: string): Promise<string> {
return getDownloadURL(ref(this.storage, fieldSrc));
return getDownloadURL(ref(this._storage, fieldSrc));
}
authSetPersistence(
persistenceInput: 'session' | 'local' | 'none'
Expand All @@ -81,12 +83,16 @@ export class FirebaseWrapperStub implements IFirebaseWrapper {
// Deprecated methods

/** @deprecated */
fireStorage(): FireStorage {
return this.storage;
auth(): FireAuth {
return getAuth(this.GetApp());
}
/** @deprecated */
storage(): FireStorage {
return this._storage;
}
/** @deprecated */
db(): FireStore {
return this.firestore;
return this._firestore;
}
/** @deprecated */
GetUserLogin(): Promise<FireUser> {
Expand Down

0 comments on commit 3a82d4d

Please sign in to comment.