@chift/chift-nodejs 1.0.29 → 1.0.31

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.
@@ -15,34 +15,48 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.InternalAPI = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
17
  const settings_1 = __importDefault(require("../helpers/settings"));
18
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
18
19
  class InternalAPI {
19
20
  constructor(auth) {
20
21
  this.debug = false;
21
22
  this.getToken = () => __awaiter(this, void 0, void 0, function* () {
22
- try {
23
- const tokenData = {
24
- clientId: this.auth.clientId,
25
- clientSecret: this.auth.clientSecret,
26
- accountId: this.auth.accountId,
27
- };
28
- if (this.auth.marketplaceId) {
29
- tokenData['marketplaceId'] = this.auth.marketplaceId;
30
- }
31
- if (this.auth.envId) {
32
- tokenData['envId'] = this.auth.envId;
23
+ var _a;
24
+ const maxRetries = 3;
25
+ const baseDelayMs = 1500;
26
+ let lastErr;
27
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
28
+ try {
29
+ const tokenData = {
30
+ clientId: this.auth.clientId,
31
+ clientSecret: this.auth.clientSecret,
32
+ accountId: this.auth.accountId,
33
+ };
34
+ if (this.auth.marketplaceId) {
35
+ tokenData['marketplaceId'] = this.auth.marketplaceId;
36
+ }
37
+ if (this.auth.envId) {
38
+ tokenData['envId'] = this.auth.envId;
39
+ }
40
+ const res = yield axios_1.default.post(`${this.auth.baseUrl || settings_1.default.BASE_URL}/token`, tokenData, {
41
+ headers: {
42
+ 'Content-Type': 'application/json',
43
+ },
44
+ });
45
+ this.token = res.data;
46
+ return;
33
47
  }
34
- const res = yield axios_1.default.post(`${this.auth.baseUrl || settings_1.default.BASE_URL}/token`, tokenData);
35
- this.token = res.data;
36
- }
37
- catch (err) {
38
- if (axios_1.default.isAxiosError(err)) {
39
- if (err.response) {
40
- if (err.response.status === 401) {
41
- throw new Error('The provided credentials are not correct');
42
- }
48
+ catch (err) {
49
+ lastErr = err;
50
+ if (axios_1.default.isAxiosError(err) && ((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
51
+ throw new Error('The provided credentials are not correct');
52
+ }
53
+ if (attempt < maxRetries) {
54
+ const delayMs = baseDelayMs * attempt;
55
+ yield sleep(delayMs);
43
56
  }
44
57
  }
45
58
  }
59
+ throw lastErr || new Error('Token refresh failed');
46
60
  });
47
61
  this.getPaginationParams = (currPage) => {
48
62
  return {
@@ -54,6 +68,9 @@ class InternalAPI {
54
68
  this.auth = auth;
55
69
  this.instance = axios_1.default.create({
56
70
  baseURL: this.auth.baseUrl || settings_1.default.BASE_URL,
71
+ headers: {
72
+ 'Content-Type': 'application/json',
73
+ },
57
74
  });
58
75
  this.get = this.instance.get;
59
76
  this.post = this.instance.post;
@@ -61,7 +78,7 @@ class InternalAPI {
61
78
  this.delete = this.instance.delete;
62
79
  this.instance.interceptors.request.use((config) => {
63
80
  return new Promise((resolve, reject) => {
64
- var _a, _b;
81
+ var _a;
65
82
  if (this.connectionId) {
66
83
  config.headers['X-Chift-ConnectionId'] = this.connectionId;
67
84
  }
@@ -73,12 +90,18 @@ class InternalAPI {
73
90
  this.relatedChainExecutionId;
74
91
  }
75
92
  if (this.token) {
76
- if (((_a = this.token) === null || _a === void 0 ? void 0 : _a.expires_on) < new Date().getTime()) {
93
+ const now = new Date().getTime();
94
+ const bufferMs = 30 * 1000;
95
+ // API returns expires_on in seconds; refresh when expired or within buffer
96
+ if (this.token.expires_on * 1000 < now + bufferMs) {
77
97
  return this.getToken()
78
98
  .then(() => {
79
99
  var _a;
100
+ if (!((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token)) {
101
+ return reject(new Error('Token refresh did not return a valid token'));
102
+ }
80
103
  config.headers['Authorization'] =
81
- 'Bearer ' + ((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token);
104
+ 'Bearer ' + this.token.access_token;
82
105
  return resolve(config);
83
106
  })
84
107
  .catch((err) => {
@@ -86,7 +109,7 @@ class InternalAPI {
86
109
  });
87
110
  }
88
111
  else {
89
- config.headers['Authorization'] = 'Bearer ' + ((_b = this.token) === null || _b === void 0 ? void 0 : _b.access_token);
112
+ config.headers['Authorization'] = 'Bearer ' + ((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token);
90
113
  return resolve(config);
91
114
  }
92
115
  }
@@ -94,8 +117,11 @@ class InternalAPI {
94
117
  return this.getToken()
95
118
  .then(() => {
96
119
  var _a;
120
+ if (!((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token)) {
121
+ return reject(new Error('Token fetch did not return a valid token'));
122
+ }
97
123
  config.headers['Authorization'] =
98
- 'Bearer ' + ((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token);
124
+ 'Bearer ' + this.token.access_token;
99
125
  return resolve(config);
100
126
  })
101
127
  .catch((err) => {
@@ -104,7 +130,6 @@ class InternalAPI {
104
130
  }
105
131
  });
106
132
  }, function (error) {
107
- // Do something with request error
108
133
  return Promise.reject(error);
109
134
  });
110
135
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chift/chift-nodejs",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
4
4
  "description": "The Chift NodeJS library provides convenient access to the Chift API from applications written in the NodeJS language (Javascript/Typescript).",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",