Skip to content

Commit 07ff9cf

Browse files
committed
upgrade firebase version
implement test file
1 parent 9501830 commit 07ff9cf

File tree

15 files changed

+683
-1231
lines changed

15 files changed

+683
-1231
lines changed

.eslintrc

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
{
2-
"env": {
3-
"browser": true,
4-
"node": true,
5-
},
6-
"extends": [
7-
"kentcdodds/best-practices",
8-
"kentcdodds/possible-errors",
9-
"kentcdodds/mocha"
10-
],
11-
"rules": {},
2+
"env": {
3+
"browser": true,
4+
"node": true
5+
},
6+
"extends": [],
7+
"rules": {}
128
}

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "refine-firebase",
3-
"version": "1.0.28",
3+
"version": "1.1.1",
44
"description": "Firebase Tool for Refine",
55
"main": "lib/index.js",
66
"author": "Resul TURAN",
@@ -10,18 +10,17 @@
1010
"Firebase"
1111
],
1212
"devDependencies": {
13+
"@pankod/refine-core": "^3.95.3",
1314
"@types/uuid": "^8.3.1",
14-
"chai": "^4.3.4",
15-
"mocha": "^9.1.3",
16-
"typescript": "^4.4.4"
15+
"typescript": "^4.9.4"
1716
},
1817
"scripts": {
1918
"dev": "tsc --watch --preserveWatchOutput",
2019
"build": "tsc"
2120
},
2221
"dependencies": {
23-
"firebase": "^9.1.3",
24-
"uuid": "^8.3.2"
22+
"firebase": "^9.16.0",
23+
"uuid": "^9.0.0"
2524
},
2625
"files": [
2726
"lib"

src/Database.ts

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { IPropsDatabase, IDataContextProvider } from "./interfaces";
1+
import { DataProvider } from "@pankod/refine-core";
2+
import { IDatabaseOptions } from "./interfaces";
23

3-
export class BaseDatabase {
4-
props: IPropsDatabase;
5-
constructor (props?: IPropsDatabase) {
6-
7-
this.props = props;
4+
export abstract class BaseDatabase {
85

6+
constructor (private options?: IDatabaseOptions) {
97
this.getDataProvider = this.getDataProvider.bind(this);
108
this.createData = this.createData.bind(this);
119
this.createManyData = this.createManyData.bind(this);
@@ -16,72 +14,50 @@ export class BaseDatabase {
1614
this.getOne = this.getOne.bind(this);
1715
this.updateData = this.updateData.bind(this);
1816
this.updateManyData = this.updateManyData.bind(this);
19-
this.customMethod = this.customMethod.bind(this);
2017
this.getAPIUrl = this.getAPIUrl.bind(this);
2118
this.requestPayloadFactory = this.requestPayloadFactory.bind(this);
2219
this.responsePayloadFactory = this.responsePayloadFactory.bind(this);
2320
}
2421

2522
requestPayloadFactory(resource: string, data: any): any {
26-
if (this.props?.requestPayloadFactory) {
27-
return (this.props.requestPayloadFactory(resource, data));
23+
if (this.options?.requestPayloadFactory) {
24+
return (this.options.requestPayloadFactory(resource, data));
2825
} else {
2926
return { ...data };
3027
}
3128
}
3229

3330
responsePayloadFactory(resource: string, data: any): any {
34-
if (this.props?.responsePayloadFactory) {
35-
return (this.props?.responsePayloadFactory(resource, data));
31+
if (this.options?.responsePayloadFactory) {
32+
return (this.options?.responsePayloadFactory(resource, data));
3633
} else {
3734
return { ...data };
3835
}
3936
}
4037

41-
async createData(args: any): Promise<any> {
42-
43-
}
38+
abstract createData(args: any): Promise<any>;
4439

45-
async createManyData(args: any): Promise<any> {
40+
abstract createManyData(args: any): Promise<any>;
4641

47-
}
42+
abstract deleteData(args: any): Promise<any>;
4843

49-
async deleteData(args: any): Promise<any> {
44+
abstract deleteManyData(args: any): Promise<any>;
5045

51-
}
46+
abstract getList(args: any): Promise<any>;
5247

53-
async deleteManyData(args: any): Promise<any> {
48+
abstract getMany(args: any): Promise<any>;
5449

55-
}
50+
abstract getOne(args: any): Promise<any>;
5651

57-
async getList(args: any): Promise<any> {
58-
59-
}
60-
61-
async getMany(args: any): Promise<any> {
62-
63-
}
52+
abstract updateData(args: any): Promise<any>;
6453

65-
async getOne(args: any): Promise<any> {
66-
67-
}
68-
69-
async updateData(args: any): Promise<any> {
70-
71-
}
72-
async updateManyData(args: any): Promise<any> {
73-
74-
}
75-
76-
async customMethod(args: any): Promise<any> {
77-
78-
}
54+
abstract updateManyData(args: any): Promise<any>;
7955

8056
getAPIUrl() {
8157
return "";
8258
}
8359

84-
getDataProvider(): IDataContextProvider {
60+
getDataProvider(): DataProvider {
8561
return {
8662
create: this.createData,
8763
createMany: this.createManyData,
@@ -92,7 +68,6 @@ export class BaseDatabase {
9268
getOne: this.getOne,
9369
update: this.updateData,
9470
updateMany: this.updateManyData,
95-
custom: this.customMethod,
9671
getApiUrl: this.getAPIUrl,
9772
};
9873
}

src/FirestoreDatabase.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Firestore, getDocs, getFirestore, collection, addDoc, doc, getDoc, updateDoc, deleteDoc, where, query, CollectionReference, DocumentData, Query, orderBy } from "firebase/firestore";
2-
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IPropsDatabase, IUpdateData, IUpdateManyData, CrudOperators } from "./interfaces";
2+
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IDatabaseOptions, IUpdateData, IUpdateManyData, CrudOperators } from "./interfaces";
33
import { BaseDatabase } from "./Database";
44

55

66
export class FirestoreDatabase extends BaseDatabase {
77
database: Firestore;
88

9-
constructor (props?: IPropsDatabase) {
10-
super(props);
11-
this.database = getFirestore(props.firebaseApp);
9+
constructor (options?: IDatabaseOptions, database?: Firestore) {
10+
super(options);
11+
this.database = database || getFirestore(options?.firebaseApp);
1212

1313
this.getCollectionRef = this.getCollectionRef.bind(this);
1414
this.getFilterQuery = this.getFilterQuery.bind(this);
@@ -63,7 +63,7 @@ export class FirestoreDatabase extends BaseDatabase {
6363

6464
async createManyData<TVariables = {}>(args: ICreateData<TVariables>): Promise<any> {
6565
try {
66-
var data = await this.createData(args)
66+
var data = await this.createData(args);
6767

6868
return { data };
6969
} catch (error) {
@@ -84,7 +84,7 @@ export class FirestoreDatabase extends BaseDatabase {
8484
async deleteManyData(args: IDeleteManyData): Promise<any> {
8585
try {
8686
args.ids.forEach(async id => {
87-
this.deleteData({ resource: args.resource, id })
87+
this.deleteData({ resource: args.resource, id });
8888
});
8989
} catch (error) {
9090
Promise.reject(error);

src/firebaseAuth.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { createUserWithEmailAndPassword, sendPasswordResetEmail, signInWithEmailAndPassword, updateEmail, updatePassword, getAuth, signOut, Auth, RecaptchaVerifier, updateProfile, sendEmailVerification, browserLocalPersistence, browserSessionPersistence, RecaptchaParameters, getIdTokenResult, ParsedToken, User as FirebaseUser } from "firebase/auth";
21
import { FirebaseApp } from "@firebase/app";
3-
import { IRegisterArgs, ILoginArgs, IUser, IAuthCallbacks, IAuthContext } from "./interfaces";
2+
import { AuthProvider } from "@pankod/refine-core";
3+
import { Auth, browserLocalPersistence, browserSessionPersistence, createUserWithEmailAndPassword, getAuth, getIdTokenResult, ParsedToken, RecaptchaParameters, RecaptchaVerifier, sendEmailVerification, sendPasswordResetEmail, signInWithEmailAndPassword, signOut, updateEmail, updatePassword, updateProfile, User as FirebaseUser } from "firebase/auth";
4+
import { IAuthCallbacks, ILoginArgs, IRegisterArgs, IUser } from "./interfaces";
45

56
export class FirebaseAuth {
6-
77
auth: Auth;
8-
authActions: IAuthCallbacks;
98

10-
constructor (authActions?: IAuthCallbacks, firebaseApp?: FirebaseApp) {
11-
this.auth = getAuth(firebaseApp);
9+
constructor (
10+
private readonly authActions?: IAuthCallbacks,
11+
firebaseApp?: FirebaseApp,
12+
auth?: Auth
13+
) {
14+
this.auth = auth || getAuth(firebaseApp);
1215
this.auth.useDeviceLanguage();
1316

1417
this.getAuthProvider = this.getAuthProvider.bind(this);
@@ -21,8 +24,6 @@ export class FirebaseAuth {
2124
this.handleCheckAuth = this.handleCheckAuth.bind(this);
2225
this.createRecaptcha = this.createRecaptcha.bind(this);
2326
this.getPermissions = this.getPermissions.bind(this);
24-
25-
this.authActions = authActions;
2627
}
2728

2829
public async handleLogOut() {
@@ -102,12 +103,12 @@ export class FirebaseAuth {
102103
name: user?.displayName || ""
103104
};
104105
}
105-
106+
106107
private getFirebaseUser(): Promise<FirebaseUser> {
107108
return new Promise<FirebaseUser>((resolve, reject) => {
108109
const unsubscribe = this.auth?.onAuthStateChanged(user => {
109-
unsubscribe();
110-
resolve(user);
110+
unsubscribe();
111+
resolve(user as FirebaseUser | PromiseLike<FirebaseUser>);
111112
}, reject);
112113
});
113114
}
@@ -122,8 +123,8 @@ export class FirebaseAuth {
122123

123124
public async getPermissions(): Promise<ParsedToken> {
124125
if (this.auth?.currentUser) {
125-
var idTokenResult = await getIdTokenResult(this.auth.currentUser);
126-
return idTokenResult?.claims
126+
const idTokenResult = await getIdTokenResult(this.auth.currentUser);
127+
return idTokenResult?.claims;
127128
} else {
128129
return Promise.reject("User is not found");
129130
}
@@ -133,7 +134,7 @@ export class FirebaseAuth {
133134
return new RecaptchaVerifier(containerOrId, parameters, this.auth);
134135
}
135136

136-
public getAuthProvider(): IAuthContext {
137+
public getAuthProvider(): AuthProvider {
137138
return {
138139
login: this.handleLogIn,
139140
logout: this.handleLogOut,

src/firebaseDatabase.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Database, get, getDatabase, ref, remove, set } from "firebase/database";
2-
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IPropsDatabase, IUpdateData, IUpdateManyData } from "./interfaces";
2+
import { ICreateData, IDeleteData, IDeleteManyData, IGetList, IGetMany, IGetOne, IDatabaseOptions, IUpdateData, IUpdateManyData } from "./interfaces";
33
import { BaseDatabase } from "./Database";
44

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

11-
constructor (props?: IPropsDatabase) {
12-
super(props);
13-
this.database = getDatabase(props?.firebaseApp);
11+
constructor (options?: IDatabaseOptions, database?: Database) {
12+
super(options);
13+
this.database = database || getDatabase(options?.firebaseApp);
1414
this.getRef = this.getRef.bind(this);
1515
}
1616

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

144144
} catch (error) {
145145
Promise.reject(error);

src/index.test.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/interfaces/IDataBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ declare interface ICustomMethod {
5454
metaData?: MetaDataQuery;
5555
}
5656

57-
declare interface IPropsDatabase {
57+
declare interface IDatabaseOptions {
5858
firebaseApp?: FirebaseApp,
5959
requestPayloadFactory?: (resource: string, data: any) => any,
6060
responsePayloadFactory?: (resource: string, data: any) => any,
6161
}
62-
export { IPropsDatabase, ICustomMethod, IGetList, IGetMany, IGetOne, IDeleteManyData, IDeleteData, IUpdateManyData, IUpdateData, ICreateData };
62+
export { IDatabaseOptions, ICustomMethod, IGetList, IGetMany, IGetOne, IDeleteManyData, IDeleteData, IUpdateManyData, IUpdateData, ICreateData };

0 commit comments

Comments
 (0)