暫無描述
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.

runtimeconfig.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.variables = exports.configs = void 0;
  4. const _ = require("lodash");
  5. const api_1 = require("../api");
  6. const apiv2_1 = require("../apiv2");
  7. const logger_1 = require("../logger");
  8. const API_VERSION = "v1beta1";
  9. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.runtimeconfigOrigin, apiVersion: API_VERSION });
  10. function listConfigs(projectId) {
  11. return apiClient
  12. .get(`/projects/${projectId}/configs`, {
  13. retryCodes: [500, 503],
  14. })
  15. .then((resp) => resp.body.configs);
  16. }
  17. function createConfig(projectId, configId) {
  18. const path = ["projects", projectId, "configs"].join("/");
  19. return apiClient
  20. .post(`/projects/${projectId}/configs`, {
  21. name: path + "/" + configId,
  22. }, {
  23. retryCodes: [500, 503],
  24. })
  25. .catch((err) => {
  26. if (_.get(err, "context.response.statusCode") === 409) {
  27. return Promise.resolve();
  28. }
  29. return Promise.reject(err);
  30. });
  31. }
  32. function deleteConfig(projectId, configId) {
  33. return apiClient
  34. .delete(`/projects/${projectId}/configs/${configId}`, {
  35. retryCodes: [500, 503],
  36. })
  37. .catch((err) => {
  38. if (_.get(err, "context.response.statusCode") === 404) {
  39. logger_1.logger.debug("Config already deleted.");
  40. return Promise.resolve();
  41. }
  42. throw err;
  43. });
  44. }
  45. function listVariables(configPath) {
  46. return apiClient
  47. .get(`${configPath}/variables`, {
  48. retryCodes: [500, 503],
  49. })
  50. .then((resp) => {
  51. return Promise.resolve(resp.body.variables || []);
  52. });
  53. }
  54. function getVariable(varPath) {
  55. return apiClient
  56. .get(varPath, {
  57. retryCodes: [500, 503],
  58. })
  59. .then((resp) => {
  60. return Promise.resolve(resp.body);
  61. });
  62. }
  63. function createVariable(projectId, configId, varId, value) {
  64. const path = `/projects/${projectId}/configs/${configId}/variables`;
  65. return apiClient
  66. .post(path, {
  67. name: `${path}/${varId}`,
  68. text: value,
  69. }, {
  70. retryCodes: [500, 503],
  71. })
  72. .catch((err) => {
  73. if (_.get(err, "context.response.statusCode") === 404) {
  74. return createConfig(projectId, configId).then(() => {
  75. return createVariable(projectId, configId, varId, value);
  76. });
  77. }
  78. return Promise.reject(err);
  79. });
  80. }
  81. function updateVariable(projectId, configId, varId, value) {
  82. const path = `/projects/${projectId}/configs/${configId}/variables/${varId}`;
  83. return apiClient.put(path, {
  84. name: path,
  85. text: value,
  86. }, {
  87. retryCodes: [500, 503],
  88. });
  89. }
  90. function setVariable(projectId, configId, varId, value) {
  91. const path = ["projects", projectId, "configs", configId, "variables", varId].join("/");
  92. return getVariable(path)
  93. .then(() => {
  94. return updateVariable(projectId, configId, varId, value);
  95. })
  96. .catch((err) => {
  97. if (_.get(err, "context.response.statusCode") === 404) {
  98. return createVariable(projectId, configId, varId, value);
  99. }
  100. return Promise.reject(err);
  101. });
  102. }
  103. function deleteVariable(projectId, configId, varId) {
  104. return apiClient
  105. .delete(`/projects/${projectId}/configs/${configId}/variables/${varId}`, {
  106. retryCodes: [500, 503],
  107. queryParams: { recursive: "true" },
  108. })
  109. .catch((err) => {
  110. if (_.get(err, "context.response.statusCode") === 404) {
  111. logger_1.logger.debug("Variable already deleted.");
  112. return Promise.resolve();
  113. }
  114. return Promise.reject(err);
  115. });
  116. }
  117. exports.configs = {
  118. list: listConfigs,
  119. create: createConfig,
  120. delete: deleteConfig,
  121. };
  122. exports.variables = {
  123. list: listVariables,
  124. get: getVariable,
  125. set: setVariable,
  126. delete: deleteVariable,
  127. };