@martinmilo/verve
Version:
TypeScript domain modeling library with field-level authorization, business rule validation, and context-aware access control
40 lines • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.can = can;
exports.getCanCondition = getCanCondition;
const canRules = new WeakMap();
function can(condition) {
return function (target, context) {
// Handle new stage 3 decorators (TypeScript 5.0+)
if (context && typeof context === 'object' && 'kind' in context) {
const ctx = context;
if (ctx.kind === 'method') {
const propertyKey = ctx.name;
const targetObject = target;
let rules = canRules.get(targetObject);
if (!rules) {
rules = {};
canRules.set(targetObject, rules);
}
rules[propertyKey] = condition;
return target; // Return the original method for stage 3 decorators
}
}
// Handle legacy decorators (experimentalDecorators)
const propertyKey = context;
const descriptor = arguments[2];
const targetObject = typeof target === 'function' ? target.prototype : target;
let rules = canRules.get(targetObject);
if (!rules) {
rules = {};
canRules.set(targetObject, rules);
}
rules[propertyKey] = condition;
return descriptor;
};
}
function getCanCondition(target, methodName) {
var _a;
return (_a = canRules.get(target)) === null || _a === void 0 ? void 0 : _a[methodName];
}
//# sourceMappingURL=can.js.map