@zanzojs/react 0.1.0-beta.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Gonzalo Jeria
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ZanzoContext: () => ZanzoContext,
24
+ ZanzoProvider: () => ZanzoProvider,
25
+ useZanzo: () => useZanzo
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/ZanzoProvider.tsx
30
+ var import_react = require("react");
31
+ var import_core = require("@zanzojs/core");
32
+ var import_jsx_runtime = require("react/jsx-runtime");
33
+ var ZanzoContext = (0, import_react.createContext)(null);
34
+ var ZanzoProvider = ({ snapshot, children }) => {
35
+ const client = (0, import_react.useMemo)(() => new import_core.ZanzoClient(snapshot), [snapshot]);
36
+ const value = (0, import_react.useMemo)(() => ({
37
+ can: (action, resource) => client.can(action, resource)
38
+ }), [client]);
39
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ZanzoContext.Provider, { value, children });
40
+ };
41
+
42
+ // src/useZanzo.ts
43
+ var import_react2 = require("react");
44
+ function useZanzo() {
45
+ const context = (0, import_react2.useContext)(ZanzoContext);
46
+ if (!context) {
47
+ throw new Error("useZanzo must be used within a ZanzoProvider");
48
+ }
49
+ return context;
50
+ }
51
+ // Annotate the CommonJS export names for ESM import in node:
52
+ 0 && (module.exports = {
53
+ ZanzoContext,
54
+ ZanzoProvider,
55
+ useZanzo
56
+ });
57
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/ZanzoProvider.tsx","../src/useZanzo.ts"],"sourcesContent":["export * from './ZanzoProvider.js';\nexport * from './useZanzo.js';\n","import React, { createContext, useMemo, ReactNode } from 'react';\nimport { ZanzoClient } from '@zanzojs/core';\n\nexport interface ZanzoContextValue {\n can: (action: string, resource: string) => boolean;\n}\n\nexport const ZanzoContext = createContext<ZanzoContextValue | null>(null);\n\nexport interface ZanzoProviderProps {\n /**\n * The flat JSON snapshot object provided by the server-side ZanzoCompiler.\n * e.g. `{ \"Project:1\": [\"read\", \"write\"] }`\n */\n snapshot: Record<string, string[]>;\n children: ReactNode;\n}\n\n/**\n * Injects the Zanzo ReBAC evaluation context into the React Component Tree.\n * It builds an optimized O(1) in-memory client using the provided snapshot.\n */\nexport const ZanzoProvider: React.FC<ZanzoProviderProps> = ({ snapshot, children }) => {\n\n // Instance the lightweight client only once per snapshot reference\n const client = useMemo(() => new ZanzoClient(snapshot), [snapshot]);\n\n // Context value exposed to hooks\n const value = useMemo<ZanzoContextValue>(() => ({\n can: (action: string, resource: string) => client.can(action, resource)\n }), [client]);\n\n return (\n <ZanzoContext.Provider value={value}>\n {children}\n </ZanzoContext.Provider>\n );\n};\n","import { useContext } from 'react';\nimport { ZanzoContext, ZanzoContextValue } from './ZanzoProvider.js';\n\n/**\n * Consumes the Zanzo ReBAC context to evaluate permissions synchronously.\n * Must be used within a `<ZanzoProvider>` boundary.\n * \n * @example\n * ```tsx\n * const { can } = useZanzo();\n * \n * if (!can('read', 'Invoice:123')) {\n * return <AccessDenied />;\n * }\n * ```\n */\nexport function useZanzo(): ZanzoContextValue {\n const context = useContext(ZanzoContext);\n \n if (!context) {\n throw new Error('useZanzo must be used within a ZanzoProvider');\n }\n\n return context;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAyD;AACzD,kBAA4B;AAgCpB;AA1BD,IAAM,mBAAe,4BAAwC,IAAI;AAejE,IAAM,gBAA8C,CAAC,EAAE,UAAU,SAAS,MAAM;AAGnF,QAAM,aAAS,sBAAQ,MAAM,IAAI,wBAAY,QAAQ,GAAG,CAAC,QAAQ,CAAC;AAGlE,QAAM,YAAQ,sBAA2B,OAAO;AAAA,IAC5C,KAAK,CAAC,QAAgB,aAAqB,OAAO,IAAI,QAAQ,QAAQ;AAAA,EAC1E,IAAI,CAAC,MAAM,CAAC;AAEZ,SACI,4CAAC,aAAa,UAAb,EAAsB,OAClB,UACL;AAER;;;ACrCA,IAAAA,gBAA2B;AAgBpB,SAAS,WAA8B;AAC5C,QAAM,cAAU,0BAAW,YAAY;AAEvC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,8CAA8C;AAAA,EAChE;AAEA,SAAO;AACT;","names":["import_react"]}
@@ -0,0 +1,36 @@
1
+ import React, { ReactNode } from 'react';
2
+
3
+ interface ZanzoContextValue {
4
+ can: (action: string, resource: string) => boolean;
5
+ }
6
+ declare const ZanzoContext: React.Context<ZanzoContextValue | null>;
7
+ interface ZanzoProviderProps {
8
+ /**
9
+ * The flat JSON snapshot object provided by the server-side ZanzoCompiler.
10
+ * e.g. `{ "Project:1": ["read", "write"] }`
11
+ */
12
+ snapshot: Record<string, string[]>;
13
+ children: ReactNode;
14
+ }
15
+ /**
16
+ * Injects the Zanzo ReBAC evaluation context into the React Component Tree.
17
+ * It builds an optimized O(1) in-memory client using the provided snapshot.
18
+ */
19
+ declare const ZanzoProvider: React.FC<ZanzoProviderProps>;
20
+
21
+ /**
22
+ * Consumes the Zanzo ReBAC context to evaluate permissions synchronously.
23
+ * Must be used within a `<ZanzoProvider>` boundary.
24
+ *
25
+ * @example
26
+ * ```tsx
27
+ * const { can } = useZanzo();
28
+ *
29
+ * if (!can('read', 'Invoice:123')) {
30
+ * return <AccessDenied />;
31
+ * }
32
+ * ```
33
+ */
34
+ declare function useZanzo(): ZanzoContextValue;
35
+
36
+ export { ZanzoContext, type ZanzoContextValue, ZanzoProvider, type ZanzoProviderProps, useZanzo };
@@ -0,0 +1,36 @@
1
+ import React, { ReactNode } from 'react';
2
+
3
+ interface ZanzoContextValue {
4
+ can: (action: string, resource: string) => boolean;
5
+ }
6
+ declare const ZanzoContext: React.Context<ZanzoContextValue | null>;
7
+ interface ZanzoProviderProps {
8
+ /**
9
+ * The flat JSON snapshot object provided by the server-side ZanzoCompiler.
10
+ * e.g. `{ "Project:1": ["read", "write"] }`
11
+ */
12
+ snapshot: Record<string, string[]>;
13
+ children: ReactNode;
14
+ }
15
+ /**
16
+ * Injects the Zanzo ReBAC evaluation context into the React Component Tree.
17
+ * It builds an optimized O(1) in-memory client using the provided snapshot.
18
+ */
19
+ declare const ZanzoProvider: React.FC<ZanzoProviderProps>;
20
+
21
+ /**
22
+ * Consumes the Zanzo ReBAC context to evaluate permissions synchronously.
23
+ * Must be used within a `<ZanzoProvider>` boundary.
24
+ *
25
+ * @example
26
+ * ```tsx
27
+ * const { can } = useZanzo();
28
+ *
29
+ * if (!can('read', 'Invoice:123')) {
30
+ * return <AccessDenied />;
31
+ * }
32
+ * ```
33
+ */
34
+ declare function useZanzo(): ZanzoContextValue;
35
+
36
+ export { ZanzoContext, type ZanzoContextValue, ZanzoProvider, type ZanzoProviderProps, useZanzo };
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ // src/ZanzoProvider.tsx
2
+ import { createContext, useMemo } from "react";
3
+ import { ZanzoClient } from "@zanzojs/core";
4
+ import { jsx } from "react/jsx-runtime";
5
+ var ZanzoContext = createContext(null);
6
+ var ZanzoProvider = ({ snapshot, children }) => {
7
+ const client = useMemo(() => new ZanzoClient(snapshot), [snapshot]);
8
+ const value = useMemo(() => ({
9
+ can: (action, resource) => client.can(action, resource)
10
+ }), [client]);
11
+ return /* @__PURE__ */ jsx(ZanzoContext.Provider, { value, children });
12
+ };
13
+
14
+ // src/useZanzo.ts
15
+ import { useContext } from "react";
16
+ function useZanzo() {
17
+ const context = useContext(ZanzoContext);
18
+ if (!context) {
19
+ throw new Error("useZanzo must be used within a ZanzoProvider");
20
+ }
21
+ return context;
22
+ }
23
+ export {
24
+ ZanzoContext,
25
+ ZanzoProvider,
26
+ useZanzo
27
+ };
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/ZanzoProvider.tsx","../src/useZanzo.ts"],"sourcesContent":["import React, { createContext, useMemo, ReactNode } from 'react';\nimport { ZanzoClient } from '@zanzojs/core';\n\nexport interface ZanzoContextValue {\n can: (action: string, resource: string) => boolean;\n}\n\nexport const ZanzoContext = createContext<ZanzoContextValue | null>(null);\n\nexport interface ZanzoProviderProps {\n /**\n * The flat JSON snapshot object provided by the server-side ZanzoCompiler.\n * e.g. `{ \"Project:1\": [\"read\", \"write\"] }`\n */\n snapshot: Record<string, string[]>;\n children: ReactNode;\n}\n\n/**\n * Injects the Zanzo ReBAC evaluation context into the React Component Tree.\n * It builds an optimized O(1) in-memory client using the provided snapshot.\n */\nexport const ZanzoProvider: React.FC<ZanzoProviderProps> = ({ snapshot, children }) => {\n\n // Instance the lightweight client only once per snapshot reference\n const client = useMemo(() => new ZanzoClient(snapshot), [snapshot]);\n\n // Context value exposed to hooks\n const value = useMemo<ZanzoContextValue>(() => ({\n can: (action: string, resource: string) => client.can(action, resource)\n }), [client]);\n\n return (\n <ZanzoContext.Provider value={value}>\n {children}\n </ZanzoContext.Provider>\n );\n};\n","import { useContext } from 'react';\nimport { ZanzoContext, ZanzoContextValue } from './ZanzoProvider.js';\n\n/**\n * Consumes the Zanzo ReBAC context to evaluate permissions synchronously.\n * Must be used within a `<ZanzoProvider>` boundary.\n * \n * @example\n * ```tsx\n * const { can } = useZanzo();\n * \n * if (!can('read', 'Invoice:123')) {\n * return <AccessDenied />;\n * }\n * ```\n */\nexport function useZanzo(): ZanzoContextValue {\n const context = useContext(ZanzoContext);\n \n if (!context) {\n throw new Error('useZanzo must be used within a ZanzoProvider');\n }\n\n return context;\n}\n"],"mappings":";AAAA,SAAgB,eAAe,eAA0B;AACzD,SAAS,mBAAmB;AAgCpB;AA1BD,IAAM,eAAe,cAAwC,IAAI;AAejE,IAAM,gBAA8C,CAAC,EAAE,UAAU,SAAS,MAAM;AAGnF,QAAM,SAAS,QAAQ,MAAM,IAAI,YAAY,QAAQ,GAAG,CAAC,QAAQ,CAAC;AAGlE,QAAM,QAAQ,QAA2B,OAAO;AAAA,IAC5C,KAAK,CAAC,QAAgB,aAAqB,OAAO,IAAI,QAAQ,QAAQ;AAAA,EAC1E,IAAI,CAAC,MAAM,CAAC;AAEZ,SACI,oBAAC,aAAa,UAAb,EAAsB,OAClB,UACL;AAER;;;ACrCA,SAAS,kBAAkB;AAgBpB,SAAS,WAA8B;AAC5C,QAAM,UAAU,WAAW,YAAY;AAEvC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,8CAA8C;AAAA,EAChE;AAEA,SAAO;AACT;","names":[]}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@zanzojs/react",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "React bindings for Zanzo ReBAC. O(1) permission checks via ZanzoProvider and useZanzo hook.",
5
+ "keywords": [
6
+ "@zanzojs/core",
7
+ "react",
8
+ "rebac",
9
+ "authorization",
10
+ "permissions",
11
+ "hooks"
12
+ ],
13
+ "homepage": "https://github.com/GonzaloJeria/zanzo",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/GonzaloJeria/zanzo.git",
17
+ "directory": "packages/react"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Gonzalo Jeria",
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "import": "./dist/index.js",
25
+ "require": "./dist/index.cjs",
26
+ "types": "./dist/index.d.ts"
27
+ }
28
+ },
29
+ "main": "./dist/index.cjs",
30
+ "module": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "files": [
33
+ "dist",
34
+ "README.md",
35
+ "LICENSE"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "peerDependencies": {
41
+ "@zanzojs/core": "^0.1.0-beta.0",
42
+ "react": ">=18.0.0",
43
+ "react-dom": ">=18.0.0"
44
+ },
45
+ "devDependencies": {
46
+ "react": "^18.3.1",
47
+ "react-dom": "^18.3.1",
48
+ "@types/react": "^18.0.0",
49
+ "@types/react-dom": "^18.0.0",
50
+ "@testing-library/react": "^14.2.1",
51
+ "jsdom": "^24.0.0",
52
+ "tsup": "latest",
53
+ "typescript": "^5.7.2",
54
+ "vitest": "latest",
55
+ "@zanzojs/core": "0.1.0-beta.0"
56
+ },
57
+ "scripts": {
58
+ "build": "tsup",
59
+ "test": "vitest run",
60
+ "test:types": "vitest run --typecheck"
61
+ }
62
+ }