1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.RemoteConfig = void 0;
|
20 | var validator = require("../utils/validator");
|
21 | var remote_config_api_client_internal_1 = require("./remote-config-api-client-internal");
|
22 |
|
23 |
|
24 |
|
25 | var RemoteConfig = (function () {
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 | function RemoteConfig(app) {
|
32 | this.app = app;
|
33 | this.client = new remote_config_api_client_internal_1.RemoteConfigApiClient(app);
|
34 | }
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 | RemoteConfig.prototype.getTemplate = function () {
|
41 | return this.client.getTemplate()
|
42 | .then(function (templateResponse) {
|
43 | return new RemoteConfigTemplateImpl(templateResponse);
|
44 | });
|
45 | };
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | RemoteConfig.prototype.getTemplateAtVersion = function (versionNumber) {
|
54 | return this.client.getTemplateAtVersion(versionNumber)
|
55 | .then(function (templateResponse) {
|
56 | return new RemoteConfigTemplateImpl(templateResponse);
|
57 | });
|
58 | };
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | RemoteConfig.prototype.validateTemplate = function (template) {
|
66 | return this.client.validateTemplate(template)
|
67 | .then(function (templateResponse) {
|
68 | return new RemoteConfigTemplateImpl(templateResponse);
|
69 | });
|
70 | };
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | RemoteConfig.prototype.publishTemplate = function (template, options) {
|
86 | return this.client.publishTemplate(template, options)
|
87 | .then(function (templateResponse) {
|
88 | return new RemoteConfigTemplateImpl(templateResponse);
|
89 | });
|
90 | };
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | RemoteConfig.prototype.rollback = function (versionNumber) {
|
104 | return this.client.rollback(versionNumber)
|
105 | .then(function (templateResponse) {
|
106 | return new RemoteConfigTemplateImpl(templateResponse);
|
107 | });
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | RemoteConfig.prototype.listVersions = function (options) {
|
119 | return this.client.listVersions(options)
|
120 | .then(function (listVersionsResponse) {
|
121 | var _a, _b;
|
122 | return {
|
123 | versions: (_b = (_a = listVersionsResponse.versions) === null || _a === void 0 ? void 0 : _a.map(function (version) { return new VersionImpl(version); })) !== null && _b !== void 0 ? _b : [],
|
124 | nextPageToken: listVersionsResponse.nextPageToken,
|
125 | };
|
126 | });
|
127 | };
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | RemoteConfig.prototype.createTemplateFromJSON = function (json) {
|
136 | if (!validator.isNonEmptyString(json)) {
|
137 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'JSON string must be a valid non-empty string');
|
138 | }
|
139 | var template;
|
140 | try {
|
141 | template = JSON.parse(json);
|
142 | }
|
143 | catch (e) {
|
144 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', "Failed to parse the JSON string: " + json + ". " + e);
|
145 | }
|
146 | return new RemoteConfigTemplateImpl(template);
|
147 | };
|
148 | return RemoteConfig;
|
149 | }());
|
150 | exports.RemoteConfig = RemoteConfig;
|
151 |
|
152 |
|
153 |
|
154 | var RemoteConfigTemplateImpl = (function () {
|
155 | function RemoteConfigTemplateImpl(config) {
|
156 | if (!validator.isNonNullObject(config) ||
|
157 | !validator.isNonEmptyString(config.etag)) {
|
158 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', "Invalid Remote Config template: " + JSON.stringify(config));
|
159 | }
|
160 | this.etagInternal = config.etag;
|
161 | if (typeof config.parameters !== 'undefined') {
|
162 | if (!validator.isNonNullObject(config.parameters)) {
|
163 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Remote Config parameters must be a non-null object');
|
164 | }
|
165 | this.parameters = config.parameters;
|
166 | }
|
167 | else {
|
168 | this.parameters = {};
|
169 | }
|
170 | if (typeof config.parameterGroups !== 'undefined') {
|
171 | if (!validator.isNonNullObject(config.parameterGroups)) {
|
172 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Remote Config parameter groups must be a non-null object');
|
173 | }
|
174 | this.parameterGroups = config.parameterGroups;
|
175 | }
|
176 | else {
|
177 | this.parameterGroups = {};
|
178 | }
|
179 | if (typeof config.conditions !== 'undefined') {
|
180 | if (!validator.isArray(config.conditions)) {
|
181 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Remote Config conditions must be an array');
|
182 | }
|
183 | this.conditions = config.conditions;
|
184 | }
|
185 | else {
|
186 | this.conditions = [];
|
187 | }
|
188 | if (typeof config.version !== 'undefined') {
|
189 | this.version = new VersionImpl(config.version);
|
190 | }
|
191 | }
|
192 | Object.defineProperty(RemoteConfigTemplateImpl.prototype, "etag", {
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 | get: function () {
|
199 | return this.etagInternal;
|
200 | },
|
201 | enumerable: false,
|
202 | configurable: true
|
203 | });
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 | RemoteConfigTemplateImpl.prototype.toJSON = function () {
|
210 | return {
|
211 | conditions: this.conditions,
|
212 | parameters: this.parameters,
|
213 | parameterGroups: this.parameterGroups,
|
214 | etag: this.etag,
|
215 | version: this.version,
|
216 | };
|
217 | };
|
218 | return RemoteConfigTemplateImpl;
|
219 | }());
|
220 |
|
221 |
|
222 |
|
223 | var VersionImpl = (function () {
|
224 | function VersionImpl(version) {
|
225 | if (!validator.isNonNullObject(version)) {
|
226 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', "Invalid Remote Config version instance: " + JSON.stringify(version));
|
227 | }
|
228 | if (typeof version.versionNumber !== 'undefined') {
|
229 | if (!validator.isNonEmptyString(version.versionNumber) &&
|
230 | !validator.isNumber(version.versionNumber)) {
|
231 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version number must be a non-empty string in int64 format or a number');
|
232 | }
|
233 | if (!Number.isInteger(Number(version.versionNumber))) {
|
234 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version number must be an integer or a string in int64 format');
|
235 | }
|
236 | this.versionNumber = version.versionNumber;
|
237 | }
|
238 | if (typeof version.updateOrigin !== 'undefined') {
|
239 | if (!validator.isNonEmptyString(version.updateOrigin)) {
|
240 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version update origin must be a non-empty string');
|
241 | }
|
242 | this.updateOrigin = version.updateOrigin;
|
243 | }
|
244 | if (typeof version.updateType !== 'undefined') {
|
245 | if (!validator.isNonEmptyString(version.updateType)) {
|
246 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version update type must be a non-empty string');
|
247 | }
|
248 | this.updateType = version.updateType;
|
249 | }
|
250 | if (typeof version.updateUser !== 'undefined') {
|
251 | if (!validator.isNonNullObject(version.updateUser)) {
|
252 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version update user must be a non-null object');
|
253 | }
|
254 | this.updateUser = version.updateUser;
|
255 | }
|
256 | if (typeof version.description !== 'undefined') {
|
257 | if (!validator.isNonEmptyString(version.description)) {
|
258 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version description must be a non-empty string');
|
259 | }
|
260 | this.description = version.description;
|
261 | }
|
262 | if (typeof version.rollbackSource !== 'undefined') {
|
263 | if (!validator.isNonEmptyString(version.rollbackSource)) {
|
264 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version rollback source must be a non-empty string');
|
265 | }
|
266 | this.rollbackSource = version.rollbackSource;
|
267 | }
|
268 | if (typeof version.isLegacy !== 'undefined') {
|
269 | if (!validator.isBoolean(version.isLegacy)) {
|
270 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version.isLegacy must be a boolean');
|
271 | }
|
272 | this.isLegacy = version.isLegacy;
|
273 | }
|
274 |
|
275 |
|
276 |
|
277 | if (typeof version.updateTime !== 'undefined') {
|
278 | if (!this.isValidTimestamp(version.updateTime)) {
|
279 | throw new remote_config_api_client_internal_1.FirebaseRemoteConfigError('invalid-argument', 'Version update time must be a valid date string');
|
280 | }
|
281 | this.updateTime = new Date(version.updateTime).toUTCString();
|
282 | }
|
283 | }
|
284 | |
285 |
|
286 |
|
287 | VersionImpl.prototype.toJSON = function () {
|
288 | return {
|
289 | versionNumber: this.versionNumber,
|
290 | updateOrigin: this.updateOrigin,
|
291 | updateType: this.updateType,
|
292 | updateUser: this.updateUser,
|
293 | description: this.description,
|
294 | rollbackSource: this.rollbackSource,
|
295 | isLegacy: this.isLegacy,
|
296 | updateTime: this.updateTime,
|
297 | };
|
298 | };
|
299 | VersionImpl.prototype.isValidTimestamp = function (timestamp) {
|
300 |
|
301 |
|
302 | return validator.isNonEmptyString(timestamp) && (new Date(timestamp)).getTime() > 0;
|
303 | };
|
304 | return VersionImpl;
|
305 | }());
|