UNPKG

5.08 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2019 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18export interface RemoteConfig {
19 /**
20 * Defines configuration for the Remote Config SDK.
21 */
22 settings: Settings;
23
24 /**
25 * Object containing default values for configs.
26 */
27 defaultConfig: { [key: string]: string | number | boolean };
28
29 /**
30 * The Unix timestamp in milliseconds of the last <i>successful</i> fetch, or negative one if
31 * the {@link RemoteConfig} instance either hasn't fetched or initialization
32 * is incomplete.
33 */
34 fetchTimeMillis: number;
35
36 /**
37 * The status of the last fetch <i>attempt</i>.
38 */
39 lastFetchStatus: FetchStatus;
40
41 /**
42 * Makes the last fetched config available to the getters.
43 * Returns a promise which resolves to true if the current call activated the fetched configs.
44 * If the fetched configs were already activated, the promise will resolve to false.
45 */
46 activate(): Promise<boolean>;
47
48 /**
49 * Ensures the last activated config are available to the getters.
50 */
51 ensureInitialized(): Promise<void>;
52
53 /**
54 * Fetches and caches configuration from the Remote Config service.
55 */
56 fetch(): Promise<void>;
57
58 /**
59 * Performs fetch and activate operations, as a convenience.
60 * Returns a promise which resolves to true if the current call activated the fetched configs.
61 * If the fetched configs were already activated, the promise will resolve to false.
62 */
63 fetchAndActivate(): Promise<boolean>;
64
65 /**
66 * Gets all config.
67 */
68 getAll(): { [key: string]: Value };
69
70 /**
71 * Gets the value for the given key as a boolean.
72 *
73 * Convenience method for calling <code>remoteConfig.getValue(key).asBoolean()</code>.
74 */
75 getBoolean(key: string): boolean;
76
77 /**
78 * Gets the value for the given key as a number.
79 *
80 * Convenience method for calling <code>remoteConfig.getValue(key).asNumber()</code>.
81 */
82 getNumber(key: string): number;
83
84 /**
85 * Gets the value for the given key as a String.
86 *
87 * Convenience method for calling <code>remoteConfig.getValue(key).asString()</code>.
88 */
89 getString(key: string): string;
90
91 /**
92 * Gets the {@link Value} for the given key.
93 */
94 getValue(key: string): Value;
95
96 /**
97 * Defines the log level to use.
98 */
99 setLogLevel(logLevel: LogLevel): void;
100}
101
102/**
103 * Indicates the source of a value.
104 *
105 * <ul>
106 * <li>"static" indicates the value was defined by a static constant.</li>
107 * <li>"default" indicates the value was defined by default config.</li>
108 * <li>"remote" indicates the value was defined by fetched config.</li>
109 * </ul>
110 */
111export type ValueSource = 'static' | 'default' | 'remote';
112
113/**
114 * Wraps a value with metadata and type-safe getters.
115 */
116export interface Value {
117 /**
118 * Gets the value as a boolean.
119 *
120 * The following values (case-insensitive) are interpreted as true:
121 * "1", "true", "t", "yes", "y", "on". Other values are interpreted as false.
122 */
123 asBoolean(): boolean;
124
125 /**
126 * Gets the value as a number. Comparable to calling <code>Number(value) || 0</code>.
127 */
128 asNumber(): number;
129
130 /**
131 * Gets the value as a string.
132 */
133 asString(): string;
134
135 /**
136 * Gets the {@link ValueSource} for the given key.
137 */
138 getSource(): ValueSource;
139}
140
141/**
142 * Defines configuration options for the Remote Config SDK.
143 */
144export interface Settings {
145 /**
146 * Defines the maximum age in milliseconds of an entry in the config cache before
147 * it is considered stale. Defaults to 43200000 (Twelve hours).
148 */
149 minimumFetchIntervalMillis: number;
150
151 /**
152 * Defines the maximum amount of milliseconds to wait for a response when fetching
153 * configuration from the Remote Config server. Defaults to 60000 (One minute).
154 */
155 fetchTimeoutMillis: number;
156}
157
158/**
159 * Summarizes the outcome of the last attempt to fetch config from the Firebase Remote Config server.
160 *
161 * <ul>
162 * <li>"no-fetch-yet" indicates the {@link RemoteConfig} instance has not yet attempted
163 * to fetch config, or that SDK initialization is incomplete.</li>
164 * <li>"success" indicates the last attempt succeeded.</li>
165 * <li>"failure" indicates the last attempt failed.</li>
166 * <li>"throttle" indicates the last attempt was rate-limited.</li>
167 * </ul>
168 */
169export type FetchStatus = 'no-fetch-yet' | 'success' | 'failure' | 'throttle';
170
171/**
172 * Defines levels of Remote Config logging.
173 */
174export type LogLevel = 'debug' | 'error' | 'silent';
175
176declare module '@firebase/component' {
177 interface NameServiceMapping {
178 'remoteConfig-compat': RemoteConfig;
179 }
180}