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 all 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
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,
charlesfries marked this conversation as resolved.
Show resolved Hide resolved
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');