@voucherify/sdk 2.0.4 → 2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @voucherify/sdk
2
2
 
3
+ ## 2.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [`a7811e7`](https://github.com/voucherifyio/voucherify-js-sdk/commit/a7811e7e2939efe7a4c59c004dbc9591fcd73030) [#121](https://github.com/voucherifyio/voucherify-js-sdk/pull/121) Thanks [@jkaliszuk](https://github.com/jkaliszuk)! - Added logic that allow to storage lastResponseHeaders data and get apiLimit through ApiLimitsHandler
8
+
3
9
  ## 2.0.4
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -66,6 +66,8 @@ Server side:
66
66
  <a href="#events">Events</a>
67
67
  |
68
68
  <a href="#async-actions">Async Actions</a>
69
+ |
70
+ <a href="#api-limits-handler">Api Limits Handler</a>
69
71
  </p>
70
72
 
71
73
  <p align="center">
@@ -1124,6 +1126,37 @@ client.asyncActions.list()
1124
1126
  client.asyncActions.list(params)
1125
1127
  ```
1126
1128
 
1129
+ ---
1130
+
1131
+ ### Api Limits Handler
1132
+
1133
+ Methods are provided within `client.apiLimitsHandler.*` namespace.
1134
+
1135
+ - [Are Limits Available](#are-limits-available)
1136
+ - [Get Rate Limit](#get-rate-limit)
1137
+ - [Get Rate Limit Remaining](#get-rate-limit-remaining)
1138
+
1139
+ #### Are Limits Available
1140
+ ```javascript
1141
+ client.apiLimitsHandler.areLimitsAvailable()
1142
+ ```
1143
+
1144
+ This method should be called each time beforehand [Get Rate Limit](#get-rate-limit) or [Get Rate Limit Remaining](#get-rate-limit-remaining) to ensure limit data are available.
1145
+
1146
+ #### Get Rate Limit
1147
+ ```javascript
1148
+ if(client.apiLimitsHandler.areLimitsAvailable()){
1149
+ client.apiLimitsHandler.getRateLimit()
1150
+ }
1151
+ ```
1152
+
1153
+ #### Get Rate Limit Remaining
1154
+ ```javascript
1155
+ if(client.apiLimitsHandler.areLimitsAvailable()){
1156
+ client.apiLimitsHandler.getRateLimitRemaining()
1157
+ }
1158
+ ```
1159
+
1127
1160
  ---
1128
1161
 
1129
1162
  # 💅 Client Side
@@ -0,0 +1,9 @@
1
+ import { RequestController } from './RequestController';
2
+ export declare class ApiLimitsHandler {
3
+ private readonly requestController;
4
+ constructor(requestController: RequestController);
5
+ private getLastResponseHeadersFromController;
6
+ areLimitsAvailable(): boolean;
7
+ getRateLimit(): number;
8
+ getRateLimitRemaining(): number;
9
+ }
@@ -11,7 +11,12 @@ export declare class RequestController {
11
11
  private basePath;
12
12
  private headers;
13
13
  private request;
14
+ private lastResponseHeaders;
15
+ private isLastResponseHeadersSet;
14
16
  constructor({ basePath, baseURL, headers }: RequestControllerOptions);
17
+ isLastReponseHeadersSet(): boolean;
18
+ getLastResponseHeaders(): Record<string, string>;
19
+ private setLastResponseHeaders;
15
20
  setBaseUrl(baseURL: string): void;
16
21
  get<T>(path: string, params?: Record<string, any>): Promise<T>;
17
22
  post<T>(path: string, body: Record<string, any>, params?: Record<string, any>, headers?: Record<string, any>): Promise<T>;
@@ -14,6 +14,7 @@ import { Rewards } from './Rewards';
14
14
  import { Loyalties } from './Loyalties';
15
15
  import { ValidationRules } from './ValidationRules';
16
16
  import { Segments } from './Segments';
17
+ import { ApiLimitsHandler } from './ApiLimitsHandler';
17
18
  export interface VoucherifyServerSideOptions {
18
19
  /**
19
20
  * Optionally, you can add `apiUrl` to the client options if you want to use Voucherify running in a specific region.
@@ -108,4 +109,5 @@ export declare function VoucherifyServerSide(options: VoucherifyServerSideOption
108
109
  validationRules: ValidationRules;
109
110
  events: Events;
110
111
  asyncActions: AsyncActions;
112
+ apiLimitsHandler: ApiLimitsHandler;
111
113
  };
@@ -44,9 +44,13 @@ class RequestController {
44
44
  this.basePath = void 0;
45
45
  this.headers = void 0;
46
46
  this.request = void 0;
47
+ this.lastResponseHeaders = void 0;
48
+ this.isLastResponseHeadersSet = void 0;
47
49
  this.basePath = basePath;
48
50
  this.baseURL = baseURL;
49
51
  this.headers = headers;
52
+ this.lastResponseHeaders = {};
53
+ this.isLastResponseHeadersSet = false;
50
54
  this.request = axios.create({
51
55
  baseURL: `${this.baseURL}/${this.basePath}/`,
52
56
  headers: this.headers,
@@ -66,6 +70,19 @@ class RequestController {
66
70
  });
67
71
  }
68
72
 
73
+ isLastReponseHeadersSet() {
74
+ return this.isLastResponseHeadersSet;
75
+ }
76
+
77
+ getLastResponseHeaders() {
78
+ return this.lastResponseHeaders;
79
+ }
80
+
81
+ setLastResponseHeaders(headers) {
82
+ this.lastResponseHeaders = headers;
83
+ this.isLastResponseHeadersSet = true;
84
+ }
85
+
69
86
  setBaseUrl(baseURL) {
70
87
  this.baseURL = baseURL;
71
88
  this.request.defaults.baseURL = `${baseURL}/${this.basePath}/`;
@@ -78,6 +95,7 @@ class RequestController {
78
95
  return Qs.stringify(params);
79
96
  }
80
97
  });
98
+ this.setLastResponseHeaders(response.headers);
81
99
  return response.data;
82
100
  }
83
101
 
@@ -89,6 +107,7 @@ class RequestController {
89
107
  },
90
108
  headers
91
109
  });
110
+ this.setLastResponseHeaders(response.headers);
92
111
  return response.data;
93
112
  }
94
113
 
@@ -96,6 +115,7 @@ class RequestController {
96
115
  const response = await this.request.put(path, body, {
97
116
  params
98
117
  });
118
+ this.setLastResponseHeaders(response.headers);
99
119
  return response.data;
100
120
  }
101
121
 
@@ -103,6 +123,7 @@ class RequestController {
103
123
  const response = await this.request.delete(path, {
104
124
  params
105
125
  });
126
+ this.setLastResponseHeaders(response.headers);
106
127
  return response.data;
107
128
  }
108
129
 
@@ -1307,6 +1328,36 @@ class Segments {
1307
1328
 
1308
1329
  }
1309
1330
 
1331
+ class ApiLimitsHandler {
1332
+ constructor(requestController) {
1333
+ this.requestController = void 0;
1334
+ this.requestController = requestController;
1335
+ }
1336
+
1337
+ getLastResponseHeadersFromController() {
1338
+ return this.requestController.getLastResponseHeaders();
1339
+ }
1340
+
1341
+ areLimitsAvailable() {
1342
+ return this.requestController.isLastReponseHeadersSet();
1343
+ }
1344
+
1345
+ getRateLimit() {
1346
+ var _this$getLastResponse;
1347
+
1348
+ const rateLimit = (_this$getLastResponse = this.getLastResponseHeadersFromController()['x-rate-limit-limit']) != null ? _this$getLastResponse : 0;
1349
+ return parseInt(rateLimit, 10);
1350
+ }
1351
+
1352
+ getRateLimitRemaining() {
1353
+ var _this$getLastResponse2;
1354
+
1355
+ const rateLimitRemaining = (_this$getLastResponse2 = this.getLastResponseHeadersFromController()['x-rate-limit-remaining']) != null ? _this$getLastResponse2 : 0;
1356
+ return parseInt(rateLimitRemaining, 10);
1357
+ }
1358
+
1359
+ }
1360
+
1310
1361
  // campaigns: Campaigns
1311
1362
  // consents: Consents
1312
1363
  // customers: Customers
@@ -1335,7 +1386,7 @@ function VoucherifyServerSide(options) {
1335
1386
  let headers = {
1336
1387
  'X-App-Id': options.applicationId,
1337
1388
  'X-App-Token': options.secretKey,
1338
- 'X-Voucherify-Channel': options.channel || `${environment()}-SDK-v${"2.0.4"}`,
1389
+ 'X-Voucherify-Channel': options.channel || `${environment()}-SDK-v${"2.0.5"}`,
1339
1390
  'Content-Type': 'application/json'
1340
1391
  };
1341
1392
 
@@ -1381,6 +1432,7 @@ function VoucherifyServerSide(options) {
1381
1432
  const loyalties = new Loyalties(client);
1382
1433
  const segments = new Segments(client);
1383
1434
  const validationRules = new ValidationRules(client);
1435
+ const apiLimitsHandler = new ApiLimitsHandler(client);
1384
1436
  return {
1385
1437
  vouchers,
1386
1438
  campaigns,
@@ -1397,7 +1449,8 @@ function VoucherifyServerSide(options) {
1397
1449
  segments,
1398
1450
  validationRules,
1399
1451
  events,
1400
- asyncActions
1452
+ asyncActions,
1453
+ apiLimitsHandler
1401
1454
  };
1402
1455
  }
1403
1456
 
@@ -1580,7 +1633,7 @@ function VoucherifyClientSide(options) {
1580
1633
  let headers = {
1581
1634
  'X-Client-Application-Id': options.clientApplicationId,
1582
1635
  'X-Client-Token': options.clientSecretKey,
1583
- 'X-Voucherify-Channel': `${environment()}-ClientSide-SDK-v${"2.0.4"}`
1636
+ 'X-Voucherify-Channel': `${environment()}-ClientSide-SDK-v${"2.0.5"}`
1584
1637
  };
1585
1638
 
1586
1639
  if (environment().startsWith('Node')) {