forked from firebase/firebase-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase-namespace.ts
More file actions
420 lines (368 loc) · 14 KB
/
firebase-namespace.ts
File metadata and controls
420 lines (368 loc) · 14 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*!
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App as AppCore } from './core';
import { AppStore, defaultAppStore } from './lifecycle';
import {
app, appCheck, auth, messaging, machineLearning, storage, firestore, database,
instanceId, installations, projectManagement, securityRules , remoteConfig, fpnv, AppOptions,
} from '../firebase-namespace-api';
import { cert, refreshToken, applicationDefault } from './credential-factory';
import { getSdkVersion } from '../utils/index';
import App = app.App;
import AppCheck = appCheck.AppCheck;
import Auth = auth.Auth;
import Database = database.Database;
import Firestore = firestore.Firestore;
import Installations = installations.Installations;
import InstanceId = instanceId.InstanceId;
import MachineLearning = machineLearning.MachineLearning;
import Messaging = messaging.Messaging;
import ProjectManagement = projectManagement.ProjectManagement;
import RemoteConfig = remoteConfig.RemoteConfig;
import SecurityRules = securityRules.SecurityRules;
import Storage = storage.Storage;
import Fpnv = fpnv.Fpnv;
export interface FirebaseServiceNamespace <T> {
(app?: App): T;
[key: string]: any;
}
/**
* Internals of a FirebaseNamespace instance.
*/
export class FirebaseNamespaceInternals {
constructor(private readonly appStore: AppStore) {}
/**
* Initializes the App instance.
*
* @param options - Optional options for the App instance. If none present will try to initialize
* from the FIREBASE_CONFIG environment variable. If the environment variable contains a string
* that starts with '{' it will be parsed as JSON, otherwise it will be assumed to be pointing
* to a file.
* @param appName - Optional name of the FirebaseApp instance.
*
* @returns A new App instance.
*/
public initializeApp(options?: AppOptions, appName?: string): App {
const app = this.appStore.initializeApp(options, appName);
return extendApp(app);
}
/**
* Returns the App instance with the provided name (or the default App instance
* if no name is provided).
*
* @param appName - Optional name of the FirebaseApp instance to return.
* @returns The App instance which has the provided name.
*/
public app(appName?: string): App {
const app = this.appStore.getApp(appName);
return extendApp(app);
}
/*
* Returns an array of all the non-deleted App instances.
*/
public get apps(): App[] {
return this.appStore.getApps().map((app) => extendApp(app));
}
}
const firebaseCredential = {
cert, refreshToken, applicationDefault
};
/**
* Global Firebase context object.
*/
export class FirebaseNamespace {
// Hack to prevent Babel from modifying the object returned as the default admin namespace.
/* tslint:disable:variable-name */
public __esModule = true;
/* tslint:enable:variable-name */
public credential = firebaseCredential;
public SDK_VERSION = getSdkVersion();
public INTERNAL: FirebaseNamespaceInternals;
/* tslint:disable */
// TODO(jwenger): Database is the only consumer of firebase.Promise. We should update it to use
// use the native Promise and then remove this.
public Promise: any = Promise;
/* tslint:enable */
constructor(appStore?: AppStore) {
this.INTERNAL = new FirebaseNamespaceInternals(appStore ?? new AppStore());
}
/**
* Gets the `Auth` service namespace. The returned namespace can be used to get the
* `Auth` service for the default app or an explicitly specified app.
*/
get auth(): FirebaseServiceNamespace<Auth> {
const fn: FirebaseServiceNamespace<Auth> = (app?: App) => {
return this.ensureApp(app).auth();
};
const auth = require('../auth/auth').Auth;
return Object.assign(fn, { Auth: auth });
}
/**
* Gets the `Database` service namespace. The returned namespace can be used to get the
* `Database` service for the default app or an explicitly specified app.
*/
get database(): FirebaseServiceNamespace<Database> {
const fn: FirebaseServiceNamespace<Database> = (app?: App) => {
return this.ensureApp(app).database();
};
// eslint-disable-next-line @typescript-eslint/no-var-requires
return Object.assign(fn, require('@firebase/database-compat/standalone'));
}
/**
* Gets the `Messaging` service namespace. The returned namespace can be used to get the
* `Messaging` service for the default app or an explicitly specified app.
*/
get messaging(): FirebaseServiceNamespace<Messaging> {
const fn: FirebaseServiceNamespace<Messaging> = (app?: App) => {
return this.ensureApp(app).messaging();
};
const messaging = require('../messaging/messaging').Messaging;
return Object.assign(fn, { Messaging: messaging });
}
/**
* Gets the `Storage` service namespace. The returned namespace can be used to get the
* `Storage` service for the default app or an explicitly specified app.
*/
get storage(): FirebaseServiceNamespace<Storage> {
const fn: FirebaseServiceNamespace<Storage> = (app?: App) => {
return this.ensureApp(app).storage();
};
const storage = require('../storage/storage').Storage;
return Object.assign(fn, { Storage: storage });
}
/**
* Gets the `Firestore` service namespace. The returned namespace can be used to get the
* `Firestore` service for the default app or an explicitly specified app.
*/
get firestore(): FirebaseServiceNamespace<Firestore> {
let fn: FirebaseServiceNamespace<Firestore> = (app?: App) => {
return this.ensureApp(app).firestore();
};
// eslint-disable-next-line @typescript-eslint/no-var-requires
const firestore = require('@google-cloud/firestore');
fn = Object.assign(fn, firestore.Firestore);
// `v1beta1` and `v1` are lazy-loaded in the Firestore SDK. We use the same trick here
// to avoid triggering this lazy-loading upon initialization.
Object.defineProperty(fn, 'v1beta1', {
get: () => {
return firestore.v1beta1;
},
});
Object.defineProperty(fn, 'v1', {
get: () => {
return firestore.v1;
},
});
return fn;
}
/**
* Gets the `MachineLearning` service namespace. The returned namespace can be
* used to get the `MachineLearning` service for the default app or an
* explicityly specified app.
*/
get machineLearning(): FirebaseServiceNamespace<MachineLearning> {
const fn: FirebaseServiceNamespace<MachineLearning> =
(app?: App) => {
return this.ensureApp(app).machineLearning();
};
const machineLearning =
require('../machine-learning/machine-learning').MachineLearning;
return Object.assign(fn, { MachineLearning: machineLearning });
}
/**
* Gets the `Installations` service namespace. The returned namespace can be used to get the
* `Installations` service for the default app or an explicitly specified app.
*/
get installations(): FirebaseServiceNamespace<Installations> {
const fn: FirebaseServiceNamespace<Installations> = (app?: App) => {
return this.ensureApp(app).installations();
};
const installations = require('../installations/installations').Installations;
return Object.assign(fn, { Installations: installations });
}
/**
* Gets the `InstanceId` service namespace. The returned namespace can be used to get the
* `Instance` service for the default app or an explicitly specified app.
*/
get instanceId(): FirebaseServiceNamespace<InstanceId> {
const fn: FirebaseServiceNamespace<InstanceId> = (app?: App) => {
return this.ensureApp(app).instanceId();
};
const instanceId = require('../instance-id/instance-id').InstanceId;
return Object.assign(fn, { InstanceId: instanceId });
}
/**
* Gets the `ProjectManagement` service namespace. The returned namespace can be used to get the
* `ProjectManagement` service for the default app or an explicitly specified app.
*/
get projectManagement(): FirebaseServiceNamespace<ProjectManagement> {
const fn: FirebaseServiceNamespace<ProjectManagement> = (app?: App) => {
return this.ensureApp(app).projectManagement();
};
const projectManagement = require('../project-management/project-management').ProjectManagement;
return Object.assign(fn, { ProjectManagement: projectManagement });
}
/**
* Gets the `SecurityRules` service namespace. The returned namespace can be used to get the
* `SecurityRules` service for the default app or an explicitly specified app.
*/
get securityRules(): FirebaseServiceNamespace<SecurityRules> {
const fn: FirebaseServiceNamespace<SecurityRules> = (app?: App) => {
return this.ensureApp(app).securityRules();
};
const securityRules = require('../security-rules/security-rules').SecurityRules;
return Object.assign(fn, { SecurityRules: securityRules });
}
/**
* Gets the `RemoteConfig` service namespace. The returned namespace can be used to get the
* `RemoteConfig` service for the default app or an explicitly specified app.
*/
get remoteConfig(): FirebaseServiceNamespace<RemoteConfig> {
const fn: FirebaseServiceNamespace<RemoteConfig> = (app?: App) => {
return this.ensureApp(app).remoteConfig();
};
const remoteConfig = require('../remote-config/remote-config').RemoteConfig;
return Object.assign(fn, { RemoteConfig: remoteConfig });
}
/**
* Gets the `AppCheck` service namespace. The returned namespace can be used to get the
* `AppCheck` service for the default app or an explicitly specified app.
*/
get appCheck(): FirebaseServiceNamespace<AppCheck> {
const fn: FirebaseServiceNamespace<AppCheck> = (app?: App) => {
return this.ensureApp(app).appCheck();
};
const appCheck = require('../app-check/app-check').AppCheck;
return Object.assign(fn, { AppCheck: appCheck });
}
/**
* Gets the `Fpnv` service namespace. The returned namespace can be used to get the
* `Fpnv` service for the default app or an explicitly specified app.
*/
get fpnv(): FirebaseServiceNamespace<Fpnv> {
const fn: FirebaseServiceNamespace<Fpnv> = (app?: App) => {
return this.ensureApp(app).fpnv();
};
const fpnv = require('../fpnv/fpnv').Fpnv;
return Object.assign(fn, { Fpnv: fpnv });
}
// TODO: Change the return types to app.App in the following methods.
/**
* Initializes the FirebaseApp instance.
*
* @param options - Optional options for the FirebaseApp instance.
* If none present will try to initialize from the FIREBASE_CONFIG environment variable.
* If the environment variable contains a string that starts with '{' it will be parsed as JSON,
* otherwise it will be assumed to be pointing to a file.
* @param appName - Optional name of the FirebaseApp instance.
*
* @returns A new FirebaseApp instance.
*/
public initializeApp(options?: AppOptions, appName?: string): App {
return this.INTERNAL.initializeApp(options, appName);
}
/**
* Returns the FirebaseApp instance with the provided name (or the default FirebaseApp instance
* if no name is provided).
*
* @param appName - Optional name of the FirebaseApp instance to return.
* @returns The FirebaseApp instance which has the provided name.
*/
public app(appName?: string): App {
return this.INTERNAL.app(appName);
}
/*
* Returns an array of all the non-deleted FirebaseApp instances.
*/
public get apps(): App[] {
return this.INTERNAL.apps;
}
private ensureApp(app?: App): App {
if (typeof app === 'undefined') {
app = this.app();
}
return app;
}
}
/**
* In order to maintain backward compatibility, we instantiate a default namespace instance in
* this module, and delegate all app lifecycle operations to it. In a future implementation where
* the old admin namespace is no longer supported, we should remove this.
*
* @internal
*/
export const defaultNamespace = new FirebaseNamespace(defaultAppStore);
function extendApp(app: AppCore): App {
const result: App = app as App;
if ((result as any).__extended) {
return result;
}
result.auth = () => {
const fn = require('../auth/index').getAuth;
return fn(app);
};
result.appCheck = () => {
const fn = require('../app-check/index').getAppCheck;
return fn(app);
};
result.database = (url?: string) => {
const fn = require('../database/index').getDatabaseWithUrl;
return fn(url, app);
};
result.messaging = () => {
const fn = require('../messaging/index').getMessaging;
return fn(app);
};
result.storage = () => {
const fn = require('../storage/index').getStorage;
return fn(app);
};
result.firestore = () => {
const fn = require('../firestore/index').getFirestore;
return fn(app);
};
result.instanceId = () => {
const fn = require('../instance-id/index').getInstanceId;
return fn(app);
}
result.installations = () => {
const fn = require('../installations/index').getInstallations;
return fn(app);
};
result.machineLearning = () => {
const fn = require('../machine-learning/index').getMachineLearning;
return fn(app);
}
result.projectManagement = () => {
const fn = require('../project-management/index').getProjectManagement;
return fn(app);
};
result.securityRules = () => {
const fn = require('../security-rules/index').getSecurityRules;
return fn(app);
};
result.remoteConfig = () => {
const fn = require('../remote-config/index').getRemoteConfig;
return fn(app);
};
result.fpnv = () => {
const fn = require('../fpnv/index').getFirebasePnv;
return fn(app);
};
(result as any).__extended = true;
return result;
}