pricing4ts
Version:
 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
143 lines (142 loc) • 5.89 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PricingJwtUtils = void 0;
var jwt = __importStar(require("jwt-simple"));
var jwt_simple_1 = require("jwt-simple");
var PricingTokenError_1 = require("../exceptions/PricingTokenError");
/**
* Utility class for handling JSON Web Tokens related to pricing.
*/
var PricingJwtUtils = /** @class */ (function () {
function PricingJwtUtils() {
}
/**
* Decodes a Pricing JSON Web Token.
*
* @param token - The JWT to decode.
* @returns The decoded token as a Record<string, any.
* @throws {PricingTokenError} If the token is not found or expired.
*/
PricingJwtUtils.decodeToken = function (token) {
if (token === undefined || token === null) {
throw new PricingTokenError_1.PricingTokenError("Pricing token not found");
}
else if (this.isJwtExpired(token)) {
throw new PricingTokenError_1.PricingTokenError("Pricing token expired");
}
var decodedToken = (0, jwt_simple_1.decode)(token, this.context.getJwtSecret());
return decodedToken;
};
/**
* Encodes a set of claims into a JSON Web Token (JWT).
*
* @param claims - The claims to encode into the token.
* @returns The encoded JWT.
* @throws {PricingTokenError} If required claims are missing.
*/
PricingJwtUtils.encodeToken = function (claims) {
if (!claims) {
throw new PricingTokenError_1.PricingTokenError("[ERROR] Claims not found when encoding token");
}
else if (!claims.sub) {
throw new PricingTokenError_1.PricingTokenError("[ERROR] Subject not found in claims when encoding token");
}
else if (!claims.subscriptionContext) {
throw new PricingTokenError_1.PricingTokenError("[ERROR] User context not found in claims when encoding token");
}
claims.exp = Math.floor(Date.now()) + this.context.getJwtExpiration();
var encodedToken = (0, jwt_simple_1.encode)(claims, this.context.getJwtSecret(), 'HS512', {
header: { alg: 'HS512', typ: 'JWT' },
});
return encodedToken;
};
/**
* Extracts the evaluated features from a Pricing JSON Web Token (JWT).
*
* @param token - The JWT to extract features from.
* @returns The features as a Record<string, {@link FeatureStatus}>.
*/
PricingJwtUtils.getFeaturesFromJwtToken = function (token) {
var decodedToken = this.decodeToken(token);
var tokenFeatures = decodedToken.features;
return tokenFeatures;
};
/**
* Extracts the subject from a Pricing JSON Web Token (JWT).
*
* @param token - The JWT to extract the subject from.
* @returns The subject as a string.
*/
PricingJwtUtils.getSubjectFromJwtToken = function (token) {
var decodedToken = this.decodeToken(token);
var tokenSubject = decodedToken.sub;
return tokenSubject;
};
/**
* Updates the features in a Pricing JSON Web Token (JWT).
*
* @param oldToken - The JWT to update.
* @param newFeatureStatuses - A *Record<string, {@link FeatureStatus}>* showcasing the new features evaluation to update in the token.
* @returns The updated JWT.
*/
PricingJwtUtils.updateTokenFeatures = function (oldToken, newFeatureStatuses) {
var decodedToken = this.decodeToken(oldToken);
decodedToken.features = newFeatureStatuses;
return this.encodeToken(decodedToken);
};
/**
* Updates a specific feature in a Pricing JSON Web Token (JWT).
*
* @param oldToken - The JWT to update.
* @param newFeatureStatuses - A *Record<string, {@link FeatureStatus}>* showcasing the new features evaluation to update in the token.
* @returns The updated JWT.
*/
PricingJwtUtils.updateEvalOfTokenFeature = function (oldToken, featureToChangeEval, newEval) {
var decodedToken = this.decodeToken(oldToken);
decodedToken.features[featureToChangeEval].eval = newEval;
return this.encodeToken(decodedToken);
};
/**
* Configures the {@links PricingContext} that will be used to perform the JWT operations.
*
* @param context - The {@link PricingContext} to set.
*/
PricingJwtUtils.setContext = function (context) {
this.context = context;
};
/**
* Checks if a JSON Web Token is expired.
*
* @param token - The JWT to check.
* @returns *true* if the token is expired, *false* otherwise.
*/
PricingJwtUtils.isJwtExpired = function (token) {
var decodedToken = jwt.decode(token, this.context.getJwtSecret());
return decodedToken.exp < new Date().getTime();
};
return PricingJwtUtils;
}());
exports.PricingJwtUtils = PricingJwtUtils;