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.

fetchWebSetup.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.fetchWebSetup = exports.getCachedWebSetup = void 0;
  4. const apiv2_1 = require("./apiv2");
  5. const configstore_1 = require("./configstore");
  6. const api_1 = require("./api");
  7. const projectUtils_1 = require("./projectUtils");
  8. const logger_1 = require("./logger");
  9. const constants_1 = require("./emulator/constants");
  10. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.firebaseApiOrigin, auth: true, apiVersion: "v1beta1" });
  11. const hostingApiClient = new apiv2_1.Client({
  12. urlPrefix: api_1.hostingApiOrigin,
  13. auth: true,
  14. apiVersion: "v1beta1",
  15. });
  16. const CONFIGSTORE_KEY = "webconfig";
  17. function setCachedWebSetup(projectId, config) {
  18. const allConfigs = configstore_1.configstore.get(CONFIGSTORE_KEY) || {};
  19. allConfigs[projectId] = config;
  20. configstore_1.configstore.set(CONFIGSTORE_KEY, allConfigs);
  21. }
  22. function getCachedWebSetup(options) {
  23. const projectId = (0, projectUtils_1.needProjectId)(options);
  24. const allConfigs = configstore_1.configstore.get(CONFIGSTORE_KEY) || {};
  25. return allConfigs[projectId];
  26. }
  27. exports.getCachedWebSetup = getCachedWebSetup;
  28. async function listAllSites(projectId, nextPageToken) {
  29. const queryParams = nextPageToken ? { pageToken: nextPageToken } : {};
  30. const res = await hostingApiClient.get(`/projects/${projectId}/sites`, {
  31. queryParams,
  32. });
  33. const sites = res.body.sites;
  34. if (res.body.nextPageToken) {
  35. const remainder = await listAllSites(projectId, res.body.nextPageToken);
  36. return [...sites, ...remainder];
  37. }
  38. return sites;
  39. }
  40. function constructDefaultWebSetup(projectId) {
  41. return {
  42. projectId,
  43. databaseURL: `https://${projectId}.firebaseio.com`,
  44. storageBucket: `${projectId}.appspot.com`,
  45. apiKey: "fake-api-key",
  46. authDomain: `${projectId}.firebaseapp.com`,
  47. };
  48. }
  49. async function fetchWebSetup(options) {
  50. const projectId = (0, projectUtils_1.needProjectId)(options);
  51. if (constants_1.Constants.isDemoProject(projectId)) {
  52. return constructDefaultWebSetup(projectId);
  53. }
  54. let hostingAppId = undefined;
  55. try {
  56. const sites = await listAllSites(projectId);
  57. const defaultSite = sites.find((s) => s.type === "DEFAULT_SITE");
  58. if (defaultSite && defaultSite.appId) {
  59. hostingAppId = defaultSite.appId;
  60. }
  61. }
  62. catch (e) {
  63. logger_1.logger.debug("Failed to list hosting sites");
  64. logger_1.logger.debug(e);
  65. }
  66. const appId = hostingAppId || "-";
  67. const res = await apiClient.get(`/projects/${projectId}/webApps/${appId}/config`);
  68. const config = res.body;
  69. if (!config.appId && hostingAppId) {
  70. config.appId = hostingAppId;
  71. }
  72. setCachedWebSetup(config.projectId, config);
  73. return config;
  74. }
  75. exports.fetchWebSetup = fetchWebSetup;