@mondaydotcomorg/monday-authorization 1.0.44-featureyoniadd-granted-feature-sdk-api.559 → 1.0.44-featureyoniadd-granted-feature-sdk-api.560

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/README.md CHANGED
@@ -16,10 +16,6 @@ show is the action, post is the resource.
16
16
  yarn add @mondaydotcomorg/monday-authorization
17
17
  ```
18
18
 
19
- ## Usage
20
-
21
- First init the package in order for it to work properly
22
-
23
19
  app.ts:
24
20
 
25
21
  ```ts
@@ -28,11 +24,24 @@ import * as MondayAuthorization from '@mondaydotcomorg/monday-authorization';
28
24
 
29
25
  ...
30
26
 
31
- MondayAuthorization.init({ prometheus: getPrometheus() });
27
+ MondayAuthorization.init({
28
+ prometheus: getPrometheus(),
29
+ redisClient: redisClient,
30
+ grantedFeatureRedisExpirationInSeconds: 10 * 60
31
+ });
32
32
  startServer(...)
33
33
  ```
34
34
 
35
- Then add this code to any route declaration:
35
+ **Recommended** - optionally init authorization with redisClient so the granted feature results will be cached and reduce http calls.
36
+
37
+ - grantedFeatureRedisExpirationInSeconds - (optional), redis TTL for cached granted features, default set to 5 minutes
38
+
39
+
40
+ ## Usage
41
+
42
+ ### Use as Middleware
43
+
44
+ Add this code to any route declaration:
36
45
 
37
46
  ```ts
38
47
  import { Router } from 'express';
@@ -80,3 +89,15 @@ function, that looks like this:
80
89
  Add `authorizationCheckMiddleware` to make sure that all routes are covered by authorization check. Put this
81
90
  middleware before you define the routes.
82
91
  If you want to skip authorization, use `skipAuthorizationMiddleware`.
92
+
93
+
94
+ ### Granted Features API
95
+
96
+ Use `AuthorizationService.isUserGrantedWithFeature(accountId: number, userId: number, featureName: string): Promise<boolean>` to retrieve if a user is granted with a feature
97
+
98
+ ```ts
99
+ import {AuthorizationService, init} from '@mondaydotcomorg/monday-authorization';
100
+
101
+ let isFeatuerGranted = await AuthorizationService.isUserGrantedWithFeature(739628, 2, "unauthorized_content_change_message")
102
+ ```
103
+
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export interface InitOptions {
3
3
  prometheus?: any;
4
4
  mondayFetchOptions?: MondayFetchOptions;
5
5
  redisClient?: any;
6
- redisKeyExpirationInSeconds?: number;
6
+ grantedFeatureRedisExpirationInSeconds?: number;
7
7
  }
8
8
  export declare function init(options?: InitOptions): void;
9
9
  export { authorizationCheckMiddleware, getAuthorizationMiddleware, skipAuthorizationMiddleware, } from './lib/authorization-middleware';
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ function init(options = {}) {
11
11
  authorization_service_1.setRequestFetchOptions(options.mondayFetchOptions);
12
12
  }
13
13
  if (options.redisClient) {
14
- authorization_service_1.setRedisClient(options.redisClient, options.redisKeyExpirationInSeconds);
14
+ authorization_service_1.setRedisClient(options.redisClient, options.grantedFeatureRedisExpirationInSeconds);
15
15
  }
16
16
  }
17
17
  exports.init = init;
@@ -5,10 +5,10 @@ export interface AuthorizeResponse {
5
5
  unauthorizedIds?: number[];
6
6
  }
7
7
  export declare function setRequestFetchOptions(customMondayFetchOptions: MondayFetchOptions): void;
8
- export declare function setRedisClient(client: any, redisKeyExpirationInSeconds?: number): void;
8
+ export declare function setRedisClient(client: any, grantedFeatureRedisExpirationInSeconds?: number): void;
9
9
  export declare class AuthorizationService {
10
10
  static redisClient: any;
11
- static cacheExpirationInSeconds: number;
11
+ static grantedFeatureRedisExpirationInSeconds: number;
12
12
  /**
13
13
  * @deprecated use the second form with authorizationRequestObjects instead,
14
14
  * support of this function will be dropped gradually
@@ -45,10 +45,10 @@ function setRequestFetchOptions(customMondayFetchOptions) {
45
45
  mondayFetchOptions = Object.assign(Object.assign({}, defaultMondayFetchOptions), customMondayFetchOptions);
46
46
  }
47
47
  exports.setRequestFetchOptions = setRequestFetchOptions;
48
- function setRedisClient(client, redisKeyExpirationInSeconds = 5 * 60) {
48
+ function setRedisClient(client, grantedFeatureRedisExpirationInSeconds = 5 * 60) {
49
49
  AuthorizationService.redisClient = client;
50
- if (redisKeyExpirationInSeconds && redisKeyExpirationInSeconds > 0) {
51
- AuthorizationService.cacheExpirationInSeconds = redisKeyExpirationInSeconds;
50
+ if (grantedFeatureRedisExpirationInSeconds && grantedFeatureRedisExpirationInSeconds > 0) {
51
+ AuthorizationService.grantedFeatureRedisExpirationInSeconds = grantedFeatureRedisExpirationInSeconds;
52
52
  }
53
53
  }
54
54
  exports.setRedisClient = setRedisClient;
@@ -68,8 +68,9 @@ class AuthorizationService {
68
68
  }
69
69
  static isUserGrantedWithFeature(accountId, userId, featureName) {
70
70
  return __awaiter(this, void 0, void 0, function* () {
71
+ let cachedKey = this.getCachedKey(userId, featureName);
71
72
  if (this.redisClient) {
72
- let grantedFeatureValue = yield this.redisClient.get(this.getCachedKey(userId, featureName));
73
+ let grantedFeatureValue = yield this.redisClient.get(cachedKey);
73
74
  if (!(grantedFeatureValue === undefined || grantedFeatureValue === null)) {
74
75
  // redis returns the value as string
75
76
  return grantedFeatureValue === 'true';
@@ -77,7 +78,7 @@ class AuthorizationService {
77
78
  }
78
79
  let grantedFeatureValue = yield this.fetchIsUserGrantedWithFeature(featureName, accountId, userId);
79
80
  if (this.redisClient) {
80
- yield this.redisClient.set(this.getCachedKey(userId, featureName), grantedFeatureValue, 'EX', this.cacheExpirationInSeconds);
81
+ yield this.redisClient.set(cachedKey, grantedFeatureValue, 'EX', this.grantedFeatureRedisExpirationInSeconds);
81
82
  }
82
83
  return grantedFeatureValue;
83
84
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mondaydotcomorg/monday-authorization",
3
- "version": "1.0.44-featureyoniadd-granted-feature-sdk-api.559+451485bc",
3
+ "version": "1.0.44-featureyoniadd-granted-feature-sdk-api.560+aa13dbf4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "BSD-3-Clause",
@@ -32,5 +32,5 @@
32
32
  "files": [
33
33
  "dist/"
34
34
  ],
35
- "gitHead": "451485bcb0a7978d4418ba9a8da24215378f5516"
35
+ "gitHead": "aa13dbf433d5ef55c0b59c19fedab103d4d707b4"
36
36
  }