|
18 | 18 | import { base64Decode } from './crypt';
|
19 | 19 | import { getGlobal } from './environment';
|
20 | 20 |
|
21 |
| -type ExperimentalKey = 'authTokenSyncURL' | 'authIdTokenMaxAge'; |
| 21 | +/** |
| 22 | + * Keys for experimental properties on the `FirebaseDefaults` object. |
| 23 | + * @public |
| 24 | + */ |
| 25 | +export type ExperimentalKey = 'authTokenSyncURL' | 'authIdTokenMaxAge'; |
22 | 26 |
|
23 |
| -interface FirebaseDefaults { |
| 27 | +/** |
| 28 | + * An object that can be injected into the environment as __FIREBASE_DEFAULTS__, |
| 29 | + * either as a property of globalThis, a shell environment variable, or a |
| 30 | + * cookie. |
| 31 | + * |
| 32 | + * This object can be used to automatically configure and initialize |
| 33 | + * a Firebase app as well as any emulators. |
| 34 | + * |
| 35 | + * @public |
| 36 | + */ |
| 37 | +export interface FirebaseDefaults { |
24 | 38 | config?: Record<string, string>;
|
25 | 39 | emulatorHosts?: Record<string, string>;
|
26 | 40 | _authTokenSyncURL?: string;
|
@@ -81,17 +95,37 @@ const getDefaultsFromCookie = (): FirebaseDefaults | undefined => {
|
81 | 95 | return decoded && JSON.parse(decoded);
|
82 | 96 | };
|
83 | 97 |
|
| 98 | +/** |
| 99 | + * Get the __FIREBASE_DEFAULTS__ object. It checks in order: |
| 100 | + * (1) if such an object exists as a property of `globalThis` |
| 101 | + * (2) if such an object was provided on a shell environment variable |
| 102 | + * (3) if such an object exists in a cookie |
| 103 | + */ |
84 | 104 | const getDefaults = (): FirebaseDefaults | undefined =>
|
85 | 105 | getDefaultsFromGlobal() ||
|
86 | 106 | getDefaultsFromEnvVariable() ||
|
87 | 107 | getDefaultsFromCookie();
|
88 | 108 |
|
89 |
| -export const getDefaultEmulatorHost = (name: string): string | undefined => |
90 |
| - getDefaults()?.emulatorHosts?.[name]; |
| 109 | +/** |
| 110 | + * Returns emulator host stored in the __FIREBASE_DEFAULTS__ object |
| 111 | + * for the given product. |
| 112 | + * @public |
| 113 | + */ |
| 114 | +export const getDefaultEmulatorHost = (productName: string): string | undefined => |
| 115 | + getDefaults()?.emulatorHosts?.[productName]; |
91 | 116 |
|
| 117 | +/** |
| 118 | + * Returns Firebase app config stored in the __FIREBASE_DEFAULTS__ object. |
| 119 | + * @public |
| 120 | + */ |
92 | 121 | export const getDefaultAppConfig = (): Record<string, string> | undefined =>
|
93 | 122 | getDefaults()?.config;
|
94 | 123 |
|
| 124 | +/** |
| 125 | + * Returns an experimental setting on the __FIREBASE_DEFAULTS__ object (properties |
| 126 | + * prefixed by "_") |
| 127 | + * @public |
| 128 | + */ |
95 | 129 | export const getExperimentalSetting = <T extends ExperimentalKey>(
|
96 | 130 | name: T
|
97 | 131 | ): FirebaseDefaults[`_${T}`] =>
|
|
0 commit comments