@chift/chift-nodejs 1.0.29 → 1.0.30
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/dist/src/modules/internalApi.js +44 -26
- package/package.json +1 -1
|
@@ -15,34 +15,44 @@ 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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
this.token = res.data;
|
|
42
|
+
return;
|
|
33
43
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
lastErr = err;
|
|
46
|
+
if (axios_1.default.isAxiosError(err) && ((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
|
|
47
|
+
throw new Error('The provided credentials are not correct');
|
|
48
|
+
}
|
|
49
|
+
if (attempt < maxRetries) {
|
|
50
|
+
const delayMs = baseDelayMs * attempt;
|
|
51
|
+
yield sleep(delayMs);
|
|
43
52
|
}
|
|
44
53
|
}
|
|
45
54
|
}
|
|
55
|
+
throw lastErr || new Error('Token refresh failed');
|
|
46
56
|
});
|
|
47
57
|
this.getPaginationParams = (currPage) => {
|
|
48
58
|
return {
|
|
@@ -61,7 +71,7 @@ class InternalAPI {
|
|
|
61
71
|
this.delete = this.instance.delete;
|
|
62
72
|
this.instance.interceptors.request.use((config) => {
|
|
63
73
|
return new Promise((resolve, reject) => {
|
|
64
|
-
var _a
|
|
74
|
+
var _a;
|
|
65
75
|
if (this.connectionId) {
|
|
66
76
|
config.headers['X-Chift-ConnectionId'] = this.connectionId;
|
|
67
77
|
}
|
|
@@ -73,12 +83,18 @@ class InternalAPI {
|
|
|
73
83
|
this.relatedChainExecutionId;
|
|
74
84
|
}
|
|
75
85
|
if (this.token) {
|
|
76
|
-
|
|
86
|
+
const now = new Date().getTime();
|
|
87
|
+
const bufferMs = 30 * 1000;
|
|
88
|
+
// API returns expires_on in seconds; refresh when expired or within buffer
|
|
89
|
+
if (this.token.expires_on * 1000 < now + bufferMs) {
|
|
77
90
|
return this.getToken()
|
|
78
91
|
.then(() => {
|
|
79
92
|
var _a;
|
|
93
|
+
if (!((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token)) {
|
|
94
|
+
return reject(new Error('Token refresh did not return a valid token'));
|
|
95
|
+
}
|
|
80
96
|
config.headers['Authorization'] =
|
|
81
|
-
'Bearer ' +
|
|
97
|
+
'Bearer ' + this.token.access_token;
|
|
82
98
|
return resolve(config);
|
|
83
99
|
})
|
|
84
100
|
.catch((err) => {
|
|
@@ -86,7 +102,7 @@ class InternalAPI {
|
|
|
86
102
|
});
|
|
87
103
|
}
|
|
88
104
|
else {
|
|
89
|
-
config.headers['Authorization'] = 'Bearer ' + ((
|
|
105
|
+
config.headers['Authorization'] = 'Bearer ' + ((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token);
|
|
90
106
|
return resolve(config);
|
|
91
107
|
}
|
|
92
108
|
}
|
|
@@ -94,8 +110,11 @@ class InternalAPI {
|
|
|
94
110
|
return this.getToken()
|
|
95
111
|
.then(() => {
|
|
96
112
|
var _a;
|
|
113
|
+
if (!((_a = this.token) === null || _a === void 0 ? void 0 : _a.access_token)) {
|
|
114
|
+
return reject(new Error('Token fetch did not return a valid token'));
|
|
115
|
+
}
|
|
97
116
|
config.headers['Authorization'] =
|
|
98
|
-
'Bearer ' +
|
|
117
|
+
'Bearer ' + this.token.access_token;
|
|
99
118
|
return resolve(config);
|
|
100
119
|
})
|
|
101
120
|
.catch((err) => {
|
|
@@ -104,7 +123,6 @@ class InternalAPI {
|
|
|
104
123
|
}
|
|
105
124
|
});
|
|
106
125
|
}, function (error) {
|
|
107
|
-
// Do something with request error
|
|
108
126
|
return Promise.reject(error);
|
|
109
127
|
});
|
|
110
128
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chift/chift-nodejs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.30",
|
|
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",
|