@feature-hub/core
Version:
Create scalable web applications using micro frontends.
39 lines • 1.9 kB
JavaScript
import semverSatisfies from 'semver/functions/satisfies';
import semverValid from 'semver/functions/valid';
/**
* The `ExternalsValidator` validates required externals against the provided
* set of externals it is initilized with.
*/
export 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 (!semverValid(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 (!semverSatisfies(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)}.`);
}
}
}
}
//# sourceMappingURL=externals-validator.js.map