-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfirebase-settings.ts
154 lines (127 loc) · 3.81 KB
/
firebase-settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import type ApplicationInstance from '@ember/application/instance';
import type { FirebaseApp, FirebaseError, FirebaseOptions } from 'firebase/app';
import type { 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;
settings?: { [key: string]: string };
emulator?: {
hostname: string;
port: number;
options?: { [key: string]: unknown };
};
}
interface AuthAddonConfig {
isCustomSetup?: boolean;
emulator?: {
hostname: string;
port: number;
options?: { disableWarnings: boolean };
};
}
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;
}
interface Config {
'ember-cloud-firestore-adapter': AddonConfig;
}
function getDb(app: FirebaseApp, config: FirestoreAddonConfig): Firestore {
if (config.settings) {
return initializeFirestore(app, config.settings);
}
return getFirestore(app);
}
function setupFirestore(app: FirebaseApp, config: FirestoreAddonConfig): void {
const db = getDb(app, config);
if (config.emulator) {
const { hostname, port, options } = config.emulator;
connectFirestoreEmulator(db, hostname, port, options);
}
}
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;
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);
}
}
function setupModularInstance(config: AddonConfig) {
const app = initializeApp(config.firebaseConfig);
if (config.firestore && !config.firestore.isCustomSetup) {
setupFirestore(app, config.firestore);
}
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 {
const config = appInstance.resolveRegistration(
'config:environment',
) as Config;
const addonConfig = config['ember-cloud-firestore-adapter'];
try {
setupModularInstance(addonConfig);
} catch (e) {
if ((e as FirebaseError).code !== 'failed-precondition') {
throw new Error(
`There was a problem with initializing Firebase. Check if you've configured the addon properly. | Error: ${e}`,
);
}
}
}
export default {
initialize,
};