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

75 lines (74 loc) 3.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LATEST_PRICING2YAML_VERSION = exports.PRICING2YAML_VERSIONS = void 0; exports.calculateNextVersion = calculateNextVersion; exports.update = update; // deno-lint-ignore-file no-explicit-any var semver_1 = __importDefault(require("semver")); var pricing_validators_1 = require("../../main/utils/pricing-validators"); var updaters_1 = require("./version-updaters/updaters"); var yaml_utils_1 = require("../../server/utils/yaml-utils"); exports.PRICING2YAML_VERSIONS = ["1.0", "1.1", "2.0", "2.1", "3.0", "3.1"]; exports.LATEST_PRICING2YAML_VERSION = exports.PRICING2YAML_VERSIONS[exports.PRICING2YAML_VERSIONS.length - 1]; function calculateNextVersion(currentVersion) { var currentVersionIndex = exports.PRICING2YAML_VERSIONS.indexOf(currentVersion); var nextVersion = currentVersionIndex === exports.PRICING2YAML_VERSIONS.length - 1 ? null : exports.PRICING2YAML_VERSIONS[currentVersionIndex + 1]; return nextVersion; } function update(extractedPricing, useCheckpoints, pricingPath) { var _a, _b; if (useCheckpoints && pricingPath === undefined) { throw new Error("Pricing path is required for version checking."); } var pricingVersion = _parseToSemver((0, pricing_validators_1.validateSyntaxVersion)((_a = extractedPricing.syntaxVersion) !== null && _a !== void 0 ? _a : extractedPricing.version)); var latestVersion = _parseToSemver(exports.LATEST_PRICING2YAML_VERSION); if (!semver_1.default.eq(pricingVersion, latestVersion)) { if (exports.PRICING2YAML_VERSIONS.includes((_b = extractedPricing.syntaxVersion) !== null && _b !== void 0 ? _b : extractedPricing.version)) { if (useCheckpoints) _performUpdate(extractedPricing, pricingPath); else _performUpdate(extractedPricing); } else { throw new Error("Unsupported version: ".concat(pricingVersion.major, ".").concat(pricingVersion.minor, ". Please, visit the changelogs of Pricing2Yaml to check the supported versions.")); } } else if (semver_1.default.gt(pricingVersion, latestVersion)) { throw new Error("Wow! You have travelled to the future! This version of Pricing2Yaml is greater than the latest version available: ".concat(exports.LATEST_PRICING2YAML_VERSION)); } return extractedPricing; } function _performUpdate(extractedPricing, pricingPath) { var _a, _b; var currentVersion = (_a = extractedPricing.syntaxVersion) !== null && _a !== void 0 ? _a : extractedPricing.version; var nextVersion = calculateNextVersion(currentVersion); var updater = updaters_1.updaters[(_b = extractedPricing.syntaxVersion) !== null && _b !== void 0 ? _b : extractedPricing.version]; if (updater === null) { if (pricingPath !== undefined) (0, yaml_utils_1.writePricingToYaml)(extractedPricing, pricingPath); return; } else { var updatedPricing = null; try { updatedPricing = updater(extractedPricing); } catch (err) { if (pricingPath !== undefined) (0, yaml_utils_1.writePricingWithErrorToYaml)(extractedPricing, pricingPath); throw new Error("An error occurred while updating from version ".concat(currentVersion, " to ").concat(nextVersion, ". Your Pricing2Yaml model has been saved at version ").concat(currentVersion, ". Please resolve the issues and try again. Error details: ").concat(err.message, ".")); } _performUpdate(updatedPricing, pricingPath); } } function _parseToSemver(version) { var parsedVersion = semver_1.default.parse(version + ".0"); if (parsedVersion === null) throw new Error("Invalid version: ".concat(version)); return parsedVersion; }