Skip to content

Commit

Permalink
upgrade firebase version
Browse files Browse the repository at this point in the history
implement test file
  • Loading branch information
resulturan committed Jan 21, 2023
1 parent 9501830 commit 07ff9cf
Show file tree
Hide file tree
Showing 15 changed files with 683 additions and 1,231 deletions.
16 changes: 6 additions & 10 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"env": {
"browser": true,
"node": true,
},
"extends": [
"kentcdodds/best-practices",
"kentcdodds/possible-errors",
"kentcdodds/mocha"
],
"rules": {},
"env": {
"browser": true,
"node": true
},
"extends": [],
"rules": {}
}
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "refine-firebase",
"version": "1.0.28",
"version": "1.1.1",
"description": "Firebase Tool for Refine",
"main": "lib/index.js",
"author": "Resul TURAN",
Expand All @@ -10,18 +10,17 @@
"Firebase"
],
"devDependencies": {
"@pankod/refine-core": "^3.95.3",
"@types/uuid": "^8.3.1",
"chai": "^4.3.4",
"mocha": "^9.1.3",
"typescript": "^4.4.4"
"typescript": "^4.9.4"
},
"scripts": {
"dev": "tsc --watch --preserveWatchOutput",
"build": "tsc"
},
"dependencies": {
"firebase": "^9.1.3",
"uuid": "^8.3.2"
"firebase": "^9.16.0",
"uuid": "^9.0.0"
},
"files": [
"lib"
Expand Down
61 changes: 18 additions & 43 deletions src/Database.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { IPropsDatabase, IDataContextProvider } from "./interfaces";
import { DataProvider } from "@pankod/refine-core";
import { IDatabaseOptions } from "./interfaces";

export class BaseDatabase {
props: IPropsDatabase;
constructor (props?: IPropsDatabase) {

this.props = props;
export abstract class BaseDatabase {

constructor (private options?: IDatabaseOptions) {
this.getDataProvider = this.getDataProvider.bind(this);
this.createData = this.createData.bind(this);
this.createManyData = this.createManyData.bind(this);
Expand All @@ -16,72 +14,50 @@ export class BaseDatabase {
this.getOne = this.getOne.bind(this);
this.updateData = this.updateData.bind(this);
this.updateManyData = this.updateManyData.bind(this);
this.customMethod = this.customMethod.bind(this);
this.getAPIUrl = this.getAPIUrl.bind(this);
this.requestPayloadFactory = this.requestPayloadFactory.bind(this);
this.responsePayloadFactory = this.responsePayloadFactory.bind(this);
}

requestPayloadFactory(resource: string, data: any): any {
if (this.props?.requestPayloadFactory) {
return (this.props.requestPayloadFactory(resource, data));
if (this.options?.requestPayloadFactory) {
return (this.options.requestPayloadFactory(resource, data));
} else {
return { ...data };
}
}

responsePayloadFactory(resource: string, data: any): any {
if (this.props?.responsePayloadFactory) {
return (this.props?.responsePayloadFactory(resource, data));
if (this.options?.responsePayloadFactory) {
return (this.options?.responsePayloadFactory(resource, data));
} else {
return { ...data };
}
}

async createData(args: any): Promise<any> {

}
abstract createData(args: any): Promise<any>;

async createManyData(args: any): Promise<any> {
abstract createManyData(args: any): Promise<any>;

}
abstract deleteData(args: any): Promise<any>;

async deleteData(args: any): Promise<any> {
abstract deleteManyData(args: any): Promise<any>;

}
abstract getList(args: any): Promise<any>;

async deleteManyData(args: any): Promise<any> {
abstract getMany(args: any): Promise<any>;

}
abstract getOne(args: any): Promise<any>;

async getList(args: any): Promise<any> {

}

async getMany(args: any): Promise<any> {

}
abstract updateData(args: any): Promise<any>;

async getOne(args: any): Promise<any> {

}

async updateData(args: any): Promise<any> {

}
async updateManyData(args: any): Promise<any> {

}

async customMethod(args: any): Promise<any> {

}
abstract updateManyData(args: any): Promise<any>;

getAPIUrl() {
return "";
}

getDataProvider(): IDataContextProvider {
getDataProvider(): DataProvider {
return {
create: this.createData,
createMany: this.createManyData,
Expand All @@ -92,7 +68,6 @@ export class BaseDatabase {
getOne: this.getOne,
update: this.updateData,
updateMany: this.updateManyData,
custom: this.customMethod,
getApiUrl: this.getAPIUrl,
};
}
Expand Down
12 changes: 6 additions & 6 deletions src/FirestoreDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Firestore, getDocs, getFirestore, collection, addDoc, doc, getDoc, updateDoc, deleteDoc, where, query, CollectionReference, DocumentData, Query, orderBy } from "firebase/firestore";
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IPropsDatabase, IUpdateData, IUpdateManyData, CrudOperators } from "./interfaces";
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IDatabaseOptions, IUpdateData, IUpdateManyData, CrudOperators } from "./interfaces";
import { BaseDatabase } from "./Database";


export class FirestoreDatabase extends BaseDatabase {
database: Firestore;

constructor (props?: IPropsDatabase) {
super(props);
this.database = getFirestore(props.firebaseApp);
constructor (options?: IDatabaseOptions, database?: Firestore) {
super(options);
this.database = database || getFirestore(options?.firebaseApp);

this.getCollectionRef = this.getCollectionRef.bind(this);
this.getFilterQuery = this.getFilterQuery.bind(this);
Expand Down Expand Up @@ -63,7 +63,7 @@ export class FirestoreDatabase extends BaseDatabase {

async createManyData<TVariables = {}>(args: ICreateData<TVariables>): Promise<any> {
try {
var data = await this.createData(args)
var data = await this.createData(args);

return { data };
} catch (error) {
Expand All @@ -84,7 +84,7 @@ export class FirestoreDatabase extends BaseDatabase {
async deleteManyData(args: IDeleteManyData): Promise<any> {
try {
args.ids.forEach(async id => {
this.deleteData({ resource: args.resource, id })
this.deleteData({ resource: args.resource, id });
});
} catch (error) {
Promise.reject(error);
Expand Down
29 changes: 15 additions & 14 deletions src/firebaseAuth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { createUserWithEmailAndPassword, sendPasswordResetEmail, signInWithEmailAndPassword, updateEmail, updatePassword, getAuth, signOut, Auth, RecaptchaVerifier, updateProfile, sendEmailVerification, browserLocalPersistence, browserSessionPersistence, RecaptchaParameters, getIdTokenResult, ParsedToken, User as FirebaseUser } from "firebase/auth";
import { FirebaseApp } from "@firebase/app";
import { IRegisterArgs, ILoginArgs, IUser, IAuthCallbacks, IAuthContext } from "./interfaces";
import { AuthProvider } from "@pankod/refine-core";
import { Auth, browserLocalPersistence, browserSessionPersistence, createUserWithEmailAndPassword, getAuth, getIdTokenResult, ParsedToken, RecaptchaParameters, RecaptchaVerifier, sendEmailVerification, sendPasswordResetEmail, signInWithEmailAndPassword, signOut, updateEmail, updatePassword, updateProfile, User as FirebaseUser } from "firebase/auth";
import { IAuthCallbacks, ILoginArgs, IRegisterArgs, IUser } from "./interfaces";

export class FirebaseAuth {

auth: Auth;
authActions: IAuthCallbacks;

constructor (authActions?: IAuthCallbacks, firebaseApp?: FirebaseApp) {
this.auth = getAuth(firebaseApp);
constructor (
private readonly authActions?: IAuthCallbacks,
firebaseApp?: FirebaseApp,
auth?: Auth
) {
this.auth = auth || getAuth(firebaseApp);
this.auth.useDeviceLanguage();

this.getAuthProvider = this.getAuthProvider.bind(this);
Expand All @@ -21,8 +24,6 @@ export class FirebaseAuth {
this.handleCheckAuth = this.handleCheckAuth.bind(this);
this.createRecaptcha = this.createRecaptcha.bind(this);
this.getPermissions = this.getPermissions.bind(this);

this.authActions = authActions;
}

public async handleLogOut() {
Expand Down Expand Up @@ -102,12 +103,12 @@ export class FirebaseAuth {
name: user?.displayName || ""
};
}

private getFirebaseUser(): Promise<FirebaseUser> {
return new Promise<FirebaseUser>((resolve, reject) => {
const unsubscribe = this.auth?.onAuthStateChanged(user => {
unsubscribe();
resolve(user);
unsubscribe();
resolve(user as FirebaseUser | PromiseLike<FirebaseUser>);
}, reject);
});
}
Expand All @@ -122,8 +123,8 @@ export class FirebaseAuth {

public async getPermissions(): Promise<ParsedToken> {
if (this.auth?.currentUser) {
var idTokenResult = await getIdTokenResult(this.auth.currentUser);
return idTokenResult?.claims
const idTokenResult = await getIdTokenResult(this.auth.currentUser);
return idTokenResult?.claims;
} else {
return Promise.reject("User is not found");
}
Expand All @@ -133,7 +134,7 @@ export class FirebaseAuth {
return new RecaptchaVerifier(containerOrId, parameters, this.auth);
}

public getAuthProvider(): IAuthContext {
public getAuthProvider(): AuthProvider {
return {
login: this.handleLogIn,
logout: this.handleLogOut,
Expand Down
12 changes: 6 additions & 6 deletions src/firebaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Database, get, getDatabase, ref, remove, set } from "firebase/database";
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IPropsDatabase, IUpdateData, IUpdateManyData } from "./interfaces";
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IDatabaseOptions, IUpdateData, IUpdateManyData } from "./interfaces";
import { BaseDatabase } from "./Database";

import { v4 as uuidv4 } from 'uuid';
Expand All @@ -8,9 +8,9 @@ import { v4 as uuidv4 } from 'uuid';
export class FirebaseDatabase extends BaseDatabase {
database: Database;

constructor (props?: IPropsDatabase) {
super(props);
this.database = getDatabase(props?.firebaseApp);
constructor (options?: IDatabaseOptions, database?: Database) {
super(options);
this.database = database || getDatabase(options?.firebaseApp);
this.getRef = this.getRef.bind(this);
}

Expand Down Expand Up @@ -137,9 +137,9 @@ export class FirebaseDatabase extends BaseDatabase {
let data: Array<any> = [];
args.ids.forEach(async (id: string) => {
const result = this.updateData({ resource: args.resource, variables: args.variables, id });
data.push(result)
data.push(result);
});
return { data }
return { data };

} catch (error) {
Promise.reject(error);
Expand Down
10 changes: 0 additions & 10 deletions src/index.test.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/interfaces/IDataBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ declare interface ICustomMethod {
metaData?: MetaDataQuery;
}

declare interface IPropsDatabase {
declare interface IDatabaseOptions {
firebaseApp?: FirebaseApp,
requestPayloadFactory?: (resource: string, data: any) => any,
responsePayloadFactory?: (resource: string, data: any) => any,
}
export { IPropsDatabase, ICustomMethod, IGetList, IGetMany, IGetOne, IDeleteManyData, IDeleteData, IUpdateManyData, IUpdateData, ICreateData };
export { IDatabaseOptions, ICustomMethod, IGetList, IGetMany, IGetOne, IDeleteManyData, IDeleteData, IUpdateManyData, IUpdateData, ICreateData };
Loading

0 comments on commit 07ff9cf

Please sign in to comment.