@feature-hub/core
Version:
Create scalable web applications using micro frontends.
46 lines • 2.25 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExternalsValidator = void 0;
const satisfies_1 = __importDefault(require("semver/functions/satisfies"));
const valid_1 = __importDefault(require("semver/functions/valid"));
/**
* The `ExternalsValidator` validates required externals against the provided
* set of externals it is initilized with.
*/
class ExternalsValidator {
/**
* @throws Throws an error if the provided externals contain an invalid
* version.
*/
constructor(providedExternals) {
this.providedExternals = providedExternals;
for (const [externalName, providedVersion] of Object.entries(providedExternals)) {
if (!(0, valid_1.default)(providedVersion)) {
throw new Error(`The provided version ${JSON.stringify(providedVersion)} for the external ${JSON.stringify(externalName)} is invalid.`);
}
}
}
/**
* Validate that the required externals are provided in a compatible version.
*
* @throws Throws an error if the required externals can't be satisfied.
*/
validate(requiredExternals, consumerId) {
for (const [externalName, versionRange] of Object.entries(requiredExternals)) {
const providedVersion = this.providedExternals[externalName];
if (!providedVersion) {
throw new Error(`The external dependency ${JSON.stringify(externalName)}${consumerId ? ` as required by ${JSON.stringify(consumerId)}` : ``} is not provided.`);
}
if (!(0, satisfies_1.default)(providedVersion, versionRange)) {
throw new Error(`The external dependency ${JSON.stringify(externalName)} ${consumerId
? `as required by ${JSON.stringify(consumerId)} in the`
: `in the required`} version range ${JSON.stringify(versionRange)} is not satisfied. The provided version is ${JSON.stringify(providedVersion)}.`);
}
}
}
}
exports.ExternalsValidator = ExternalsValidator;
//# sourceMappingURL=externals-validator.js.map
;