No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.publishTemplate = exports.deployTemplate = exports.validateInputRemoteConfigTemplate = exports.getEtag = void 0;
  4. const api_1 = require("../../api");
  5. const apiv2_1 = require("../../apiv2");
  6. const error_1 = require("../../error");
  7. const TIMEOUT = 30000;
  8. const client = new apiv2_1.Client({ urlPrefix: api_1.remoteConfigApiOrigin, apiVersion: "v1" });
  9. async function getEtag(projectNumber, versionNumber) {
  10. const reqPath = `/projects/${projectNumber}/remoteConfig`;
  11. const queryParams = {};
  12. if (versionNumber) {
  13. queryParams.versionNumber = versionNumber;
  14. }
  15. const response = await client.request({
  16. method: "GET",
  17. path: reqPath,
  18. queryParams,
  19. headers: { "Accept-Encoding": "gzip" },
  20. timeout: TIMEOUT,
  21. });
  22. return response.response.headers.get("etag") || "";
  23. }
  24. exports.getEtag = getEtag;
  25. function validateInputRemoteConfigTemplate(template) {
  26. const templateCopy = JSON.parse(JSON.stringify(template));
  27. if (!templateCopy || templateCopy === "null" || templateCopy === "undefined") {
  28. throw new error_1.FirebaseError(`Invalid Remote Config template: ${JSON.stringify(templateCopy)}`);
  29. }
  30. if (typeof templateCopy.etag !== "string" || templateCopy.etag === "") {
  31. throw new error_1.FirebaseError("ETag must be a non-empty string");
  32. }
  33. if (templateCopy.conditions && !Array.isArray(templateCopy.conditions)) {
  34. throw new error_1.FirebaseError("Remote Config conditions must be an array");
  35. }
  36. return templateCopy;
  37. }
  38. exports.validateInputRemoteConfigTemplate = validateInputRemoteConfigTemplate;
  39. async function deployTemplate(projectNumber, template, etag, options) {
  40. const reqPath = `/projects/${projectNumber}/remoteConfig`;
  41. if (options === null || options === void 0 ? void 0 : options.force) {
  42. etag = "*";
  43. }
  44. const response = await client.request({
  45. method: "PUT",
  46. path: reqPath,
  47. headers: { "If-Match": etag },
  48. body: {
  49. conditions: template.conditions,
  50. parameters: template.parameters,
  51. parameterGroups: template.parameterGroups,
  52. },
  53. timeout: TIMEOUT,
  54. });
  55. return response.body;
  56. }
  57. exports.deployTemplate = deployTemplate;
  58. function publishTemplate(projectNumber, template, etag, options) {
  59. const temporaryTemplate = {
  60. conditions: template.conditions,
  61. parameters: template.parameters,
  62. parameterGroups: template.parameterGroups,
  63. etag: etag,
  64. };
  65. let validTemplate = temporaryTemplate;
  66. validTemplate = validateInputRemoteConfigTemplate(template);
  67. return deployTemplate(projectNumber, validTemplate, etag, options);
  68. }
  69. exports.publishTemplate = publishTemplate;