@lowdefy/operators-js 5.0.0 → 5.1.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.
@@ -12,8 +12,70 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ import { getFromObject } from '@lowdefy/operators';
16
- function _user({ arrayIndices, location, params, user }) {
15
+ */ import { type } from '@lowdefy/helpers';
16
+ import { getFromObject } from '@lowdefy/operators';
17
+ const roleMethods = [
18
+ 'hasRole',
19
+ 'hasSomeRoles',
20
+ 'hasAllRoles'
21
+ ];
22
+ function getUserRoles({ user, methodName }) {
23
+ const roles = user?.roles;
24
+ if (type.isNone(roles)) return [];
25
+ if (!type.isArray(roles)) {
26
+ throw new Error(`_user.${methodName} expects "user.roles" to be an array of strings. Received ${JSON.stringify(roles)}.`);
27
+ }
28
+ return roles;
29
+ }
30
+ function validateRoleString({ params, methodName }) {
31
+ if (!type.isString(params)) {
32
+ throw new Error(`_user.${methodName} accepts a string. Received ${JSON.stringify(params)}.`);
33
+ }
34
+ return params;
35
+ }
36
+ function validateRoleArray({ params, methodName }) {
37
+ if (!type.isArray(params) || !params.every((role)=>type.isString(role))) {
38
+ throw new Error(`_user.${methodName} accepts an array of strings. Received ${JSON.stringify(params)}.`);
39
+ }
40
+ return params;
41
+ }
42
+ function _user({ arrayIndices, location, methodName, params, user }) {
43
+ if (methodName === 'hasRole') {
44
+ const role = validateRoleString({
45
+ params,
46
+ methodName
47
+ });
48
+ const userRoles = getUserRoles({
49
+ user,
50
+ methodName
51
+ });
52
+ return userRoles.includes(role);
53
+ }
54
+ if (methodName === 'hasSomeRoles') {
55
+ const required = validateRoleArray({
56
+ params,
57
+ methodName
58
+ });
59
+ const userRoles = getUserRoles({
60
+ user,
61
+ methodName
62
+ });
63
+ return required.some((role)=>userRoles.includes(role));
64
+ }
65
+ if (methodName === 'hasAllRoles') {
66
+ const required = validateRoleArray({
67
+ params,
68
+ methodName
69
+ });
70
+ const userRoles = getUserRoles({
71
+ user,
72
+ methodName
73
+ });
74
+ return required.every((role)=>userRoles.includes(role));
75
+ }
76
+ if (!type.isUndefined(methodName)) {
77
+ throw new Error(`_user.${methodName} is not supported, use one of the following: ${roleMethods.join(', ')}.`);
78
+ }
17
79
  return getFromObject({
18
80
  arrayIndices,
19
81
  location,
@@ -18,7 +18,7 @@
18
18
  oneOf: [
19
19
  {
20
20
  type: 'string',
21
- description: 'Dot-notation path to value in user object.'
21
+ description: 'Dot-notation path to value in user object, or a role name when used with a role method.'
22
22
  },
23
23
  {
24
24
  type: 'integer',
@@ -31,6 +31,13 @@
31
31
  ],
32
32
  description: 'Return all user data.'
33
33
  },
34
+ {
35
+ type: 'array',
36
+ items: {
37
+ type: 'string'
38
+ },
39
+ description: 'Array of role names when used with a role method.'
40
+ },
34
41
  {
35
42
  type: 'object',
36
43
  properties: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/operators-js",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -43,12 +43,12 @@
43
43
  "dist/*"
44
44
  ],
45
45
  "dependencies": {
46
- "@lowdefy/helpers": "5.0.0",
47
- "@lowdefy/operators": "5.0.0"
46
+ "@lowdefy/helpers": "5.1.0",
47
+ "@lowdefy/operators": "5.1.0"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@jest/globals": "28.1.3",
51
- "@lowdefy/node-utils": "5.0.0",
51
+ "@lowdefy/node-utils": "5.1.0",
52
52
  "@swc/cli": "0.8.0",
53
53
  "@swc/core": "1.15.18",
54
54
  "@swc/jest": "0.2.39",