Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage to appkit and add getStorageDownloadURL method #91

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/firestore/app/appkit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { onAuthStateChanged } from 'firebase/auth';
import { updateDoc, arrayRemove, arrayUnion } from 'firebase/firestore';

import { ref, getDownloadURL } from 'firebase/storage';
import { IComputedScores, IRawScores, RoarRun } from './run';
import { ITaskVariantInfo, RoarTaskVariant } from './task';
import { IUserInfo, IUserUpdateInput, RoarAppUser } from './user';
Expand Down Expand Up @@ -303,4 +303,13 @@ export class RoarAppkit {
throw new Error('This run has not started. Use the startRun method first.');
}
}

async getStorageDownloadUrl(filePath: string) {
if (!this._initialized) {
await this._init();
}

const storageRef = ref(this.firebaseProject!.storage, filePath);
return getDownloadURL(storageRef);
}
}
2 changes: 2 additions & 0 deletions src/firestore/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FirebaseApp } from 'firebase/app';
import { Auth, User } from 'firebase/auth';
import { DocumentData, Firestore, Timestamp } from 'firebase/firestore';
import { Functions } from 'firebase/functions';
import { FirebaseStorage } from 'firebase/storage';
import { FirebaseConfigData } from './util';

export interface IRoarConfigData {
Expand All @@ -14,6 +15,7 @@ export interface IFirekit {
db: Firestore;
auth: Auth;
functions: Functions;
storage: FirebaseStorage;
user?: User;
claimsLastUpdated?: Date;
}
Expand Down
6 changes: 5 additions & 1 deletion src/firestore/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'firebase/auth';
import { connectFirestoreEmulator, Firestore, getFirestore } from 'firebase/firestore';
import { Functions, connectFunctionsEmulator, getFunctions } from 'firebase/functions';
import { getStorage, FirebaseStorage } from 'firebase/storage';
import _chunk from 'lodash/chunk';
import _difference from 'lodash/difference';
import _flatten from 'lodash/flatten';
Expand Down Expand Up @@ -117,7 +118,7 @@ export interface MarkRawConfig {
functions?: boolean;
}

type FirebaseProduct = Auth | Firestore | Functions;
type FirebaseProduct = Auth | Firestore | Functions | FirebaseStorage;

export const initializeFirebaseProject = async (
config: FirebaseConfigData,
Expand All @@ -139,6 +140,7 @@ export const initializeFirebaseProject = async (
const auth = optionallyMarkRaw('auth', getAuth(app));
const db = optionallyMarkRaw('db', getFirestore(app));
const functions = optionallyMarkRaw('functions', getFunctions(app));
const storage = optionallyMarkRaw('storage', getStorage(app));

connectFirestoreEmulator(db, '127.0.0.1', ports.db);
connectFunctionsEmulator(functions, '127.0.0.1', ports.functions);
Expand All @@ -154,6 +156,7 @@ export const initializeFirebaseProject = async (
auth,
db,
functions,
storage,
};
} else {
const app = safeInitializeApp(config as RealConfigData, name);
Expand All @@ -162,6 +165,7 @@ export const initializeFirebaseProject = async (
auth: optionallyMarkRaw('auth', getAuth(app)),
db: optionallyMarkRaw('db', getFirestore(app)),
functions: optionallyMarkRaw('functions', getFunctions(app)),
storage: optionallyMarkRaw('storage', getStorage(app)),
};

// Auth state persistence is set with ``setPersistence`` and specifies how a
Expand Down
Loading