@zenofolio/hyper-decor 1.0.57 → 1.0.59

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.
@@ -1,4 +1,4 @@
1
- import { MiddlewareHandler } from "hyper-express";
1
+ import { MiddlewareHandler } from "hyper-express/types";
2
2
  import { ScopeMap, ScopeType } from "../../decorators";
3
3
  type Callback = (middleware: MiddlewareHandler, scopes: ScopeMap[], names: Set<string>) => void;
4
4
  export default function scopeTransfrom(listScopes: ScopeType[], ...callback: Callback[]): MiddlewareHandler;
@@ -5,25 +5,26 @@ const exeptions_1 = require("../../exeptions");
5
5
  const helpers_1 = require("../../common/helpers");
6
6
  const constants_1 = require("../constants");
7
7
  function scopeTransfrom(listScopes, ...callback) {
8
- const { scopes, scopeNames, isEmtpy } = resolveScopes(listScopes);
8
+ const { scopes, scopeNames, isEmpty } = resolveScopes(listScopes);
9
9
  const middleware = (req, res, next) => {
10
- var _a, _b;
11
- if (isEmtpy)
12
- return next();
13
- // get the user scopes
14
- const userScopes = new Set((_a = (0, helpers_1.getScopes)(req)) !== null && _a !== void 0 ? _a : []);
15
- if (userScopes.size === 0 && isEmtpy)
10
+ var _a;
11
+ if (isEmpty)
16
12
  return next();
13
+ const userScopesRaw = (0, helpers_1.getScopes)(req);
14
+ if (!userScopesRaw || userScopesRaw.length === 0) {
15
+ return next(new exeptions_1.NotScopeException(`FORBIDDEN`, [], Array.from(scopeNames)));
16
+ }
17
+ const userScopes = new Set(userScopesRaw);
17
18
  if (userScopes.has(constants_1.FULL_ACCESS))
18
19
  return next();
19
- // find the first scope that is not in the userScopes
20
- const error = scopes.find((scope) => !userScopes.has(scope.scope));
21
- if (error) {
22
- return next(new exeptions_1.NotScopeException((_b = error.message) !== null && _b !== void 0 ? _b : `You don't have the required scopes to access this resource`, Array.from(userScopes), Array.from(scopeNames)));
20
+ for (const scope of scopes) {
21
+ if (!userScopes.has(scope.scope)) {
22
+ return next(new exeptions_1.NotScopeException((_a = scope.message) !== null && _a !== void 0 ? _a : `FORBIDDEN`, userScopesRaw, Array.from(scopeNames)));
23
+ }
23
24
  }
24
25
  return next();
25
26
  };
26
- if (scopeNames.size > 0 && callback && callback.length > 0) {
27
+ if (!isEmpty && callback.length > 0) {
27
28
  for (const cb of callback)
28
29
  cb(middleware, scopes, scopeNames);
29
30
  }
@@ -40,36 +41,47 @@ const resolveScopes = (scopes) => {
40
41
  var _a, _b;
41
42
  const $scopes = {};
42
43
  for (const scope of scopes) {
43
- if (typeof scope === "string") {
44
- $scopes[scope] = {
45
- scope,
46
- description: "",
44
+ const list = parseScope(scope);
45
+ if (list.length === 0)
46
+ continue;
47
+ for (const s of list) {
48
+ $scopes[s.scope] = {
49
+ scope: s.scope,
50
+ description: (_a = s.description) !== null && _a !== void 0 ? _a : "",
51
+ message: (_b = s.message) !== null && _b !== void 0 ? _b : `You don't have the required scopes to access this resource`,
47
52
  };
48
53
  }
49
- else if (Array.isArray(scope)) {
50
- for (const s of scope) {
51
- switch (typeof s) {
52
- case "string":
53
- $scopes[s] = {
54
- scope: s,
55
- description: "",
56
- };
57
- break;
58
- case "object":
59
- $scopes[s.scope] = {
60
- scope: s.scope,
61
- description: (_a = s.description) !== null && _a !== void 0 ? _a : "",
62
- message: (_b = s.message) !== null && _b !== void 0 ? _b : `You don't have the required scopes to access this resource`,
63
- };
64
- break;
65
- }
66
- }
67
- }
68
54
  }
69
55
  const values = Object.values($scopes);
70
56
  return {
71
57
  scopes: values,
72
58
  scopeNames: new Set(Object.keys($scopes)),
73
- isEmtpy: values.length === 0,
59
+ isEmpty: values.length === 0,
74
60
  };
75
61
  };
62
+ /**
63
+ * Parse the scope to a standard format
64
+ *
65
+ * @param scope
66
+ * @returns
67
+ */
68
+ const parseScope = (scope) => {
69
+ switch (typeof scope) {
70
+ case "string":
71
+ return [
72
+ {
73
+ scope,
74
+ description: "",
75
+ },
76
+ ];
77
+ case "object":
78
+ if (Array.isArray(scope)) {
79
+ if (scope.length === 0)
80
+ return [];
81
+ return scope.map((s) => parseScope(s)).flat();
82
+ }
83
+ else {
84
+ return [scope];
85
+ }
86
+ }
87
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenofolio/hyper-decor",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
4
4
  "description": "Project core with utilities and features",
5
5
  "main": "dist/index.js",
6
6
  "author": "zenozaga",
@@ -15,7 +15,7 @@
15
15
  "hyper-express": "^6.17.3"
16
16
  },
17
17
  "devDependencies": {
18
- "@types/file-type": "^10.9.3",
18
+ "@types/file-type": "10.6.0",
19
19
  "@types/mocha": "^10.0.8",
20
20
  "chai": "^5.1.2",
21
21
  "mocha": "^10.7.3",