@casl/react
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -1,2 +0,2 @@ | ||
import t,{PureComponent as n,createElement as r}from"react";function i(t,n){for(var r=0;r<n.length;r++){var i=n[r];i.enumerable=i.enumerable||false;i.configurable=true;if("value"in i)i.writable=true;Object.defineProperty(t,i.key,i)}}function u(t,n,r){if(n)i(t.prototype,n);if(r)i(t,r);return t}function e(){e=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var i in r)if(Object.prototype.hasOwnProperty.call(r,i))t[i]=r[i]}return t};return e.apply(this,arguments)}function o(t,n){t.prototype=Object.create(n.prototype);t.prototype.constructor=t;f(t,n)}function f(t,n){f=Object.setPrototypeOf||function t(n,r){n.__proto__=r;return n};return f(t,n)}var c=function t(){};var s=function(t){o(n,t);function n(){var n;for(var r=arguments.length,i=new Array(r),u=0;u<r;u++)i[u]=arguments[u];n=t.call.apply(t,[this].concat(i))||this;n.t=false;n.i=null;n.u=c;return n}var r=n.prototype;r.componentWillUnmount=function t(){this.u()};r.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};r.h=function t(){var n=this.props;var r=n.of||n.a||n.an||n.this||n.on;var i=n.not?"cannot":"can";return n.ability[i](n.I||n.do,r,n.field)};r.render=function t(){this.o(this.props.ability);this.t=this.h();return this.props.passThrough||this.t?this.v():null};r.v=function t(){var n=this.props,r=n.children,i=n.ability;var u="function"===typeof r?r(this.t,i):r;return u};u(n,[{key:"allowed",get:function t(){return this.t}}]);return n}(n);function a(t){var n,r;return r=n=function(t){o(n,t);function n(){return t.apply(this,arguments)||this}return n}(s),n.defaultProps={ability:t},r}function h(t){return function(n){return r(t,{children:function t(i){return r(s,e({ability:i},n))}})}}function useAbility(n){if("production"!==process.env.NODE_ENV&&"function"!==typeof t.useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");var r=t.useContext(n);var i=t.useState(),u=i[0],e=i[1];t.useEffect((function(){return r.on("updated",(function(t){if(t.rules!==u)e(t.rules)}))}),[]);return r}export{s as Can,a as createCanBoundTo,h as createContextualCan,useAbility}; | ||
import t,{PureComponent as n,createElement as r}from"react";function i(t,n){for(var r=0;r<n.length;r++){var i=n[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,a(i.key),i)}}function u(t,n,r){return n&&i(t.prototype,n),r&&i(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function e(){return e=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var i in r)({}).hasOwnProperty.call(r,i)&&(t[i]=r[i])}return t},e.apply(null,arguments)}function o(t,n){t.prototype=Object.create(n.prototype),t.prototype.constructor=t,f(t,n)}function f(t,n){return f=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,n){return t.__proto__=n,t},f(t,n)}function c(t,n){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var i=r.call(t,n||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(t)}function a(t){var n=c(t,"string");return"symbol"==typeof n?n:n+""}var s=function t(){};var v=function(t){function n(){var n;for(var r=arguments.length,i=new Array(r),u=0;u<r;u++)i[u]=arguments[u];n=t.call.apply(t,[this].concat(i))||this;n.t=false;n.i=null;n.u=s;return n}o(n,t);var r=n.prototype;r.componentWillUnmount=function t(){this.u()};r.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};r.v=function t(){var n=this.props;var r=n.of||n.a||n.an||n.this||n.on;var i=n.not?"cannot":"can";return n.ability[i](n.I||n.do,r,n.field)};r.render=function t(){this.o(this.props.ability);this.t=this.v();return this.props.passThrough||this.t?this.h():null};r.h=function t(){var n=this.props,r=n.children,i=n.ability;var u=typeof r==="function"?r(this.t,i):r;return u};return u(n,[{key:"allowed",get:function t(){return this.t}}])}(n);function h(t){var n;return n=function(t){function n(){return t.apply(this,arguments)||this}o(n,t);return n}(v),n.defaultProps={ability:t},n}function l(t){return function(n){return r(t,{children:function t(i){return r(v,e({ability:i},n))}})}}function useAbility(n){if(process.env.NODE_ENV!=="production"&&typeof t.useContext!=="function")throw new Error("You must use React >= 16.8 in order to use useAbility()");var r=t.useContext(n);var i=t.useState(),u=i[0],e=i[1];t.useEffect((function(){return r.on("updated",(function(t){if(t.rules!==u)e(t.rules)}))}),[]);return r}export{v as Can,h as createCanBoundTo,l as createContextualCan,useAbility}; | ||
//# sourceMappingURL=index.js.map |
import { PureComponent, ReactNode } from 'react'; | ||
import { AbilityTuple, SubjectType, AnyAbility, Generics, Abilities, IfString } from '@casl/ability'; | ||
declare type AbilityCanProps<T extends Abilities, Else = IfString<T, { | ||
type AbilityCanProps<T extends Abilities, Else = IfString<T, { | ||
do: T; | ||
@@ -36,4 +36,4 @@ } | { | ||
} | ||
export declare type CanProps<T extends AnyAbility> = AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>; | ||
export declare type BoundCanProps<T extends AnyAbility> = AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>; | ||
export type CanProps<T extends AnyAbility> = AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>; | ||
export type BoundCanProps<T extends AnyAbility> = AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>; | ||
export declare class Can<T extends AnyAbility, IsBound extends boolean = false> extends PureComponent<IsBound extends true ? BoundCanProps<T> : CanProps<T>> { | ||
@@ -40,0 +40,0 @@ private _isAllowed; |
@@ -1,2 +0,2 @@ | ||
(function(t,n){"object"===typeof exports&&"undefined"!==typeof module?n(exports,require("react")):"function"===typeof define&&define.amd?define(["exports","react"],n):(t="undefined"!==typeof globalThis?globalThis:t||self,n((t.casl=t.casl||{},t.casl.react={}),t.React))})(this,(function(t,n){"use strict";function i(t){return t&&"object"===typeof t&&"default"in t?t:{default:t}}var r=i(n);function e(t,n){for(var i=0;i<n.length;i++){var r=n[i];r.enumerable=r.enumerable||false;r.configurable=true;if("value"in r)r.writable=true;Object.defineProperty(t,r.key,r)}}function u(t,n,i){if(n)e(t.prototype,n);if(i)e(t,i);return t}function o(){o=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var i=arguments[n];for(var r in i)if(Object.prototype.hasOwnProperty.call(i,r))t[r]=i[r]}return t};return o.apply(this,arguments)}function f(t,n){t.prototype=Object.create(n.prototype);t.prototype.constructor=t;c(t,n)}function c(t,n){c=Object.setPrototypeOf||function t(n,i){n.__proto__=i;return n};return c(t,n)}var s=function t(){};var a=function(t){f(n,t);function n(){var n;for(var i=arguments.length,r=new Array(i),e=0;e<i;e++)r[e]=arguments[e];n=t.call.apply(t,[this].concat(r))||this;n.t=false;n.i=null;n.u=s;return n}var i=n.prototype;i.componentWillUnmount=function t(){this.u()};i.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};i.h=function t(){var n=this.props;var i=n.of||n.a||n.an||n.this||n.on;var r=n.not?"cannot":"can";return n.ability[r](n.I||n.do,i,n.field)};i.render=function t(){this.o(this.props.ability);this.t=this.h();return this.props.passThrough||this.t?this.l():null};i.l=function t(){var n=this.props,i=n.children,r=n.ability;var e="function"===typeof i?i(this.t,r):i;return e};u(n,[{key:"allowed",get:function t(){return this.t}}]);return n}(n.PureComponent);function h(t){var n,i;return i=n=function(t){f(n,t);function n(){return t.apply(this,arguments)||this}return n}(a),n.defaultProps={ability:t},i}function l(t){return function(i){return n.createElement(t,{children:function t(r){return n.createElement(a,o({ability:r},i))}})}}function useAbility(t){if("production"!==process.env.NODE_ENV&&"function"!==typeof r["default"].useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");var n=r["default"].useContext(t);var i=r["default"].useState(),e=i[0],u=i[1];r["default"].useEffect((function(){return n.on("updated",(function(t){if(t.rules!==e)u(t.rules)}))}),[]);return n}t.Can=a;t.createCanBoundTo=h;t.createContextualCan=l;t.useAbility=useAbility;Object.defineProperty(t,"__esModule",{value:true})})); | ||
(function(t,n){typeof exports==="object"&&typeof module!=="undefined"?n(exports,require("react")):typeof define==="function"&&define.amd?define(["exports","react"],n):(t=typeof globalThis!=="undefined"?globalThis:t||self,n((t.casl=t.casl||{},t.casl.react={}),t.React))})(this,(function(t,n){"use strict";function r(t,n){for(var r=0;r<n.length;r++){var i=n[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,c(i.key),i)}}function i(t,n,i){return n&&r(t.prototype,n),i&&r(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function e(){return e=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var i in r)({}).hasOwnProperty.call(r,i)&&(t[i]=r[i])}return t},e.apply(null,arguments)}function u(t,n){t.prototype=Object.create(n.prototype),t.prototype.constructor=t,o(t,n)}function o(t,n){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,n){return t.__proto__=n,t},o(t,n)}function f(t,n){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var i=r.call(t,n||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(t)}function c(t){var n=f(t,"string");return"symbol"==typeof n?n:n+""}var s=function t(){};var a=function(t){function n(){var n;for(var r=arguments.length,i=new Array(r),e=0;e<r;e++)i[e]=arguments[e];n=t.call.apply(t,[this].concat(i))||this;n.t=false;n.i=null;n.u=s;return n}u(n,t);var r=n.prototype;r.componentWillUnmount=function t(){this.u()};r.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};r.l=function t(){var n=this.props;var r=n.of||n.a||n.an||n.this||n.on;var i=n.not?"cannot":"can";return n.ability[i](n.I||n.do,r,n.field)};r.render=function t(){this.o(this.props.ability);this.t=this.l();return this.props.passThrough||this.t?this.h():null};r.h=function t(){var n=this.props,r=n.children,i=n.ability;var e=typeof r==="function"?r(this.t,i):r;return e};return i(n,[{key:"allowed",get:function t(){return this.t}}])}(n.PureComponent);function l(t){var n;return n=function(t){function n(){return t.apply(this,arguments)||this}u(n,t);return n}(a),n.defaultProps={ability:t},n}function h(t){return function(r){return n.createElement(t,{children:function t(i){return n.createElement(a,e({ability:i},r))}})}}function useAbility(t){if(process.env.NODE_ENV!=="production"&&typeof n.useContext!=="function")throw new Error("You must use React >= 16.8 in order to use useAbility()");var r=n.useContext(t);var i=n.useState(),e=i[0],u=i[1];n.useEffect((function(){return r.on("updated",(function(t){if(t.rules!==e)u(t.rules)}))}),[]);return r}t.Can=a;t.createCanBoundTo=l;t.createContextualCan=h;t.useAbility=useAbility})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@casl/react", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "React component for CASL which makes it easy to add permissions in any React application", | ||
@@ -51,4 +51,4 @@ "main": "dist/umd/index.js", | ||
"@testing-library/react-hooks": "^4.0.1", | ||
"@types/jest": "^28.0.0", | ||
"@types/node": "^14.0.5", | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^20.0.0", | ||
"@types/react": "^18.0.0", | ||
@@ -55,0 +55,0 @@ "chai": "^4.1.0", |
@@ -117,3 +117,3 @@ # CASL React | ||
> See [CASL guide](https://casl.js.org/v5/en/guide/intro) to learn how to define `Ability` instance. | ||
> See [CASL guide](https://casl.js.org/v6/en/guide/intro) to learn how to define `Ability` instance. | ||
@@ -209,4 +209,12 @@ and use our `Can` component: | ||
If you use TypeScript and React < 16.4 make sure to add `@casl/react/contextAPIPatch.d.ts` file in your `tscofig.json`, otherwise your app won't compile: | ||
If you use TypeScript and React < 16.4 make sure to create a file `contextAPIPatch.d.ts` file with the next content: | ||
```ts | ||
declare module 'react' { | ||
export type Consumer<T> = any; | ||
} | ||
``` | ||
and include it in your `tscofig.json`, otherwise your app won't compile: | ||
```json | ||
@@ -217,3 +225,3 @@ { | ||
"src/**/*", | ||
"@casl/react/contextAPIPatch.d.ts" // <-- add this line | ||
"./contextAPIPatch.d.ts" // <-- add this line | ||
] | ||
@@ -308,4 +316,13 @@ } | ||
> See [Define rules](https://casl.js.org/v5/en/guide/define-rules) to get more information of how to define `Ability` | ||
> See [Define rules](https://casl.js.org/v6/en/guide/define-rules) to get more information of how to define `Ability` | ||
## `useAbility` usage within hooks | ||
Using the return value `ability` of `const ability = useAbility(AbilityContext)` within a hook dependencies won't trigger a rerender when the rules are updated. You have to specify `ability.rules`: | ||
```jsx | ||
const posts = React.useMemo(() => getPosts(ability), [ability.rules]); | ||
// ✅ calling ability.update will update the list of posts | ||
``` | ||
## Want to help? | ||
@@ -317,3 +334,3 @@ | ||
> See [Support CASL](https://casl.js.org/v5/en/support-casljs) for details | ||
> See [Support CASL](https://casl.js.org/v6/en/support-casljs) for details | ||
@@ -320,0 +337,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
43230
339
15
95
1