UNPKG

@vtex/api

Version:
50 lines (49 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getServiceSettings = exports.getDependenciesSettings = exports.getDependenciesHash = exports.getFilteredDependencies = void 0; const crypto_1 = require("crypto"); const ramda_1 = require("ramda"); const constants_1 = require("../../../../../constants"); const app_1 = require("./../../../../../utils/app"); const joinIds = (0, ramda_1.join)(''); const dependsOnApp = (appAtMajor) => (a) => { const [name, major] = appAtMajor.split('@'); const majorInt = major.includes('.') ? major.split('.')[0] : major; const version = a._resolvedDependencies[name]; if (!version) { return false; } const [depMajor] = version.split('.'); return majorInt === depMajor; }; const getFilteredDependencies = (appAtMajor, dependencies) => { const depends = dependsOnApp(appAtMajor); return dependencies.filter(depends); }; exports.getFilteredDependencies = getFilteredDependencies; const getDependenciesHash = (dependencies) => { const dependingApps = (0, ramda_1.pluck)('id', dependencies); return (0, crypto_1.createHash)('md5') .update(joinIds(dependingApps)) .digest('hex'); }; exports.getDependenciesHash = getDependenciesHash; const getDependenciesSettings = async (apps, assets, tracingConfig) => { const appId = constants_1.APP.ID; const metaInfos = await apps.getAppsMetaInfos(undefined, undefined, tracingConfig); const appAtMajor = (0, app_1.appIdToAppAtMajor)(appId); return await assets.getSettings(metaInfos, appAtMajor, undefined, tracingConfig); }; exports.getDependenciesSettings = getDependenciesSettings; const getServiceSettings = () => { return async function settingsContext(ctx, next) { var _a; const { clients: { apps, assets }, } = ctx; const rootSpan = (_a = ctx.tracing) === null || _a === void 0 ? void 0 : _a.currentSpan; const dependenciesSettings = await (0, exports.getDependenciesSettings)(apps, assets, { tracing: { rootSpan } }); // TODO: for now returning all settings, but the ideia is to do merge ctx.vtex.settings = dependenciesSettings; await next(); }; }; exports.getServiceSettings = getServiceSettings;