Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ensureApiEnabled.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.enableApiURI = exports.ensure = exports.check = exports.POLL_SETTINGS = void 0;
  4. const colorette_1 = require("colorette");
  5. const track_1 = require("./track");
  6. const api_1 = require("./api");
  7. const apiv2_1 = require("./apiv2");
  8. const utils = require("./utils");
  9. const error_1 = require("./error");
  10. exports.POLL_SETTINGS = {
  11. pollInterval: 10000,
  12. pollsBeforeRetry: 12,
  13. };
  14. const apiClient = new apiv2_1.Client({
  15. urlPrefix: api_1.serviceUsageOrigin,
  16. apiVersion: "v1",
  17. });
  18. async function check(projectId, apiName, prefix, silent = false) {
  19. const res = await apiClient.get(`/projects/${projectId}/services/${apiName}`, {
  20. headers: { "x-goog-quota-user": `projects/${projectId}` },
  21. skipLog: { resBody: true },
  22. });
  23. const isEnabled = res.body.state === "ENABLED";
  24. if (isEnabled && !silent) {
  25. utils.logLabeledSuccess(prefix, `required API ${(0, colorette_1.bold)(apiName)} is enabled`);
  26. }
  27. return isEnabled;
  28. }
  29. exports.check = check;
  30. async function enable(projectId, apiName) {
  31. try {
  32. await apiClient.post(`/projects/${projectId}/services/${apiName}:enable`, undefined, {
  33. headers: { "x-goog-quota-user": `projects/${projectId}` },
  34. skipLog: { resBody: true },
  35. });
  36. }
  37. catch (err) {
  38. if ((0, error_1.isBillingError)(err)) {
  39. throw new error_1.FirebaseError(`Your project ${(0, colorette_1.bold)(projectId)} must be on the Blaze (pay-as-you-go) plan to complete this command. Required API ${(0, colorette_1.bold)(apiName)} can't be enabled until the upgrade is complete. To upgrade, visit the following URL:
  40. https://console.firebase.google.com/project/${projectId}/usage/details`);
  41. }
  42. throw err;
  43. }
  44. }
  45. async function pollCheckEnabled(projectId, apiName, prefix, silent, enablementRetries, pollRetries = 0) {
  46. if (pollRetries > exports.POLL_SETTINGS.pollsBeforeRetry) {
  47. return enableApiWithRetries(projectId, apiName, prefix, silent, enablementRetries + 1);
  48. }
  49. await new Promise((resolve) => {
  50. setTimeout(resolve, exports.POLL_SETTINGS.pollInterval);
  51. });
  52. const isEnabled = await check(projectId, apiName, prefix, silent);
  53. if (isEnabled) {
  54. void (0, track_1.track)("api_enabled", apiName);
  55. return;
  56. }
  57. if (!silent) {
  58. utils.logLabeledBullet(prefix, `waiting for API ${(0, colorette_1.bold)(apiName)} to activate...`);
  59. }
  60. return pollCheckEnabled(projectId, apiName, prefix, silent, enablementRetries, pollRetries + 1);
  61. }
  62. async function enableApiWithRetries(projectId, apiName, prefix, silent, enablementRetries = 0) {
  63. if (enablementRetries > 1) {
  64. throw new error_1.FirebaseError(`Timed out waiting for API ${(0, colorette_1.bold)(apiName)} to enable. Please try again in a few minutes.`);
  65. }
  66. await enable(projectId, apiName);
  67. return pollCheckEnabled(projectId, apiName, prefix, silent, enablementRetries);
  68. }
  69. async function ensure(projectId, apiName, prefix, silent = false) {
  70. if (!silent) {
  71. utils.logLabeledBullet(prefix, `ensuring required API ${(0, colorette_1.bold)(apiName)} is enabled...`);
  72. }
  73. const isEnabled = await check(projectId, apiName, prefix, silent);
  74. if (isEnabled) {
  75. return;
  76. }
  77. if (!silent) {
  78. utils.logLabeledWarning(prefix, `missing required API ${(0, colorette_1.bold)(apiName)}. Enabling now...`);
  79. }
  80. return enableApiWithRetries(projectId, apiName, prefix, silent);
  81. }
  82. exports.ensure = ensure;
  83. function enableApiURI(projectId, apiName) {
  84. return `https://console.cloud.google.com/apis/library/${apiName}?project=${projectId}`;
  85. }
  86. exports.enableApiURI = enableApiURI;