@autofleet/zehut 1.0.6 → 1.0.8

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.
@@ -0,0 +1 @@
1
+ export declare const IdentityNetwork: any;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.IdentityNetwork = void 0;
7
+ const network_1 = __importDefault(require("@autofleet/network"));
8
+ const CACHE_LIFETIME_IN_SEC = 10;
9
+ exports.IdentityNetwork = new network_1.default({
10
+ serviceName: 'IDENTITY_MS',
11
+ retries: 3,
12
+ retryCondition: () => true,
13
+ cache: process.env.NODE_ENV !== 'test' ? {
14
+ maxAge: CACHE_LIFETIME_IN_SEC * 1000,
15
+ } : undefined,
16
+ });
@@ -0,0 +1,21 @@
1
+ interface EntityPermissions {
2
+ [key: string]: string[];
3
+ }
4
+ export interface UserPayload {
5
+ businessModels: EntityPermissions;
6
+ fleets: EntityPermissions;
7
+ demandSources: EntityPermissions;
8
+ createdAt?: string;
9
+ }
10
+ export default class ApiUser {
11
+ id: string | undefined;
12
+ privatePermissions: UserPayload | undefined;
13
+ privatePermissionsLegacy: any;
14
+ emptyUser: boolean;
15
+ constructor(id?: string);
16
+ getUserPermissions(): Promise<UserPayload>;
17
+ get permissions(): UserPayload | undefined;
18
+ getUserPermissionsLegacy(): Promise<any>;
19
+ get permissionsLegacy(): UserPayload;
20
+ }
21
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const services_1 = require("../services");
13
+ class ApiUser {
14
+ constructor(id) {
15
+ this.id = id;
16
+ this.emptyUser = !!id;
17
+ }
18
+ getUserPermissions() {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ if (this.privatePermissions) {
21
+ return this.privatePermissions;
22
+ }
23
+ const { data } = yield services_1.IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload`);
24
+ this.privatePermissions = data;
25
+ return this.privatePermissions;
26
+ });
27
+ }
28
+ get permissions() {
29
+ if (!this.privatePermissionsLegacy) {
30
+ throw new Error('Cannot get permissions without calling (async) getUserPermissions before');
31
+ }
32
+ return this.privatePermissions;
33
+ }
34
+ getUserPermissionsLegacy() {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ if (this.privatePermissionsLegacy) {
37
+ return this.privatePermissionsLegacy;
38
+ }
39
+ const { data } = yield services_1.IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload-legacy`);
40
+ this.privatePermissionsLegacy = data;
41
+ return this.privatePermissionsLegacy;
42
+ });
43
+ }
44
+ get permissionsLegacy() {
45
+ if (!this.privatePermissionsLegacy) {
46
+ throw new Error('Cannot get permissionsLegacy without calling (async) getUserPermissionsLegacy before');
47
+ }
48
+ return this.privatePermissionsLegacy;
49
+ }
50
+ }
51
+ exports.default = ApiUser;
@@ -1,15 +1,7 @@
1
- interface EntityPermissions {
2
- [key: string]: string[];
3
- }
4
- export interface UserPayload {
5
- businessModels: EntityPermissions;
6
- fleets: EntityPermissions;
7
- demandSources: EntityPermissions;
8
- createdAt?: string;
9
- }
10
- declare const _default: (options?: {
1
+ import ApiUser from './ApiUser';
2
+ export declare const middleware: (options?: {
11
3
  eagerLoadUserPermissions?: boolean;
12
4
  eagerLoadUserPermissionsLegacy?: boolean;
13
5
  }) => (req: any, res: any, next: any) => Promise<void>;
14
- export default _default;
15
6
  export declare const eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<void>;
7
+ export default ApiUser;
package/lib/user/index.js CHANGED
@@ -12,61 +12,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.eagerLoadPermissionsMiddleware = void 0;
16
- const network_1 = __importDefault(require("@autofleet/network"));
17
- const CACHE_LIFETIME_IN_SEC = 10;
18
- const IdentityNetwork = new network_1.default({
19
- serviceName: 'IDENTITY_MS',
20
- retries: 3,
21
- retryCondition: () => true,
22
- cache: process.env.NODE_ENV !== 'test' ? {
23
- maxAge: CACHE_LIFETIME_IN_SEC * 1000,
24
- } : undefined,
25
- });
26
- class ApiUser {
27
- constructor(id) {
28
- this.id = id;
29
- this.emptyUser = !!id;
30
- }
31
- getUserPermissions() {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- if (this.privatePermissions) {
34
- return this.privatePermissions;
35
- }
36
- const { data } = yield IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload`);
37
- this.privatePermissions = data;
38
- return this.privatePermissions;
39
- });
40
- }
41
- get permissions() {
42
- if (!this.privatePermissionsLegacy) {
43
- throw new Error('Cannot get permissions without calling (async) getUserPermissions before');
44
- }
45
- return this.privatePermissions;
46
- }
47
- getUserPermissionsLegacy() {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- if (this.privatePermissionsLegacy) {
50
- return this.privatePermissionsLegacy;
51
- }
52
- const { data } = yield IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload-legacy`);
53
- this.privatePermissionsLegacy = data;
54
- return this.privatePermissionsLegacy;
55
- });
56
- }
57
- get permissionsLegacy() {
58
- if (!this.privatePermissionsLegacy) {
59
- throw new Error('Cannot get permissionsLegacy without calling (async) getUserPermissionsLegacy before');
60
- }
61
- return this.privatePermissionsLegacy;
62
- }
63
- }
64
- exports.default = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
15
+ exports.eagerLoadPermissionsMiddleware = exports.middleware = void 0;
16
+ const ApiUser_1 = __importDefault(require("./ApiUser"));
17
+ exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
65
18
  if (req.headers['x-af-user-id']) {
66
- req.user = new ApiUser(req.headers['x-af-user-id']);
19
+ req.user = new ApiUser_1.default(req.headers['x-af-user-id']);
67
20
  }
68
21
  else {
69
- req.user = new ApiUser();
22
+ req.user = new ApiUser_1.default();
70
23
  }
71
24
  if (options.eagerLoadUserPermissions) {
72
25
  yield req.user.getUserPermissions();
@@ -80,3 +33,4 @@ exports.eagerLoadPermissionsMiddleware = (req, res, next) => __awaiter(void 0, v
80
33
  yield req.user.getUserPermissions();
81
34
  next();
82
35
  });
36
+ exports.default = ApiUser_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",