@matir-js/react 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { MatirCore, matir, matir as matir$1 } from "@matir-js/core";
1
+ import { ExtractActionsFromSubject, ExtractPermissionsByWildcard, ExtractSubjects, ExtractWildcardSubjects, InferPermissions, MatirCore, matir, matir as matir$1 } from "@matir-js/core";
2
2
  import * as react_jsx_runtime0 from "react/jsx-runtime";
3
3
 
4
4
  //#region src/matir-context.d.ts
@@ -7,6 +7,8 @@ type Schema = ReturnType<typeof matir$1.defineSchema>;
7
7
  type RegisteredSchema = MatirRegister extends {
8
8
  schema: infer S;
9
9
  } ? S extends Schema ? S : Schema : Schema;
10
+ type RegisteredRules = RegisteredSchema["rules"];
11
+ type RegisteredActions = RegisteredSchema["actions"];
10
12
  type MatirCurrentInput = {
11
13
  role?: string;
12
14
  permissions?: Record<string, string[]>;
@@ -16,6 +18,9 @@ type CurrentRole = {
16
18
  value: string | null;
17
19
  description: string | null;
18
20
  } | null;
21
+ type CurrentPermissions = MatirRegister extends {
22
+ schema: infer S;
23
+ } ? S extends Schema ? InferPermissions<S> : Record<string, string[]> : Record<string, string[]>;
19
24
  declare function MatirProvider({
20
25
  children,
21
26
  schema,
@@ -27,9 +32,11 @@ declare function MatirProvider({
27
32
  }): react_jsx_runtime0.JSX.Element;
28
33
  declare function useCurrent(): {
29
34
  role: CurrentRole;
30
- permissions: Record<string, string[]> | null;
35
+ permissions: CurrentPermissions | null;
31
36
  setRole: (role: string) => void;
32
37
  setPermissions: (permissions: Record<string, string[]>) => void;
38
+ getPermission: <S extends ExtractSubjects<RegisteredRules>>(subject: S) => ExtractActionsFromSubject<RegisteredRules, S, RegisteredActions>[] | null;
39
+ getPermissions: <P extends ExtractWildcardSubjects<RegisteredRules>>(pattern: P) => ExtractPermissionsByWildcard<RegisteredRules, RegisteredActions, P>;
33
40
  clearAll: () => void;
34
41
  };
35
42
  declare function useAbility(): RegisteredAbility;
package/dist/index.mjs CHANGED
@@ -22,6 +22,12 @@ function MatirProvider({ children, schema, current: initialCurrent }) {
22
22
  setPermissions(permissions);
23
23
  forceUpdate();
24
24
  }, [current]);
25
+ const getCurrentPermission = useCallback((subject) => {
26
+ return current.getPermission(subject);
27
+ }, [current]);
28
+ const getCurrentPermissions = useCallback((pattern) => {
29
+ return current.getPermissions(pattern);
30
+ }, [current]);
25
31
  const clearAll = useCallback(() => {
26
32
  current.clear();
27
33
  forceUpdate();
@@ -33,6 +39,8 @@ function MatirProvider({ children, schema, current: initialCurrent }) {
33
39
  ability,
34
40
  setCurrentRole,
35
41
  setCurrentPermissions,
42
+ getCurrentPermission,
43
+ getCurrentPermissions,
36
44
  clearAll
37
45
  },
38
46
  children
@@ -46,6 +54,8 @@ function useCurrent() {
46
54
  permissions: ctx.permissions,
47
55
  setRole: ctx.setCurrentRole,
48
56
  setPermissions: ctx.setCurrentPermissions,
57
+ getPermission: ctx.getCurrentPermission,
58
+ getPermissions: ctx.getCurrentPermissions,
49
59
  clearAll: ctx.clearAll
50
60
  };
51
61
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matir-js/react",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -21,7 +21,7 @@
21
21
  "author": "andrefelipeschulle",
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@matir-js/core": "0.7.0"
24
+ "@matir-js/core": "0.8.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "react": "^19",