pricing4react
Version:
A library of components that ease the integration of feature toggling driven by pricing plans into your React application's UI.
116 lines (115 loc) • 4.69 kB
JavaScript
;
exports.__esModule = true;
exports.ReactPricingDrivenFeaturesProvider = void 0;
var react_sdk_1 = require("@openfeature/react-sdk");
var index_1 = require("../index");
var ReactPricingDrivenFeaturesProvider = /** @class */ (function () {
function ReactPricingDrivenFeaturesProvider() {
this.metadata = {
name: 'pricing-driven-features',
description: 'An Open Feature provider that enables features based on pricing information'
};
this.runsOn = 'client';
this.events = new react_sdk_1.OpenFeatureEventEmitter();
this.evaluation = {};
}
ReactPricingDrivenFeaturesProvider.prototype.onContextChange = function (oldContext, newContext) {
this.evaluation = (0, index_1.evaluatePricing)(this.pricingYaml, newContext.subscription, newContext.userContext);
};
ReactPricingDrivenFeaturesProvider.prototype.resolveBooleanEvaluation = function (flagKey, defaultValue) {
try {
return {
value: this._evaluateFeature(flagKey).value.eval
};
}
catch (error) {
console.error('Error occurred during evaluation. ERROR: ', error.message);
return {
value: defaultValue
};
}
};
ReactPricingDrivenFeaturesProvider.prototype.resolveStringEvaluation = function (flagKey, defaultValue) {
try {
var result = this._evaluateFeature(flagKey);
return {
value: result.value.eval.toString()
};
}
catch (error) {
console.error('Error occurred during evaluation. ERROR: ', error.message);
return {
value: defaultValue
};
}
};
ReactPricingDrivenFeaturesProvider.prototype.resolveNumberEvaluation = function (flagKey, defaultValue) {
try {
var result = this._evaluateFeature(flagKey);
return {
value: result.value.eval ? 1 : 0
};
}
catch (error) {
console.error('Error occurred during evaluation. ERROR: ', error.message);
return {
value: defaultValue
};
}
};
ReactPricingDrivenFeaturesProvider.prototype.resolveObjectEvaluation = function (flagKey, defaultValue) {
try {
return this._evaluateFeature(flagKey);
}
catch (error) {
console.error('Error occurred during evaluation. ERROR: ', error.message);
return {
value: defaultValue
};
}
};
ReactPricingDrivenFeaturesProvider.prototype.initialize = function (context) {
var _this = this;
if (context.pricingUrl) {
this.pricingUrl = context.pricingUrl;
if (!context.subscription) {
return Promise.reject("Subscription not provided in context. Use 'subscription' to provide one. It's value must be a list comprised of at least one name of plan/add-on.");
}
if (!context.userContext) {
return Promise.reject("User context not provided in context. Use 'userContext' to provide one. It's value must be a JSON object with at least the key 'user', with a string value identifying the user.");
}
return fetch(this.pricingUrl)
.then(function (response) { return response.text(); })
.then(function (yaml) {
_this.pricingYaml = yaml;
_this.evaluation = (0, index_1.evaluatePricing)(_this.pricingYaml, context.subscription, context.userContext);
console.log("PricingDrivenFeaturesProvider initialized with context: ", context);
});
}
else {
return Promise.reject("Pricing URL not provided in context. Use 'pricingUrl' to provide one.");
}
};
ReactPricingDrivenFeaturesProvider.prototype._evaluateFeature = function (flagKey) {
if (Object.keys(this.evaluation).length === 0) {
return {
value: {
eval: false,
used: null,
limit: null,
error: {
code: 'PROVIDER_NOT_READY',
message: 'Pricing not yet loaded into the provider'
}
}
};
}
else {
return {
value: this.evaluation[flagKey]
};
}
};
return ReactPricingDrivenFeaturesProvider;
}());
exports.ReactPricingDrivenFeaturesProvider = ReactPricingDrivenFeaturesProvider;