UNPKG

pricing4ts

Version:

![NPM Version](https://img.shields.io/npm/v/pricing4ts) Pricing4TS is a TypeScript-based toolkit designed to enhance the server-side functionality of a pricing-driven SaaS by enabling the seamless integration of pricing plans into the application logic. T

44 lines (43 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PricingAware = PricingAware; var server_1 = require("../server"); var pricing_evaluator_1 = require("../utils/pricing-evaluator"); function PricingAware(featureName) { return function (target, context) { if (context.kind !== 'method') { throw new Error('PricingAware decorator can only be applied to methods!'); } return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var pricingContext = server_1.PricingContextManager.getContext(); var evaluationResult = evaluateContext(pricingContext, featureName); if (!evaluationResult) { throw new Error("Feature ".concat(featureName, " is not enabled for the current user!")); } // Si pasa las validaciones, llama a la función original return target.apply(this, args); }; }; } function evaluateContext(evaluationContext, featureName) { var _a; var configuration = evaluationContext.getPlanContext(); var featureToEval = configuration.features[featureName]; var evaluationExpression = (_a = featureToEval.serverExpression) !== null && _a !== void 0 ? _a : featureToEval.expression; if (!evaluationExpression) { console.warn("[WARNING] Feature ".concat(featureName, " has no expression defined!")); return false; } var subscriptionContext = evaluationContext.getSubscriptionContext(); var pricingContext = (0, pricing_evaluator_1.extractContextToEvalFromSubscriptionContext)(configuration); var evalResult = eval(evaluationExpression); if (typeof evalResult !== 'boolean') { console.warn("[WARNING] Feature ".concat(featureName, " has an expression that does not return a boolean!")); return false; } return evalResult; }