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.
75 lines
2.8 KiB
75 lines
2.8 KiB
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.fetchWebSetup = exports.getCachedWebSetup = void 0;
|
|
const apiv2_1 = require("./apiv2");
|
|
const configstore_1 = require("./configstore");
|
|
const api_1 = require("./api");
|
|
const projectUtils_1 = require("./projectUtils");
|
|
const logger_1 = require("./logger");
|
|
const constants_1 = require("./emulator/constants");
|
|
const apiClient = new apiv2_1.Client({ urlPrefix: api_1.firebaseApiOrigin, auth: true, apiVersion: "v1beta1" });
|
|
const hostingApiClient = new apiv2_1.Client({
|
|
urlPrefix: api_1.hostingApiOrigin,
|
|
auth: true,
|
|
apiVersion: "v1beta1",
|
|
});
|
|
const CONFIGSTORE_KEY = "webconfig";
|
|
function setCachedWebSetup(projectId, config) {
|
|
const allConfigs = configstore_1.configstore.get(CONFIGSTORE_KEY) || {};
|
|
allConfigs[projectId] = config;
|
|
configstore_1.configstore.set(CONFIGSTORE_KEY, allConfigs);
|
|
}
|
|
function getCachedWebSetup(options) {
|
|
const projectId = (0, projectUtils_1.needProjectId)(options);
|
|
const allConfigs = configstore_1.configstore.get(CONFIGSTORE_KEY) || {};
|
|
return allConfigs[projectId];
|
|
}
|
|
exports.getCachedWebSetup = getCachedWebSetup;
|
|
async function listAllSites(projectId, nextPageToken) {
|
|
const queryParams = nextPageToken ? { pageToken: nextPageToken } : {};
|
|
const res = await hostingApiClient.get(`/projects/${projectId}/sites`, {
|
|
queryParams,
|
|
});
|
|
const sites = res.body.sites;
|
|
if (res.body.nextPageToken) {
|
|
const remainder = await listAllSites(projectId, res.body.nextPageToken);
|
|
return [...sites, ...remainder];
|
|
}
|
|
return sites;
|
|
}
|
|
function constructDefaultWebSetup(projectId) {
|
|
return {
|
|
projectId,
|
|
databaseURL: `https://${projectId}.firebaseio.com`,
|
|
storageBucket: `${projectId}.appspot.com`,
|
|
apiKey: "fake-api-key",
|
|
authDomain: `${projectId}.firebaseapp.com`,
|
|
};
|
|
}
|
|
async function fetchWebSetup(options) {
|
|
const projectId = (0, projectUtils_1.needProjectId)(options);
|
|
if (constants_1.Constants.isDemoProject(projectId)) {
|
|
return constructDefaultWebSetup(projectId);
|
|
}
|
|
let hostingAppId = undefined;
|
|
try {
|
|
const sites = await listAllSites(projectId);
|
|
const defaultSite = sites.find((s) => s.type === "DEFAULT_SITE");
|
|
if (defaultSite && defaultSite.appId) {
|
|
hostingAppId = defaultSite.appId;
|
|
}
|
|
}
|
|
catch (e) {
|
|
logger_1.logger.debug("Failed to list hosting sites");
|
|
logger_1.logger.debug(e);
|
|
}
|
|
const appId = hostingAppId || "-";
|
|
const res = await apiClient.get(`/projects/${projectId}/webApps/${appId}/config`);
|
|
const config = res.body;
|
|
if (!config.appId && hostingAppId) {
|
|
config.appId = hostingAppId;
|
|
}
|
|
setCachedWebSetup(config.projectId, config);
|
|
return config;
|
|
}
|
|
exports.fetchWebSetup = fetchWebSetup;
|