1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var __extends = (this && this.__extends) || (function () {
|
19 | var extendStatics = function (d, b) {
|
20 | extendStatics = Object.setPrototypeOf ||
|
21 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
22 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
23 | return extendStatics(d, b);
|
24 | };
|
25 | return function (d, b) {
|
26 | extendStatics(d, b);
|
27 | function __() { this.constructor = d; }
|
28 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
29 | };
|
30 | })();
|
31 | var __assign = (this && this.__assign) || function () {
|
32 | __assign = Object.assign || function(t) {
|
33 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
34 | s = arguments[i];
|
35 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
36 | t[p] = s[p];
|
37 | }
|
38 | return t;
|
39 | };
|
40 | return __assign.apply(this, arguments);
|
41 | };
|
42 | Object.defineProperty(exports, "__esModule", { value: true });
|
43 | exports.FirebaseRemoteConfigError = exports.RemoteConfigApiClient = void 0;
|
44 | var api_request_1 = require("../utils/api-request");
|
45 | var error_1 = require("../utils/error");
|
46 | var utils = require("../utils/index");
|
47 | var validator = require("../utils/validator");
|
48 | var deep_copy_1 = require("../utils/deep-copy");
|
49 |
|
50 | var FIREBASE_REMOTE_CONFIG_V1_API = 'https://firebaseremoteconfig.googleapis.com/v1';
|
51 | var FIREBASE_REMOTE_CONFIG_HEADERS = {
|
52 | 'X-Firebase-Client': "fire-admin-node/" + utils.getSdkVersion(),
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | 'Accept-Encoding': 'gzip',
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | var RemoteConfigApiClient = (function () {
|
65 | function RemoteConfigApiClient(app) {
|
66 | this.app = app;
|
67 | if (!validator.isNonNullObject(app) || !('options' in app)) {
|
68 | throw new FirebaseRemoteConfigError('invalid-argument', 'First argument passed to admin.remoteConfig() must be a valid Firebase app instance.');
|
69 | }
|
70 | this.httpClient = new api_request_1.AuthorizedHttpClient(app);
|
71 | }
|
72 | RemoteConfigApiClient.prototype.getTemplate = function () {
|
73 | var _this = this;
|
74 | return this.getUrl()
|
75 | .then(function (url) {
|
76 | var request = {
|
77 | method: 'GET',
|
78 | url: url + "/remoteConfig",
|
79 | headers: FIREBASE_REMOTE_CONFIG_HEADERS
|
80 | };
|
81 | return _this.httpClient.send(request);
|
82 | })
|
83 | .then(function (resp) {
|
84 | return _this.toRemoteConfigTemplate(resp);
|
85 | })
|
86 | .catch(function (err) {
|
87 | throw _this.toFirebaseError(err);
|
88 | });
|
89 | };
|
90 | RemoteConfigApiClient.prototype.getTemplateAtVersion = function (versionNumber) {
|
91 | var _this = this;
|
92 | var data = { versionNumber: this.validateVersionNumber(versionNumber) };
|
93 | return this.getUrl()
|
94 | .then(function (url) {
|
95 | var request = {
|
96 | method: 'GET',
|
97 | url: url + "/remoteConfig",
|
98 | headers: FIREBASE_REMOTE_CONFIG_HEADERS,
|
99 | data: data
|
100 | };
|
101 | return _this.httpClient.send(request);
|
102 | })
|
103 | .then(function (resp) {
|
104 | return _this.toRemoteConfigTemplate(resp);
|
105 | })
|
106 | .catch(function (err) {
|
107 | throw _this.toFirebaseError(err);
|
108 | });
|
109 | };
|
110 | RemoteConfigApiClient.prototype.validateTemplate = function (template) {
|
111 | var _this = this;
|
112 | template = this.validateInputRemoteConfigTemplate(template);
|
113 | return this.sendPutRequest(template, template.etag, true)
|
114 | .then(function (resp) {
|
115 |
|
116 |
|
117 |
|
118 | _this.validateEtag(resp.headers['etag']);
|
119 | return _this.toRemoteConfigTemplate(resp, template.etag);
|
120 | })
|
121 | .catch(function (err) {
|
122 | throw _this.toFirebaseError(err);
|
123 | });
|
124 | };
|
125 | RemoteConfigApiClient.prototype.publishTemplate = function (template, options) {
|
126 | var _this = this;
|
127 | template = this.validateInputRemoteConfigTemplate(template);
|
128 | var ifMatch = template.etag;
|
129 | if (options && options.force == true) {
|
130 |
|
131 |
|
132 | ifMatch = '*';
|
133 | }
|
134 | return this.sendPutRequest(template, ifMatch)
|
135 | .then(function (resp) {
|
136 | return _this.toRemoteConfigTemplate(resp);
|
137 | })
|
138 | .catch(function (err) {
|
139 | throw _this.toFirebaseError(err);
|
140 | });
|
141 | };
|
142 | RemoteConfigApiClient.prototype.rollback = function (versionNumber) {
|
143 | var _this = this;
|
144 | var data = { versionNumber: this.validateVersionNumber(versionNumber) };
|
145 | return this.getUrl()
|
146 | .then(function (url) {
|
147 | var request = {
|
148 | method: 'POST',
|
149 | url: url + "/remoteConfig:rollback",
|
150 | headers: FIREBASE_REMOTE_CONFIG_HEADERS,
|
151 | data: data
|
152 | };
|
153 | return _this.httpClient.send(request);
|
154 | })
|
155 | .then(function (resp) {
|
156 | return _this.toRemoteConfigTemplate(resp);
|
157 | })
|
158 | .catch(function (err) {
|
159 | throw _this.toFirebaseError(err);
|
160 | });
|
161 | };
|
162 | RemoteConfigApiClient.prototype.listVersions = function (options) {
|
163 | var _this = this;
|
164 | if (typeof options !== 'undefined') {
|
165 | options = this.validateListVersionsOptions(options);
|
166 | }
|
167 | return this.getUrl()
|
168 | .then(function (url) {
|
169 | var request = {
|
170 | method: 'GET',
|
171 | url: url + "/remoteConfig:listVersions",
|
172 | headers: FIREBASE_REMOTE_CONFIG_HEADERS,
|
173 | data: options
|
174 | };
|
175 | return _this.httpClient.send(request);
|
176 | })
|
177 | .then(function (resp) {
|
178 | return resp.data;
|
179 | })
|
180 | .catch(function (err) {
|
181 | throw _this.toFirebaseError(err);
|
182 | });
|
183 | };
|
184 | RemoteConfigApiClient.prototype.sendPutRequest = function (template, etag, validateOnly) {
|
185 | var _this = this;
|
186 | var path = 'remoteConfig';
|
187 | if (validateOnly) {
|
188 | path += '?validate_only=true';
|
189 | }
|
190 | return this.getUrl()
|
191 | .then(function (url) {
|
192 | var request = {
|
193 | method: 'PUT',
|
194 | url: url + "/" + path,
|
195 | headers: __assign(__assign({}, FIREBASE_REMOTE_CONFIG_HEADERS), { 'If-Match': etag }),
|
196 | data: {
|
197 | conditions: template.conditions,
|
198 | parameters: template.parameters,
|
199 | parameterGroups: template.parameterGroups,
|
200 | version: template.version,
|
201 | }
|
202 | };
|
203 | return _this.httpClient.send(request);
|
204 | });
|
205 | };
|
206 | RemoteConfigApiClient.prototype.getUrl = function () {
|
207 | return this.getProjectIdPrefix()
|
208 | .then(function (projectIdPrefix) {
|
209 | return FIREBASE_REMOTE_CONFIG_V1_API + "/" + projectIdPrefix;
|
210 | });
|
211 | };
|
212 | RemoteConfigApiClient.prototype.getProjectIdPrefix = function () {
|
213 | var _this = this;
|
214 | if (this.projectIdPrefix) {
|
215 | return Promise.resolve(this.projectIdPrefix);
|
216 | }
|
217 | return utils.findProjectId(this.app)
|
218 | .then(function (projectId) {
|
219 | if (!validator.isNonEmptyString(projectId)) {
|
220 | throw new FirebaseRemoteConfigError('unknown-error', 'Failed to determine project ID. Initialize the SDK with service account credentials, or '
|
221 | + 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
|
222 | + 'environment variable.');
|
223 | }
|
224 | _this.projectIdPrefix = "projects/" + projectId;
|
225 | return _this.projectIdPrefix;
|
226 | });
|
227 | };
|
228 | RemoteConfigApiClient.prototype.toFirebaseError = function (err) {
|
229 | if (err instanceof error_1.PrefixedFirebaseError) {
|
230 | return err;
|
231 | }
|
232 | var response = err.response;
|
233 | if (!response.isJson()) {
|
234 | return new FirebaseRemoteConfigError('unknown-error', "Unexpected response with status: " + response.status + " and body: " + response.text);
|
235 | }
|
236 | var error = response.data.error || {};
|
237 | var code = 'unknown-error';
|
238 | if (error.status && error.status in ERROR_CODE_MAPPING) {
|
239 | code = ERROR_CODE_MAPPING[error.status];
|
240 | }
|
241 | var message = error.message || "Unknown server error: " + response.text;
|
242 | return new FirebaseRemoteConfigError(code, message);
|
243 | };
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | RemoteConfigApiClient.prototype.toRemoteConfigTemplate = function (resp, customEtag) {
|
252 | var etag = (typeof customEtag == 'undefined') ? resp.headers['etag'] : customEtag;
|
253 | this.validateEtag(etag);
|
254 | return {
|
255 | conditions: resp.data.conditions,
|
256 | parameters: resp.data.parameters,
|
257 | parameterGroups: resp.data.parameterGroups,
|
258 | etag: etag,
|
259 | version: resp.data.version,
|
260 | };
|
261 | };
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | RemoteConfigApiClient.prototype.validateInputRemoteConfigTemplate = function (template) {
|
272 | var templateCopy = deep_copy_1.deepCopy(template);
|
273 | if (!validator.isNonNullObject(templateCopy)) {
|
274 | throw new FirebaseRemoteConfigError('invalid-argument', "Invalid Remote Config template: " + JSON.stringify(templateCopy));
|
275 | }
|
276 | if (!validator.isNonEmptyString(templateCopy.etag)) {
|
277 | throw new FirebaseRemoteConfigError('invalid-argument', 'ETag must be a non-empty string.');
|
278 | }
|
279 | if (!validator.isNonNullObject(templateCopy.parameters)) {
|
280 | throw new FirebaseRemoteConfigError('invalid-argument', 'Remote Config parameters must be a non-null object');
|
281 | }
|
282 | if (!validator.isNonNullObject(templateCopy.parameterGroups)) {
|
283 | throw new FirebaseRemoteConfigError('invalid-argument', 'Remote Config parameter groups must be a non-null object');
|
284 | }
|
285 | if (!validator.isArray(templateCopy.conditions)) {
|
286 | throw new FirebaseRemoteConfigError('invalid-argument', 'Remote Config conditions must be an array');
|
287 | }
|
288 | if (typeof templateCopy.version !== 'undefined') {
|
289 |
|
290 | templateCopy.version = { description: templateCopy.version.description };
|
291 | }
|
292 | return templateCopy;
|
293 | };
|
294 | |
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | RemoteConfigApiClient.prototype.validateVersionNumber = function (versionNumber, propertyName) {
|
304 | if (propertyName === void 0) { propertyName = 'versionNumber'; }
|
305 | if (!validator.isNonEmptyString(versionNumber) &&
|
306 | !validator.isNumber(versionNumber)) {
|
307 | throw new FirebaseRemoteConfigError('invalid-argument', propertyName + " must be a non-empty string in int64 format or a number");
|
308 | }
|
309 | if (!Number.isInteger(Number(versionNumber))) {
|
310 | throw new FirebaseRemoteConfigError('invalid-argument', propertyName + " must be an integer or a string in int64 format");
|
311 | }
|
312 | return versionNumber.toString();
|
313 | };
|
314 | RemoteConfigApiClient.prototype.validateEtag = function (etag) {
|
315 | if (!validator.isNonEmptyString(etag)) {
|
316 | throw new FirebaseRemoteConfigError('invalid-argument', 'ETag header is not present in the server response.');
|
317 | }
|
318 | };
|
319 | |
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | RemoteConfigApiClient.prototype.validateListVersionsOptions = function (options) {
|
329 | var optionsCopy = deep_copy_1.deepCopy(options);
|
330 | if (!validator.isNonNullObject(optionsCopy)) {
|
331 | throw new FirebaseRemoteConfigError('invalid-argument', 'ListVersionsOptions must be a non-null object.');
|
332 | }
|
333 | if (typeof optionsCopy.pageSize !== 'undefined') {
|
334 | if (!validator.isNumber(optionsCopy.pageSize)) {
|
335 | throw new FirebaseRemoteConfigError('invalid-argument', 'pageSize must be a number.');
|
336 | }
|
337 | if (optionsCopy.pageSize < 1 || optionsCopy.pageSize > 300) {
|
338 | throw new FirebaseRemoteConfigError('invalid-argument', 'pageSize must be a number between 1 and 300 (inclusive).');
|
339 | }
|
340 | }
|
341 | if (typeof optionsCopy.pageToken !== 'undefined' && !validator.isNonEmptyString(optionsCopy.pageToken)) {
|
342 | throw new FirebaseRemoteConfigError('invalid-argument', 'pageToken must be a string value.');
|
343 | }
|
344 | if (typeof optionsCopy.endVersionNumber !== 'undefined') {
|
345 | optionsCopy.endVersionNumber = this.validateVersionNumber(optionsCopy.endVersionNumber, 'endVersionNumber');
|
346 | }
|
347 | if (typeof optionsCopy.startTime !== 'undefined') {
|
348 | if (!(optionsCopy.startTime instanceof Date) && !validator.isUTCDateString(optionsCopy.startTime)) {
|
349 | throw new FirebaseRemoteConfigError('invalid-argument', 'startTime must be a valid Date object or a UTC date string.');
|
350 | }
|
351 |
|
352 | if (optionsCopy.startTime instanceof Date) {
|
353 | optionsCopy.startTime = optionsCopy.startTime.toISOString();
|
354 | }
|
355 | else {
|
356 | optionsCopy.startTime = new Date(optionsCopy.startTime).toISOString();
|
357 | }
|
358 | }
|
359 | if (typeof optionsCopy.endTime !== 'undefined') {
|
360 | if (!(optionsCopy.endTime instanceof Date) && !validator.isUTCDateString(optionsCopy.endTime)) {
|
361 | throw new FirebaseRemoteConfigError('invalid-argument', 'endTime must be a valid Date object or a UTC date string.');
|
362 | }
|
363 |
|
364 | if (optionsCopy.endTime instanceof Date) {
|
365 | optionsCopy.endTime = optionsCopy.endTime.toISOString();
|
366 | }
|
367 | else {
|
368 | optionsCopy.endTime = new Date(optionsCopy.endTime).toISOString();
|
369 | }
|
370 | }
|
371 |
|
372 | Object.keys(optionsCopy).forEach(function (key) {
|
373 | return (typeof optionsCopy[key] === 'undefined') && delete optionsCopy[key];
|
374 | });
|
375 | return optionsCopy;
|
376 | };
|
377 | return RemoteConfigApiClient;
|
378 | }());
|
379 | exports.RemoteConfigApiClient = RemoteConfigApiClient;
|
380 | var ERROR_CODE_MAPPING = {
|
381 | ABORTED: 'aborted',
|
382 | ALREADY_EXISTS: 'already-exists',
|
383 | INVALID_ARGUMENT: 'invalid-argument',
|
384 | INTERNAL: 'internal-error',
|
385 | FAILED_PRECONDITION: 'failed-precondition',
|
386 | NOT_FOUND: 'not-found',
|
387 | OUT_OF_RANGE: 'out-of-range',
|
388 | PERMISSION_DENIED: 'permission-denied',
|
389 | RESOURCE_EXHAUSTED: 'resource-exhausted',
|
390 | UNAUTHENTICATED: 'unauthenticated',
|
391 | UNKNOWN: 'unknown-error',
|
392 | };
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | var FirebaseRemoteConfigError = (function (_super) {
|
401 | __extends(FirebaseRemoteConfigError, _super);
|
402 | function FirebaseRemoteConfigError(code, message) {
|
403 | return _super.call(this, 'remote-config', code, message) || this;
|
404 | }
|
405 | return FirebaseRemoteConfigError;
|
406 | }(error_1.PrefixedFirebaseError));
|
407 | exports.FirebaseRemoteConfigError = FirebaseRemoteConfigError;
|