role-permission-engine 1.0.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/CHANGELOG.md +139 -0
- package/LICENSE +21 -0
- package/README.md +682 -0
- package/dist/index.cjs.js +2558 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +2548 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/utils.cjs.js +389 -0
- package/dist/utils.cjs.js.map +1 -0
- package/dist/utils.esm.js +385 -0
- package/dist/utils.esm.js.map +1 -0
- package/package.json +106 -0
- package/types/index.d.ts +229 -0
- package/types/utils.d.ts +40 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `role-permission-engine` will be documented in this file.
|
|
4
|
+
|
|
5
|
+
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
6
|
+
and the [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## [Unreleased]
|
|
11
|
+
|
|
12
|
+
> Changes staged for the next release go here.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## [1.0.0] — 2026-06-05
|
|
17
|
+
|
|
18
|
+
### 🎉 Initial Release
|
|
19
|
+
|
|
20
|
+
First public release of `role-permission-engine` — a lightweight, flexible
|
|
21
|
+
role and permission-based access control library for React applications.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
|
|
27
|
+
#### Core Utilities (`src/utils/checkPermission.js`)
|
|
28
|
+
|
|
29
|
+
- **`hasRole(userRoles, requiredRoles, logic?)`**
|
|
30
|
+
- Pure, framework-agnostic role check.
|
|
31
|
+
- Supports `"any"` (OR, default) and `"all"` (AND) logic.
|
|
32
|
+
- Case-insensitive and whitespace-trimming comparisons.
|
|
33
|
+
- Returns `{ allowed: boolean, reason: string }`.
|
|
34
|
+
|
|
35
|
+
- **`hasPermission(userPermissions, requiredPermissions, logic?)`**
|
|
36
|
+
- Pure, framework-agnostic permission check.
|
|
37
|
+
- Full wildcard support: `"*"` (superuser) and `"read:*"` (namespace wildcard).
|
|
38
|
+
- Supports `"any"` (OR, default) and `"all"` (AND) logic.
|
|
39
|
+
|
|
40
|
+
- **`checkAccess(options)`**
|
|
41
|
+
- Combined role **and** permission check in a single call.
|
|
42
|
+
- Both constraints must pass for `allowed: true`.
|
|
43
|
+
- Empty constraint arrays are automatically satisfied (open access).
|
|
44
|
+
- Accepts `roleLogic` and `permissionLogic` independently.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
#### React Context (`src/context/PermissionContext.jsx`)
|
|
49
|
+
|
|
50
|
+
- **`<PermissionProvider>`**
|
|
51
|
+
- Provides `roles`, `permissions`, `user`, `isAuthenticated`, and `isLoading`
|
|
52
|
+
to the entire React subtree below it.
|
|
53
|
+
- Memoized — only re-renders children when props actually change.
|
|
54
|
+
- `isLoading` support prevents premature redirects during async auth fetches.
|
|
55
|
+
|
|
56
|
+
- **`usePermissionContext()`**
|
|
57
|
+
- Hook to read the raw context value (roles, permissions, user, etc.).
|
|
58
|
+
- Throws a clear error if called outside a `<PermissionProvider>`.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
#### React Hook (`src/hooks/usePermission.js`)
|
|
63
|
+
|
|
64
|
+
- **`usePermission(options?)`**
|
|
65
|
+
- Programmatic permission checking within React components.
|
|
66
|
+
- Returns `{ allowed, denied, isLoading, isAuthenticated, reason }`.
|
|
67
|
+
- `denied` is a convenience inverse of `allowed`.
|
|
68
|
+
- `allowed` is always `false` while `isLoading` is `true`.
|
|
69
|
+
- Memoized with `useMemo` — only re-evaluates when inputs change.
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
#### React Components
|
|
74
|
+
|
|
75
|
+
- **`<BlockRoute>`** (`src/components/BlockRoute.jsx`)
|
|
76
|
+
- Route guard component — redirects unauthorized users.
|
|
77
|
+
- Props: `roles`, `permissions`, `roleLogic`, `permissionLogic`,
|
|
78
|
+
`redirectTo` (default: `"/unauthorized"`), `loadingComponent`,
|
|
79
|
+
`replace` (default: `true`), `state`.
|
|
80
|
+
- Auto-detects React Router version at runtime:
|
|
81
|
+
- v6: uses `<Navigate>`.
|
|
82
|
+
- v5: uses `<Redirect>`.
|
|
83
|
+
- Shows `loadingComponent` while `isLoading` is `true`.
|
|
84
|
+
|
|
85
|
+
- **`<PermissionGate>`** (`src/components/PermissionGate.jsx`)
|
|
86
|
+
- Inline conditional rendering — never redirects.
|
|
87
|
+
- Props: `roles`, `permissions`, `roleLogic`, `permissionLogic`,
|
|
88
|
+
`fallback` (default: `null`), `loadingComponent` (default: `null`),
|
|
89
|
+
`negate` (default: `false`).
|
|
90
|
+
- `negate` inverts the check — renders `children` when access is **denied**.
|
|
91
|
+
Ideal for guest-only UI (login buttons, upgrade banners).
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
#### TypeScript Declarations (`types/index.d.ts`)
|
|
96
|
+
|
|
97
|
+
- Full TypeScript types for all public exports:
|
|
98
|
+
`Role`, `Permission`, `LogicOperator`, `PermissionResult`,
|
|
99
|
+
`CheckAccessOptions`, `PermissionContextValue`, `PermissionProviderProps`,
|
|
100
|
+
`UsePermissionOptions`, `UsePermissionResult`, `BlockRouteProps`,
|
|
101
|
+
`PermissionGateProps`.
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
#### Build & Tooling
|
|
106
|
+
|
|
107
|
+
- **Dual-format output** via Rollup:
|
|
108
|
+
- `dist/index.cjs.js` — CommonJS (Node.js / `require()`)
|
|
109
|
+
- `dist/index.esm.js` — ES Module (`import`)
|
|
110
|
+
- **`prepublishOnly`** script: runs `build` then `test` before every `npm publish`.
|
|
111
|
+
- **JSDoc** config (`jsdoc.json`) for generating HTML documentation: `npm run docs`.
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
#### Tests
|
|
116
|
+
|
|
117
|
+
- **47 unit and integration tests** across 3 test suites (Jest + React Testing Library):
|
|
118
|
+
- `checkPermission.test.js` — 27 tests for all utility functions.
|
|
119
|
+
- `PermissionGate.test.jsx` — 9 tests for inline gate component.
|
|
120
|
+
- `BlockRoute.test.jsx` — 11 tests for route guard component.
|
|
121
|
+
- 100% test pass rate on initial release.
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
### Package Info
|
|
126
|
+
|
|
127
|
+
| Field | Value |
|
|
128
|
+
|-------------|--------------------------------------------------------------|
|
|
129
|
+
| Name | `role-permission-engine` |
|
|
130
|
+
| Version | `1.0.0` |
|
|
131
|
+
| License | MIT |
|
|
132
|
+
| Author | Darshan Raghvani |
|
|
133
|
+
| Peer deps | `react >=16.8`, `react-dom >=16.8`, `react-router-dom >=5` |
|
|
134
|
+
| Node target | ES2019+ |
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
[Unreleased]: https://github.com/DarshanR1406/role-permission-engine/compare/v1.0.0...HEAD
|
|
139
|
+
[1.0.0]: https://github.com/DarshanR1406/role-permission-engine/releases/tag/v1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Darshan Raghvani
|
|
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.
|