@casl/angular
Version:
Angular module for CASL which makes it easy to add permissions in any Angular app
152 lines (144 loc) • 7.32 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@casl/ability'), require('rxjs'), require('tslib')) :
typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@casl/ability', 'rxjs', 'tslib'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.casl = global.casl || {}, global.casl.ng = {}), global.ng.core, global.casl, global.rxjs, global.tslib));
})(this, (function (exports, i0, ability, rxjs, tslib) { 'use strict';
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var i0__namespace = /*#__PURE__*/_interopNamespaceDefault(i0);
/**
* @deprecated use AbilityService instead
*/
var AblePipe = /** @class */ (function () {
function AblePipe(ability) {
this._ability = ability;
}
AblePipe.prototype.transform = function () {
var _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return (_a = this._ability).can.apply(_a, args);
};
AblePipe.ɵfac = function AblePipe_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AblePipe)(i0__namespace.ɵɵdirectiveInject(ability.PureAbility, 16)); };
AblePipe.ɵpipe = /*@__PURE__*/ i0__namespace.ɵɵdefinePipe({ name: "able", type: AblePipe, pure: false });
return AblePipe;
}());
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(AblePipe, [{
type: i0.Pipe,
args: [{ name: 'able', pure: false, standalone: true }]
}], function () { return [{ type: undefined, decorators: [{
type: i0.Inject,
args: [ability.PureAbility]
}] }]; }, null); })();
/**
* @deprecated use AbilityService instead
*/
var AblePurePipe = /** @class */ (function () {
function AblePurePipe(ability) {
this._ability = ability;
}
AblePurePipe.prototype.transform = function () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return new rxjs.Observable(function (s) {
var emit = function () {
var _a;
return s.next((_a = _this._ability).can.apply(_a, args));
};
emit();
return _this._ability.on('updated', emit);
});
};
AblePurePipe.ɵfac = function AblePurePipe_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AblePurePipe)(i0__namespace.ɵɵdirectiveInject(ability.PureAbility, 16)); };
AblePurePipe.ɵpipe = /*@__PURE__*/ i0__namespace.ɵɵdefinePipe({ name: "ablePure", type: AblePurePipe, pure: true });
return AblePurePipe;
}());
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(AblePurePipe, [{
type: i0.Pipe,
args: [{ name: 'ablePure', standalone: true }]
}], function () { return [{ type: undefined, decorators: [{
type: i0.Inject,
args: [ability.PureAbility]
}] }]; }, null); })();
var AbilityService = /** @class */ (function () {
function AbilityService(ability) {
this.ability$ = new rxjs.Observable(function (observer) {
observer.next(ability);
return ability.on('updated', function () { return observer.next(ability); });
});
}
AbilityService.ɵfac = function AbilityService_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AbilityService)(i0__namespace.ɵɵinject(ability.PureAbility)); };
AbilityService.ɵprov = /*@__PURE__*/ i0__namespace.ɵɵdefineInjectable({ token: AbilityService, factory: AbilityService.ɵfac, providedIn: 'root' });
return AbilityService;
}());
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(AbilityService, [{
type: i0.Injectable,
args: [{ providedIn: 'root' }]
}], function () { return [{ type: undefined, decorators: [{
type: i0.Inject,
args: [ability.PureAbility]
}] }]; }, null); })();
var AbilityServiceSignal = /** @class */ (function () {
function AbilityServiceSignal() {
var _this = this;
this._rules = i0.signal.apply(void 0, tslib.__spreadArray([[]], (ngDevMode ? [{ debugName: "_rules" }] : []), false));
this._ability = i0.inject(ability.PureAbility);
this.can = function () {
var _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
_this._rules(); // generate side effect for angular to track changes in this signal
return (_a = _this._ability).can.apply(_a, args);
};
this.cannot = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return !_this.can.apply(_this, args);
};
this._disposeAbilitySubscription = this._ability.on('updated', function (event) {
_this._rules.set(event.rules);
});
}
AbilityServiceSignal.prototype.ngOnDestroy = function () {
this._disposeAbilitySubscription();
};
AbilityServiceSignal.prototype.update = function (rules) {
this._ability.update(rules);
};
AbilityServiceSignal.ɵfac = function AbilityServiceSignal_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AbilityServiceSignal)(); };
AbilityServiceSignal.ɵprov = /*@__PURE__*/ i0__namespace.ɵɵdefineInjectable({ token: AbilityServiceSignal, factory: AbilityServiceSignal.ɵfac, providedIn: 'root' });
return AbilityServiceSignal;
}());
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0__namespace.ɵsetClassMetadata(AbilityServiceSignal, [{
type: i0.Injectable,
args: [{ providedIn: 'root' }]
}], function () { return []; }, null); })();
exports.AbilityService = AbilityService;
exports.AbilityServiceSignal = AbilityServiceSignal;
exports.AblePipe = AblePipe;
exports.AblePurePipe = AblePurePipe;
}));
//# sourceMappingURL=index.js.map