Skip to content

Commit

Permalink
Add functions, storage emulator setup to initializer (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesfries authored May 15, 2023
1 parent afddcef commit fcf2ba9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
49 changes: 47 additions & 2 deletions addon/instance-initializers/firebase-settings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import ApplicationInstance from '@ember/application/instance';

import { FirebaseApp, FirebaseOptions } from 'firebase/app';
import { Firestore } from 'firebase/firestore';
import { Firestore, EmulatorMockTokenOptions } 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,29 @@ interface AuthAddonConfig {
};
}

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

interface StorageAddonConfig {
isCustomSetup?: boolean;
emulator?: {
hostname: string,
port: number,
options?: { mockUserToken?: EmulatorMockTokenOptions | string }
};
}

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

function getDb(app: FirebaseApp, config: FirestoreAddonConfig): Firestore {
Expand All @@ -51,10 +72,26 @@ function setupFirestore(app: FirebaseApp, config: FirestoreAddonConfig): void {
}

function setupAuth(app: FirebaseApp, config: AuthAddonConfig) {
if (config.emulator) {
const { hostname, port, options } = config.emulator;

connectAuthEmulator(getAuth(app), `http://${hostname}:${port}`, options);
}
}

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

connectAuthEmulator(getAuth(app), `http://${hostname}:${port}`, config.emulator.options);
connectFunctionsEmulator(getFunctions(app), hostname, port);
}
}

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

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

Expand All @@ -68,6 +105,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');

3 comments on commit fcf2ba9

@AmilKey
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charlesfries Do you plan to release these changes?

@charlesfries
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikkopaderes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, been busy with life so I can't closely monitor the project for now. Luckily, there's not much monitoring to do 😅

Thanks @charlesfries for the PR and bump. I've just cut out a release

Please sign in to comment.