@matir-js/react 0.1.1 → 0.1.3

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.
@@ -0,0 +1,61 @@
1
+ import { MatirCore, matir, matir as matir$1 } from "@matir-js/core";
2
+ import * as react_jsx_runtime0 from "react/jsx-runtime";
3
+
4
+ //#region src/matir-context.d.ts
5
+ interface MatirRegister {}
6
+ type Schema = ReturnType<typeof matir$1.defineSchema>;
7
+ type RegisteredSchema = MatirRegister extends {
8
+ schema: infer S;
9
+ } ? S extends Schema ? S : Schema : Schema;
10
+ type MatirCurrentInput = {
11
+ role?: string;
12
+ permissions?: Record<string, string[]>;
13
+ };
14
+ type RegisteredAbility = ReturnType<typeof MatirCore.createSchema<RegisteredSchema["roles"], RegisteredSchema["actions"], RegisteredSchema["rules"]>>["ability"];
15
+ type CurrentRole = {
16
+ value: string | null;
17
+ description: string | null;
18
+ } | null;
19
+ declare function MatirProvider({
20
+ children,
21
+ schema,
22
+ current: initialCurrent
23
+ }: {
24
+ children: React.ReactNode;
25
+ schema: Schema;
26
+ current?: MatirCurrentInput;
27
+ }): react_jsx_runtime0.JSX.Element;
28
+ declare function useCurrent(): {
29
+ role: CurrentRole;
30
+ setRole: (role: string) => void;
31
+ setPermissions: (permissions: Record<string, string[]>) => void;
32
+ clearAll: () => void;
33
+ };
34
+ declare function useAbility(): RegisteredAbility;
35
+ //#endregion
36
+ //#region src/can.d.ts
37
+ type CanSubject = Parameters<RegisteredAbility["can"]>[0];
38
+ type CanAction<S extends CanSubject> = Parameters<RegisteredAbility["can"] extends ((subject: S, action: infer A, ...args: any[]) => boolean) ? (subject: S, action: A, ...args: any[]) => boolean : never>[1];
39
+ type CanCondition<S extends CanSubject, A extends CanAction<S>> = Parameters<RegisteredAbility["can"] extends ((subject: S, action: A, condition: infer C, ...args: any[]) => boolean) ? (subject: S, action: A, condition: C, ...args: any[]) => boolean : never>[2];
40
+ type CanRenderProp = (allowed: boolean) => React.ReactNode;
41
+ type CanBaseProps<S extends CanSubject, A extends CanAction<S>> = {
42
+ actions?: A;
43
+ subject: S;
44
+ condition?: CanCondition<S, A>;
45
+ context?: unknown;
46
+ };
47
+ type CanNormalProps<S extends CanSubject, A extends CanAction<S>> = CanBaseProps<S, A> & {
48
+ passThrough?: false;
49
+ children: React.ReactNode;
50
+ fallback?: React.ReactNode;
51
+ };
52
+ type CanPassThroughProps<S extends CanSubject, A extends CanAction<S>> = CanBaseProps<S, A> & {
53
+ passThrough: true;
54
+ children: CanRenderProp;
55
+ fallback?: never;
56
+ };
57
+ type CanProps<S extends CanSubject, A extends CanAction<S>> = CanNormalProps<S, A> | CanPassThroughProps<S, A>;
58
+ declare function Can<S extends CanSubject, A extends CanAction<S>>(props: CanProps<S, A>): react_jsx_runtime0.JSX.Element;
59
+ //#endregion
60
+ export { Can, CanRenderProp, MatirCurrentInput, MatirProvider, MatirRegister, RegisteredAbility, matir, useAbility, useCurrent };
61
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,14 +1,13 @@
1
- import { defineSchema, matir } from "@matir-js/core";
1
+ import { matir, matir as matir$1 } from "@matir-js/core";
2
2
  import { createContext, useCallback, useContext, useMemo, useReducer, useState } from "react";
3
3
  import { Fragment, jsx } from "react/jsx-runtime";
4
-
5
4
  //#region src/matir-context.tsx
6
5
  const MatirContext = createContext(null);
7
6
  function MatirProvider({ children, schema, current: initialCurrent }) {
8
7
  const [role, setRole] = useState(null);
9
8
  const [, forceUpdate] = useReducer((x) => x + 1, 0);
10
9
  const { ability, current } = useMemo(() => {
11
- const instance = matir.createSchema(schema);
10
+ const instance = matir$1.createSchema(schema);
12
11
  if (initialCurrent?.role) instance.current.role(initialCurrent.role);
13
12
  if (initialCurrent?.permissions) instance.current.permissions(initialCurrent.permissions);
14
13
  return instance;
@@ -51,7 +50,6 @@ function useAbility() {
51
50
  if (!ctx) throw new Error("useAbility must be used within <MatirProvider />");
52
51
  return ctx.ability;
53
52
  }
54
-
55
53
  //#endregion
56
54
  //#region src/can.tsx
57
55
  function Can(props) {
@@ -59,6 +57,7 @@ function Can(props) {
59
57
  if (props.passThrough) return /* @__PURE__ */ jsx(Fragment, { children: props.children(allowed) });
60
58
  return /* @__PURE__ */ jsx(Fragment, { children: allowed ? props.children : props.fallback ?? null });
61
59
  }
62
-
63
60
  //#endregion
64
- export { Can, MatirProvider, defineSchema, useAbility, useCurrent };
61
+ export { Can, MatirProvider, matir, useAbility, useCurrent };
62
+
63
+ //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@matir-js/react",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
7
7
  "files": [
8
- "dist",
8
+ "dist/index.mjs",
9
+ "dist/index.d.mts",
9
10
  "LICENSE",
10
11
  "package.json"
11
12
  ],
@@ -20,8 +21,6 @@
20
21
  "author": "andrefelipeschulle",
21
22
  "license": "MIT",
22
23
  "dependencies": {
23
- "react": "^19",
24
- "react-dom": "^19",
25
24
  "@matir-js/core": "0.6.0"
26
25
  },
27
26
  "peerDependencies": {