@autofleet/zehut 1.0.3 → 1.0.5

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/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import User from './user';
2
+ export { User };
package/lib/index.js ADDED
@@ -0,0 +1,8 @@
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.User = void 0;
7
+ const user_1 = __importDefault(require("./user"));
8
+ exports.User = user_1.default;
@@ -0,0 +1,15 @@
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?: {
11
+ eagerLoadUserPermissions?: boolean;
12
+ eagerLoadUserPermissionsLegacy?: boolean;
13
+ }) => (req: any, res: any, next: any) => Promise<void>;
14
+ export default _default;
15
+ export declare const eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<void>;
@@ -0,0 +1,82 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
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* () {
65
+ if (req.headers['x-af-user-id']) {
66
+ req.user = new ApiUser(req.headers['x-af-user-id']);
67
+ }
68
+ else {
69
+ req.user = new ApiUser();
70
+ }
71
+ if (options.eagerLoadUserPermissions) {
72
+ yield req.user.getUserPermissions();
73
+ }
74
+ if (options.eagerLoadUserPermissionsLegacy) {
75
+ yield req.user.getUserPermissionsLegacy();
76
+ }
77
+ next();
78
+ });
79
+ exports.eagerLoadPermissionsMiddleware = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
80
+ yield req.user.getUserPermissions();
81
+ next();
82
+ });
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "manage user's identity",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
5
7
  "scripts": {
6
8
  "build": "tsc",
7
9
  "coverage": "jest --coverage --forceExit --runInBand",
@@ -29,5 +31,8 @@
29
31
  },
30
32
  "devDependencies": {
31
33
  "typescript": "^3.9.5"
32
- }
34
+ },
35
+ "files": [
36
+ "lib/**/*"
37
+ ]
33
38
  }
@@ -1,26 +0,0 @@
1
- version: 2
2
- jobs:
3
- test:
4
- docker:
5
- - image: circleci/node:11.14.0
6
- steps:
7
- - checkout
8
- - restore_cache:
9
- keys:
10
- - v1-dependencies-{{ checksum "package.json" }}
11
- # fallback to using the latest cache if no exact match is found
12
- - v1-dependencies-
13
- - run: npm i
14
- - save_cache:
15
- paths:
16
- - node_modules
17
- key: v1-dependencies-{{ checksum "package.json" }}
18
- - run: npm run coverage
19
- - run: rm -rf ./coverage
20
- - run: npm run linter
21
-
22
- workflows:
23
- version: 2
24
- build_and_test:
25
- jobs:
26
- - test
package/.eslintrc.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": [
3
- "airbnb/base"
4
- ],
5
- "env": {
6
- "jest": true
7
- }
8
- }
package/.gitlab-ci.yml DELETED
@@ -1,13 +0,0 @@
1
- image: node:11.14.0
2
-
3
- stages:
4
- - test
5
-
6
- before_script:
7
- - node -v
8
- - npm -v
9
- - npm install
10
- test:
11
- stage: test
12
- script:
13
- - npm run coverage
package/.jest.config.js DELETED
@@ -1,8 +0,0 @@
1
- module.exports = {
2
- testEnvironment: 'node',
3
- coverageThreshold: {
4
- global: {
5
- lines: 80,
6
- },
7
- },
8
- };
package/index.ts DELETED
@@ -1,5 +0,0 @@
1
- import User from './src/user';
2
-
3
- export {
4
- User
5
- };
package/src/.env DELETED
File without changes
package/src/user/index.ts DELETED
@@ -1,93 +0,0 @@
1
- import Network from '@autofleet/network';
2
- const CACHE_LIFETIME_IN_SEC = 10;
3
- const IdentityNetwork = new Network({
4
- serviceName: 'IDENTITY_MS',
5
- retries: 3,
6
- retryCondition: () => true,
7
- cache: process.env.NODE_ENV !== 'test' ? {
8
- maxAge: CACHE_LIFETIME_IN_SEC * 1000,
9
- } : undefined,
10
- });
11
-
12
- interface EntityPermissions {
13
- [key: string]: string[];
14
- }
15
-
16
- export interface UserPayload {
17
- businessModels: EntityPermissions;
18
- fleets: EntityPermissions;
19
- demandSources: EntityPermissions;
20
- createdAt?: string;
21
- }
22
-
23
- class ApiUser {
24
- id: string | undefined;
25
- privatePermissions: UserPayload | undefined;
26
- privatePermissionsLegacy: any;
27
- emptyUser: boolean;
28
-
29
- constructor(id? : string) {
30
- this.id = id;
31
- this.emptyUser = !!id;
32
- }
33
-
34
- async getUserPermissions() {
35
- if (this.privatePermissions) {
36
- return this.privatePermissions;
37
- }
38
- const { data } = await IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload`);
39
-
40
- this.privatePermissions = data;
41
- return this.privatePermissions;
42
- }
43
-
44
- get permissions(): UserPayload | undefined {
45
- if (!this.privatePermissionsLegacy) {
46
- throw new Error('Cannot get permissions without calling (async) getUserPermissions before');
47
- }
48
- return this.privatePermissions;
49
- }
50
-
51
- async getUserPermissionsLegacy() {
52
- if (this.privatePermissionsLegacy) {
53
- return this.privatePermissionsLegacy;
54
- }
55
- const { data } = await IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload-legacy`);
56
-
57
- this.privatePermissionsLegacy = data;
58
- return this.privatePermissionsLegacy;
59
- }
60
-
61
- get permissionsLegacy(): UserPayload {
62
- if (!this.privatePermissionsLegacy) {
63
- throw new Error('Cannot get permissionsLegacy without calling (async) getUserPermissionsLegacy before');
64
- }
65
- return this.privatePermissionsLegacy;
66
- }
67
- }
68
-
69
- export default (options: {
70
- eagerLoadUserPermissions?: boolean;
71
- eagerLoadUserPermissionsLegacy?: boolean;
72
- } = {}) => async (req, res, next): Promise<void> => {
73
- if (req.headers['x-af-user-id']) {
74
- req.user = new ApiUser(req.headers['x-af-user-id']);
75
- } else {
76
- req.user = new ApiUser();
77
- }
78
-
79
- if (options.eagerLoadUserPermissions) {
80
- await req.user.getUserPermissions();
81
- }
82
-
83
- if (options.eagerLoadUserPermissionsLegacy) {
84
- await req.user.getUserPermissionsLegacy();
85
- }
86
-
87
- next();
88
- };
89
-
90
- export const eagerLoadPermissionsMiddleware = async (req , res, next) => {
91
- await req.user.getUserPermissions();
92
- next();
93
- };
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es6",
4
- "module": "commonjs",
5
- "declaration": true,
6
- "outDir": "./lib",
7
- "esModuleInterop": true,
8
- "experimentalDecorators": true,
9
- "emitDecoratorMetadata": true,
10
- "allowJs": true,
11
- },
12
- "include": ["src"],
13
- "exclude": ["node_modules"]
14
- }