UNPKG

@casl/vue

Version:

Vue plugin for CASL which makes it easy to add permissions in any Vue application

73 lines (64 loc) 2.39 kB
import { defineComponent as n, inject as t, provide as o, ref as i } from "vue"; import { Ability as r } from "@casl/ability"; const e = Object.hasOwn || ((n, t) => Object.prototype.hasOwnProperty.call(n, t)); function s(n) { if (e(n, "possibleRulesFor")) return n; const t = i(!0); n.on("updated", () => { t.value = !t.value; }); const o = n.possibleRulesFor.bind(n); return n.possibleRulesFor = (n, i) => (t.value = t.value, o(n, i)), n.can = n.can.bind(n), n.cannot = n.cannot.bind(n), n; } const a = Symbol("ability"); function c() { const n = t(a); if (!n) throw new Error("Cannot inject Ability instance because it was not provided. Did you forget to install abilitiesPlugin or call provideAbility in a parent component?"); return n; } function l(n) { o(a, s(n)); } const u = n({ name: "Can", props: { I: String, do: String, a: [ String, Function ], an: [ String, Function ], this: [ String, Function, Object ], on: [ String, Function, Object ], not: Boolean, passThrough: Boolean, field: String }, setup(n, {slots: t}) { const o = n; let i = "do", r = "on"; if (void 0 === o[i] && (i = "I", r = function(n) { if (void 0 !== n.a) return "a"; if (void 0 !== n.this) return "this"; if (void 0 !== n.an) return "an"; return ""; }(n)), !o[i]) throw new Error("Neither `I` nor `do` prop was passed in <Can>"); if (!t.default) throw new Error("Expects to receive default slot"); const e = c(); return () => { const s = e.relevantRuleFor(o[i], o[r], o.field); let a = !!s && !s.inverted; return n.not && (a = !a), n.passThrough || a ? t.default({ allowed: a, reason: s?.reason, ability: e }) : null; }; } }); function f(n, t, o) { if (!(t && t instanceof r)) throw new Error("Please, provide an Ability instance to abilitiesPlugin plugin"); n.provide(a, s(t)), o && o.useGlobalProperties && (n.config.globalProperties.$ability = t, n.config.globalProperties.$can = t.can.bind(t)); } export { a as ABILITY_TOKEN, u as Can, f as abilitiesPlugin, l as provideAbility, c as useAbility }; //# sourceMappingURL=index.mjs.map