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 functions, storage emulator setup to initializer #274

Merged
merged 5 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
44 changes: 44 additions & 0 deletions addon/instance-initializers/firebase-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Firestore } from 'firebase/firestore';
import { initializeApp } from 'ember-cloud-firestore-adapter/firebase/app';
import { connectFirestoreEmulator, getFirestore, initializeFirestore } from 'ember-cloud-firestore-adapter/firebase/firestore';
import { connectAuthEmulator, getAuth } from 'ember-cloud-firestore-adapter/firebase/auth';
import { connectFunctionsEmulator, getFunctions } from 'ember-cloud-firestore-adapter/firebase/functions';
import { connectStorageEmulator, getStorage } from 'ember-cloud-firestore-adapter/firebase/storage';

interface FirestoreAddonConfig {
isCustomSetup?: boolean;
Expand All @@ -26,10 +28,28 @@ interface AuthAddonConfig {
};
}

interface FunctionsAddonConfig {
isCustomSetup?: boolean;
emulator?: {
hostname: string,
port: number,
};
}

interface StorageAddonConfig {
isCustomSetup?: boolean;
emulator?: {
hostname: string,
port: number,
charlesfries marked this conversation as resolved.
Show resolved Hide resolved
};
}

interface AddonConfig {
firebaseConfig: FirebaseOptions,
firestore?: FirestoreAddonConfig;
auth?: AuthAddonConfig;
functions?: FunctionsAddonConfig;
storage?: StorageAddonConfig;
}

function getDb(app: FirebaseApp, config: FirestoreAddonConfig): Firestore {
Expand Down Expand Up @@ -58,6 +78,22 @@ function setupAuth(app: FirebaseApp, config: AuthAddonConfig) {
}
}

function setupFunctions(app: FirebaseApp, config: FunctionsAddonConfig) {
if (config.emulator) {
const { hostname, port } = config.emulator;

connectFunctionsEmulator(getFunctions(app), hostname, port);
}
}

function setupStorage(app: FirebaseApp, config: StorageAddonConfig) {
if (config.emulator) {
const { hostname, port } = config.emulator;

connectStorageEmulator(getStorage(app), hostname, port);
}
}

function setupModularInstance(config: AddonConfig) {
const app = initializeApp(config.firebaseConfig);

Expand All @@ -68,6 +104,14 @@ function setupModularInstance(config: AddonConfig) {
if (config.auth && !config.auth?.isCustomSetup) {
setupAuth(app, config.auth);
}

if (config.functions && !config.functions?.isCustomSetup) {
setupFunctions(app, config.functions);
}

if (config.storage && !config.storage?.isCustomSetup) {
setupStorage(app, config.storage);
}
}

export function initialize(appInstance: ApplicationInstance): void {
Expand Down
2 changes: 2 additions & 0 deletions scripts/build-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ buildFastBootWrappers('firebase/auth', keys<typeof import('firebase/auth')>(), '
'prodErrorMap',
]);
buildFastBootWrappers('firebase/firestore', keys<typeof import('firebase/firestore')>(), 'firestore');
buildFastBootWrappers('firebase/functions', keys<typeof import('firebase/functions')>(), 'functions');
buildFastBootWrappers('firebase/storage', keys<typeof import('firebase/storage')>(), 'storage');