UNPKG

18.3 kBJavaScriptView Raw
1/*! firebase-admin v10.0.0 */
2"use strict";
3/*!
4 * Copyright 2020 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18var __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})();
31var __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};
42Object.defineProperty(exports, "__esModule", { value: true });
43exports.FirebaseRemoteConfigError = exports.RemoteConfigApiClient = void 0;
44var api_request_1 = require("../utils/api-request");
45var error_1 = require("../utils/error");
46var utils = require("../utils/index");
47var validator = require("../utils/validator");
48var deep_copy_1 = require("../utils/deep-copy");
49// Remote Config backend constants
50var FIREBASE_REMOTE_CONFIG_V1_API = 'https://firebaseremoteconfig.googleapis.com/v1';
51var FIREBASE_REMOTE_CONFIG_HEADERS = {
52 'X-Firebase-Client': "fire-admin-node/" + utils.getSdkVersion(),
53 // There is a known issue in which the ETag is not properly returned in cases where the request
54 // does not specify a compression type. Currently, it is required to include the header
55 // `Accept-Encoding: gzip` or equivalent in all requests.
56 // https://firebase.google.com/docs/remote-config/use-config-rest#etag_usage_and_forced_updates
57 'Accept-Encoding': 'gzip',
58};
59/**
60 * Class that facilitates sending requests to the Firebase Remote Config backend API.
61 *
62 * @internal
63 */
64var RemoteConfigApiClient = /** @class */ (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 // validating a template returns an etag with the suffix -0 means that your update
116 // was successfully validated. We set the etag back to the original etag of the template
117 // to allow future operations.
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 // setting `If-Match: *` forces the Remote Config template to be updated
131 // and circumvent the ETag, and the protection from that it provides.
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 * Creates a RemoteConfigTemplate from the API response.
246 * If provided, customEtag is used instead of the etag returned in the API response.
247 *
248 * @param {HttpResponse} resp API response object.
249 * @param {string} customEtag A custom etag to replace the etag fom the API response (Optional).
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 * Checks if the given RemoteConfigTemplate object is valid.
264 * The object must have valid parameters, parameter groups, conditions, and an etag.
265 * Removes output only properties from version metadata.
266 *
267 * @param {RemoteConfigTemplate} template A RemoteConfigTemplate object to be validated.
268 *
269 * @returns {RemoteConfigTemplate} The validated RemoteConfigTemplate object.
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 // exclude output only properties and keep the only input property: description
290 templateCopy.version = { description: templateCopy.version.description };
291 }
292 return templateCopy;
293 };
294 /**
295 * Checks if a given version number is valid.
296 * A version number must be an integer or a string in int64 format.
297 * If valid, returns the string representation of the provided version number.
298 *
299 * @param {string|number} versionNumber A version number to be validated.
300 *
301 * @returns {string} The validated version number as a string.
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 * Checks if a given `ListVersionsOptions` object is valid. If successful, creates a copy of the
321 * options object and convert `startTime` and `endTime` to RFC3339 UTC "Zulu" format, if present.
322 *
323 * @param {ListVersionsOptions} options An options object to be validated.
324 *
325 * @returns {ListVersionsOptions} A copy of the provided options object with timestamps converted
326 * to UTC Zulu format.
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 // Convert startTime to RFC3339 UTC "Zulu" format.
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 // Convert endTime to RFC3339 UTC "Zulu" format.
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 // Remove undefined fields from optionsCopy
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}());
379exports.RemoteConfigApiClient = RemoteConfigApiClient;
380var 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 * Firebase Remote Config error code structure. This extends PrefixedFirebaseError.
395 *
396 * @param {RemoteConfigErrorCode} code The error code.
397 * @param {string} message The error message.
398 * @constructor
399 */
400var FirebaseRemoteConfigError = /** @class */ (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));
407exports.FirebaseRemoteConfigError = FirebaseRemoteConfigError;