Skip to content

Commit

Permalink
masking similar to original firebase admin left
Browse files Browse the repository at this point in the history
  • Loading branch information
Moe03 committed Apr 17, 2024
1 parent b6d9130 commit d521985
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 144 deletions.
6 changes: 6 additions & 0 deletions src/admin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { initFirebaseAdmin } from "./initFAS"


export {
initFirebaseAdmin
}
7 changes: 7 additions & 0 deletions src/admin/initFAS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FirebaseAdminRest } from "../firestore/FARClass";
import { InitFirebaseAdminInput } from "../types";

export async function initFirebaseAdmin(options?: InitFirebaseAdminInput){
const db = await new FirebaseAdminRest(options).initApp();
return db;
}
14 changes: 5 additions & 9 deletions src/firebase-auth-utils/initFirebase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


import * as jwt from '@tsndr/cloudflare-worker-jwt'
import { FirebaseAdminConfig, FirestoreDatabase } from '../types';
import { InitFirebaseAdminInput, InitFirebaseAdminOuput } from '../types';
import { testFbCredentials } from './testFbCredentials';

/**
Expand Down Expand Up @@ -56,11 +56,7 @@ export async function generateJWT(input?: {
* @returns A promise that resolves to the Firestore database information.
* @throws Error if the project_id is not provided in the serviceAccount.
*/
export async function initFirebaseRest(options?: {
serviceAccount: FirebaseAdminConfig
databaseId?: string;
ignoreUndefinedValues?: boolean;
}): Promise<FirestoreDatabase> {
export async function initFirebaseRest(options?: InitFirebaseAdminInput): Promise<InitFirebaseAdminOuput> {
if (options?.serviceAccount === undefined) {
options = {
serviceAccount: {
Expand All @@ -87,8 +83,8 @@ export async function initFirebaseRest(options?: {
}

return {
name: options.databaseId || '(default)',
projectId: options.serviceAccount?.project_id,
accessToken: accessToken
databaseId: options.databaseId || '(default)',
serviceAccount: options.serviceAccount,
accessToken: accessToken,
};
}
127 changes: 0 additions & 127 deletions src/firestore/Class.ts

This file was deleted.

117 changes: 117 additions & 0 deletions src/firestore/FARClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { initFirebaseRest } from "../firebase-auth-utils/initFirebase";
import { DirectionOpREST, GetDocumentResponse, InitFirebaseAdminInput, WhereFilterOpREST } from "../types";
import { getDocRest } from "./getDoc";



/**
* Represents a Firebase Admin REST client.
*/
/**
* Initializes a new instance of the FirebaseAdminRest class.
* @param initialValue - Optional initial value for the FirebaseAdminConfig.
*/
export class FirebaseAdminRest<T = any> {

private initialValue: InitFirebaseAdminInput | undefined;

constructor(initialValue?: InitFirebaseAdminInput) {
this.initialValue = initialValue;
return this;
}

async initApp() {
const appInstance = await initFirebaseRest(this.initialValue);
return new FirestoreOperations(appInstance.databaseId);
}
}

/**
* Nested class for operations related to documents.
*/
class FirestoreOperations {

constructor(private databaseId: string) {
return this;
}

public doc<T = any>(docPath: string): DocOperations<T> {
return new DocOperations<T>(docPath, this.databaseId);
}

public collection<T extends object>(collectionPath: string): CollectionOperations<T> {
return new CollectionOperations<T>(collectionPath, this.databaseId);
}
}

/**
* Nested class for operations related to documents.
*/
class DocOperations<T = any> {
constructor(private docPath: string, private databaseId: string) { }

// Define methods for document operations here
public async get(): Promise<GetDocumentResponse<T>> {
const doc = await getDocRest(this.docPath, {
debug: true
});
return doc;
}
}

/**
* Nested class for operations related to documents.
*/
class CollectionOperations<T> {

private whereQuery: {
field: string,
op: WhereFilterOpREST,
value: any
};
private orderByQuery: {
field: string,
direction: DirectionOpREST
};
private limitQuery: number;

constructor(private collectionPath: string, private databaseId: string) {
this.whereQuery = {
field: "",
op: "EQUAL",
value: ""
}
this.orderByQuery = {
field: "",
direction: "ASCENDING"
}
this.limitQuery = 100;
}


public where(field: string, op: WhereFilterOpREST, value: any) {
this.whereQuery = {
field: field,
op: op,
value: value
}
return this;
}

public orderBy(field: string, direction: DirectionOpREST) {
this.orderByQuery = {
field: field,
direction: direction
}
return this;
}

public limit(limit: number) {
this.limitQuery = limit;
return this;
}
// Define methods for document operations here
public async get(): Promise<string> {
return `Retrieving from collection: ${this.collectionPath} with where(): ${this.whereQuery} with orderBy ${this.orderByQuery} with limit ${this.limitQuery} at ${this.collectionPath}`;
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
// [key: string]: string;
// }

export * from './admin'
export * from './firebase-auth-utils'
export * from './firestore';
export * from './types'

17 changes: 13 additions & 4 deletions src/tests/class.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { FirebaseAdminRest } from "../firestore/Class";
// import { initFirebaseRest } from "../firestore";
import { initFirebaseAdmin } from "../admin";

export interface User {
id: string;
name: string;
email: string;
password: string;
}

async function test() {
const db = new FirebaseAdminRest();
const res = await db.collection(`user`).where('name', `EQUAL`, 5).limit(5).get();
console.log(res);
// await initFirebaseRest();
const db = await initFirebaseAdmin();
const doc = await db.doc<User>(`users/test_1`).get();
console.log(doc.data())
}
test()
14 changes: 10 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ export interface FirebaseAdminConfig {
universe_domain?: string;
}

export interface FirestoreDatabase {
name: string,
projectId?: string,
accessToken: string,
export interface InitFirebaseAdminInput {
serviceAccount: FirebaseAdminConfig
databaseId?: string;
ignoreUndefinedValues?: boolean;
}

export interface InitFirebaseAdminOuput {
serviceAccount: FirebaseAdminConfig,
databaseId: string,
accessToken: string
}


Expand Down

0 comments on commit d521985

Please sign in to comment.