@autofleet/zehut 1.6.2 → 1.6.3-beta

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.
@@ -7,6 +7,7 @@ export interface UserPayload {
7
7
  demandSources: EntityPermissions;
8
8
  createdAt?: string;
9
9
  }
10
+ export declare type CustomPermissionLoader = (string: any) => Promise<UserPayload>;
10
11
  export default class ApiUser {
11
12
  id: string | undefined;
12
13
  privatePermissions: UserPayload | undefined;
@@ -14,6 +15,7 @@ export default class ApiUser {
14
15
  emptyUser: boolean;
15
16
  constructor(id?: string);
16
17
  getUserPermissions(): Promise<UserPayload>;
18
+ useCustomPermissionLoader(customPermissionLoader: any): Promise<unknown>;
17
19
  get businessModels(): string[] | undefined;
18
20
  get fleets(): string[] | undefined;
19
21
  get demandSources(): string[] | undefined;
@@ -8,9 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  /* eslint-disable consistent-return */
16
+ const node_cache_1 = __importDefault(require("node-cache"));
13
17
  const identity_ms_1 = require("../services/identity-ms");
18
+ const userCache = new node_cache_1.default({ stdTTL: 10 });
14
19
  class ApiUser {
15
20
  constructor(id) {
16
21
  this.id = id;
@@ -29,6 +34,24 @@ class ApiUser {
29
34
  return this.privatePermissions;
30
35
  });
31
36
  }
37
+ useCustomPermissionLoader(customPermissionLoader) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ if (!this.id) {
40
+ return;
41
+ }
42
+ if (this.privatePermissions) {
43
+ return this.privatePermissions;
44
+ }
45
+ const cachedResult = userCache.get(this.id);
46
+ if (cachedResult) {
47
+ return cachedResult;
48
+ }
49
+ const data = yield customPermissionLoader(this.id);
50
+ userCache.set(this.id, data, data);
51
+ this.privatePermissions = data;
52
+ return this.privatePermissions;
53
+ });
54
+ }
32
55
  get businessModels() {
33
56
  return this.getUserProperty('businessModels');
34
57
  }
@@ -1,7 +1,8 @@
1
- import ApiUser from './api-user';
1
+ import ApiUser, { CustomPermissionLoader } from './api-user';
2
2
  export declare const middleware: (options?: {
3
3
  eagerLoadUserPermissions?: boolean;
4
4
  eagerLoadUserPermissionsLegacy?: boolean;
5
+ customPermissionLoader?: CustomPermissionLoader;
5
6
  }) => (req: any, res: any, next: any) => Promise<any>;
6
7
  export declare const middlewareWithDecode: (options?: {
7
8
  eagerLoadUserPermissions?: boolean;
package/lib/user/index.js CHANGED
@@ -26,7 +26,12 @@ exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, voi
26
26
  else {
27
27
  const userObject = new api_user_1.default(userId);
28
28
  if (options.eagerLoadUserPermissions) {
29
- yield userObject.getUserPermissions();
29
+ if (options.customPermissionLoader) {
30
+ yield userObject.useCustomPermissionLoader(options.customPermissionLoader);
31
+ }
32
+ else {
33
+ yield userObject.getUserPermissions();
34
+ }
30
35
  }
31
36
  if (options.eagerLoadUserPermissionsLegacy) {
32
37
  yield userObject.getUserPermissionsLegacy();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "1.6.2",
3
+ "version": "1.6.3-beta",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -31,6 +31,7 @@
31
31
  "@types/jest": "^22.0.0",
32
32
  "jsonwebtoken": "^8.5.1",
33
33
  "moment": "^2.29.1",
34
+ "node-cache": "^5.1.2",
34
35
  "uuid": "^8.3.2"
35
36
  },
36
37
  "devDependencies": {