firebase-tools
Version:
Command-Line Interface for Firebase
75 lines (74 loc) • 3.3 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.webApps = void 0;
const apps_1 = require("../management/apps");
const error_1 = require("../error");
const utils_1 = require("../utils");
const CREATE_NEW_FIREBASE_WEB_APP = "CREATE_NEW_WEB_APP";
const CONTINUE_WITHOUT_SELECTING_FIREBASE_WEB_APP = "CONTINUE_WITHOUT_SELECTING_FIREBASE_WEB_APP";
exports.webApps = {
CREATE_NEW_FIREBASE_WEB_APP,
CONTINUE_WITHOUT_SELECTING_FIREBASE_WEB_APP,
getOrCreateWebApp,
generateWebAppName,
};
async function getOrCreateWebApp(projectId, firebaseWebAppId, backendId) {
var _a;
const webAppsInProject = await (0, apps_1.listFirebaseApps)(projectId, apps_1.AppPlatform.WEB);
if (firebaseWebAppId) {
const webApp = webAppsInProject.find((app) => app.appId === firebaseWebAppId);
if (webApp === undefined) {
throw new error_1.FirebaseError(`The web app '${firebaseWebAppId}' does not exist in project ${projectId}`);
}
return {
name: (_a = webApp.displayName) !== null && _a !== void 0 ? _a : webApp.appId,
id: webApp.appId,
};
}
const webAppName = await generateWebAppName(projectId, backendId);
try {
const app = await (0, apps_1.createWebApp)(projectId, { displayName: webAppName });
(0, utils_1.logSuccess)(`Created a new Firebase web app named "${webAppName}"`);
return { name: app.displayName, id: app.appId };
}
catch (e) {
if (isQuotaError(e)) {
(0, utils_1.logWarning)("Unable to create a new web app, the project has reached the quota for Firebase apps. Navigate to your Firebase console to manage or delete a Firebase app to continue. ");
return;
}
throw new error_1.FirebaseError("Unable to create a Firebase web app", {
original: e instanceof Error ? e : undefined,
});
}
}
async function generateWebAppName(projectId, backendId) {
const webAppsInProject = await (0, apps_1.listFirebaseApps)(projectId, apps_1.AppPlatform.WEB);
const appsMap = firebaseAppsToMap(webAppsInProject);
if (!appsMap.get(backendId)) {
return backendId;
}
let uniqueId = 1;
let webAppName = `${backendId}_${uniqueId}`;
while (appsMap.get(webAppName)) {
uniqueId += 1;
webAppName = `${backendId}_${uniqueId}`;
}
return webAppName;
}
function firebaseAppsToMap(apps) {
return new Map(apps.map((obj) => {
var _a;
return [
(_a = obj.displayName) !== null && _a !== void 0 ? _a : obj.appId,
obj.appId,
];
}));
}
function isQuotaError(error) {
var _a, _b, _c, _d, _e;
const original = error.original;
const code = (original === null || original === void 0 ? void 0 : original.status) ||
((_b = (_a = original === null || original === void 0 ? void 0 : original.context) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.statusCode) ||
((_e = (_d = (_c = original === null || original === void 0 ? void 0 : original.context) === null || _c === void 0 ? void 0 : _c.body) === null || _d === void 0 ? void 0 : _d.error) === null || _e === void 0 ? void 0 : _e.code);
return code === 429;
}
;