@r2d2bzh/moleculer-authz-helpers 2.0.1 → 2.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r2d2bzh/moleculer-authz-helpers",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Moleculer mixin to implement preflight runner",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -16,12 +16,12 @@
16
16
  "access": "public"
17
17
  },
18
18
  "devDependencies": {
19
- "@r2d2bzh/js-rules": "^0.7.1",
20
- "ava": "^3.15.0",
21
- "c8": "^7.10.0",
22
- "eslint": "^7.32.0",
23
- "moleculer": "^0.14.18",
24
- "uuid": "^8.3.2"
19
+ "@r2d2bzh/js-rules": "^1.0.3",
20
+ "ava": "^5.2.0",
21
+ "c8": "^7.12.0",
22
+ "eslint": "^8.34.0",
23
+ "moleculer": "^0.14.28",
24
+ "uuid": "^9.0.0"
25
25
  },
26
26
  "c8": {
27
27
  "check-coverage": true,
@@ -39,10 +39,10 @@
39
39
  "node": ">=12.0.0"
40
40
  },
41
41
  "peerDependencies": {
42
- "moleculer": "^0.14.0"
42
+ "moleculer": "^0.14.28"
43
43
  },
44
44
  "dependencies": {
45
- "@r2d2bzh/moleculer-event-callback": "~0.0.1",
45
+ "@r2d2bzh/moleculer-event-callback": "^1.0.0",
46
46
  "lodash": "^4.17.21"
47
47
  }
48
48
  }
@@ -36,13 +36,14 @@ export const unsafeAddPreflightMixin = (serviceSchema, { timeout }) => {
36
36
  };
37
37
 
38
38
  export default (serviceSchema, { timeout = 200 } = {}) => {
39
- Object.entries(serviceSchema.actions || {})
40
- .filter(([, actionSpecification]) => actionSpecification?.rest && actionSpecification.rest?.authorization !== false)
41
- .forEach(([actionName, actionSpecification]) => {
42
- if (!(actionSpecification?.preflight instanceof Function || actionSpecification?.preflight?.handler)) {
43
- throw new MoleculerError('missing preflight handler', 500, 'MISSING_PREFLIGHT', { actionName });
44
- }
45
- });
39
+ const authorizedActions = Object.entries(serviceSchema.actions || {}).filter(
40
+ ([, actionSpecification]) => actionSpecification?.rest && actionSpecification.rest?.authorization !== false
41
+ );
42
+ for (const [actionName, actionSpecification] of authorizedActions) {
43
+ if (!(actionSpecification?.preflight instanceof Function || actionSpecification?.preflight?.handler)) {
44
+ throw new MoleculerError('missing preflight handler', 500, 'MISSING_PREFLIGHT', { actionName });
45
+ }
46
+ }
46
47
 
47
48
  return unsafeAddPreflightMixin(serviceSchema, { timeout });
48
49
  };
@@ -1,13 +1,18 @@
1
1
  const removeUnanswered = (authzAnswers) => authzAnswers.filter((answer) => answer !== undefined);
2
2
 
3
- const reduceAnswers = (answers, reducer) =>
4
- answers.length
5
- ? answers.reduce(
6
- ([authorizationStatus, authorizationsCount], answer) =>
7
- reducer(answer, authorizationStatus, authorizationsCount),
8
- [true, 0]
9
- )
10
- : [false, 0];
3
+ const reduceAnswers = (answers, reducer) => {
4
+ if (answers.length > 0) {
5
+ let accumulator = [true, 0];
6
+
7
+ for (const answer of answers) {
8
+ const [authorizationStatus, authorizationsCount] = accumulator;
9
+ accumulator = reducer(answer, authorizationStatus, authorizationsCount);
10
+ }
11
+ return accumulator;
12
+ }
13
+
14
+ return [false, 0];
15
+ };
11
16
 
12
17
  const onMultipleAuthorizations =
13
18
  (handle) =>
@@ -34,17 +39,21 @@ const processAndCountAnswer = (warn) => {
34
39
  const doAnswersAuthorize = processAndCountManyAnswers(warn);
35
40
  return function (answer, authorizationStatus = true, authorizationsCount = 0) {
36
41
  switch (Object.prototype.toString.call(answer)) {
37
- case '[object Boolean]':
42
+ case '[object Boolean]': {
38
43
  return [authorizationStatus && answer, authorizationsCount + 1];
39
- case '[object Array]':
44
+ }
45
+ case '[object Array]': {
40
46
  // A new array means a new authorization request with its own
41
47
  // answers count that does not reflect on the current one
42
48
  return [doAnswersAuthorize(answer, authorizationStatus), authorizationsCount];
43
- case '[object Undefined]':
49
+ }
50
+ case '[object Undefined]': {
44
51
  return [false, authorizationsCount];
45
- default:
52
+ }
53
+ default: {
46
54
  warn(`odd authorization answer (${answer})`);
47
55
  return [false, authorizationsCount];
56
+ }
48
57
  }
49
58
  };
50
59
  };
@@ -56,7 +65,7 @@ export default (logger) => {
56
65
  const [authorize] = doesAnswerAuthorizeAndWarn(answer);
57
66
  return authorize;
58
67
  };
59
- } catch (e) {
68
+ } catch {
60
69
  throw new Error(`logger.warn must be a function`);
61
70
  }
62
71
  };
@@ -1,6 +1,6 @@
1
- export default (ctx) => (requests) =>
1
+ export default (context) => (requests) =>
2
2
  Promise.all(
3
3
  requests.map(({ eventName, parameters, options }) =>
4
- ctx.service.$$callEvent(ctx, { eventName, payload: parameters, options })
4
+ context.service.$$callEvent(context, { eventName, payload: parameters, options })
5
5
  )
6
6
  );