@foxy.io/sdk 1.16.1 → 1.16.2
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/cjs/backend/API.js +28 -15
- package/dist/esm/backend/API.js +28 -15
- package/dist/types/backend/API.d.ts +1 -0
- package/package.json +1 -1
package/dist/cjs/backend/API.js
CHANGED
|
@@ -52,6 +52,7 @@ class API extends Core.API {
|
|
|
52
52
|
this.clientSecret = params.clientSecret;
|
|
53
53
|
this.clientId = params.clientId;
|
|
54
54
|
this.version = (_d = params.version) !== null && _d !== void 0 ? _d : API.VERSION;
|
|
55
|
+
this.__tokenRefreshPromise = null;
|
|
55
56
|
}
|
|
56
57
|
static getToken(opts, throwOnFailure = false) {
|
|
57
58
|
var _a, _b;
|
|
@@ -83,25 +84,36 @@ class API extends Core.API {
|
|
|
83
84
|
__fetch(input, init) {
|
|
84
85
|
var _a, _b, _c;
|
|
85
86
|
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
-
let token = JSON.parse((_a = this.storage.getItem(API.ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : 'null');
|
|
87
87
|
let request = new cross_fetch_1.Request(input, init);
|
|
88
88
|
let headers = request.headers;
|
|
89
89
|
const fetchNewAccessToken = () => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
this.
|
|
91
|
-
|
|
92
|
-
this.
|
|
93
|
-
return null;
|
|
94
|
-
});
|
|
95
|
-
if (rawToken) {
|
|
96
|
-
const token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
97
|
-
this.storage.setItem(API.ACCESS_TOKEN, JSON.stringify(token));
|
|
98
|
-
this.console.info('Access token updated.');
|
|
99
|
-
return token;
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
this.console.warn('Failed to fetch access token. Proceeding without authentication.');
|
|
103
|
-
return null;
|
|
90
|
+
if (this.__tokenRefreshPromise) {
|
|
91
|
+
this.console.trace('Token refresh already in progress, waiting...');
|
|
92
|
+
return this.__tokenRefreshPromise;
|
|
104
93
|
}
|
|
94
|
+
this.__tokenRefreshPromise = (() => __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
try {
|
|
96
|
+
this.console.trace('Fetching a new access token...');
|
|
97
|
+
const rawToken = yield API.getToken(this, true).catch(err => {
|
|
98
|
+
this.console.error(err.message);
|
|
99
|
+
return null;
|
|
100
|
+
});
|
|
101
|
+
if (rawToken) {
|
|
102
|
+
const token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
103
|
+
this.storage.setItem(API.ACCESS_TOKEN, JSON.stringify(token));
|
|
104
|
+
this.console.info('Access token updated.');
|
|
105
|
+
return token;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
this.console.warn('Failed to fetch access token. Proceeding without authentication.');
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
this.__tokenRefreshPromise = null;
|
|
114
|
+
}
|
|
115
|
+
}))();
|
|
116
|
+
return this.__tokenRefreshPromise;
|
|
105
117
|
});
|
|
106
118
|
const setHeaders = (accessToken) => {
|
|
107
119
|
if (!headers.get('Authorization') && accessToken)
|
|
@@ -111,6 +123,7 @@ class API extends Core.API {
|
|
|
111
123
|
if (!headers.get('FOXY-API-VERSION'))
|
|
112
124
|
headers.set('FOXY-API-VERSION', this.version);
|
|
113
125
|
};
|
|
126
|
+
let token = JSON.parse((_a = this.storage.getItem(API.ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : 'null');
|
|
114
127
|
if (token) {
|
|
115
128
|
const expiresAt = new Date(token.date_created).getTime() + token.expires_in * 1000;
|
|
116
129
|
const refreshAt = Date.now() + API.REFRESH_THRESHOLD;
|
package/dist/esm/backend/API.js
CHANGED
|
@@ -27,6 +27,7 @@ export class API extends Core.API {
|
|
|
27
27
|
this.clientSecret = params.clientSecret;
|
|
28
28
|
this.clientId = params.clientId;
|
|
29
29
|
this.version = (_d = params.version) !== null && _d !== void 0 ? _d : API.VERSION;
|
|
30
|
+
this.__tokenRefreshPromise = null;
|
|
30
31
|
}
|
|
31
32
|
static getToken(opts, throwOnFailure = false) {
|
|
32
33
|
var _a, _b;
|
|
@@ -58,25 +59,36 @@ export class API extends Core.API {
|
|
|
58
59
|
__fetch(input, init) {
|
|
59
60
|
var _a, _b, _c;
|
|
60
61
|
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
-
let token = JSON.parse((_a = this.storage.getItem(API.ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : 'null');
|
|
62
62
|
let request = new Request(input, init);
|
|
63
63
|
let headers = request.headers;
|
|
64
64
|
const fetchNewAccessToken = () => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
this.
|
|
66
|
-
|
|
67
|
-
this.
|
|
68
|
-
return null;
|
|
69
|
-
});
|
|
70
|
-
if (rawToken) {
|
|
71
|
-
const token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
72
|
-
this.storage.setItem(API.ACCESS_TOKEN, JSON.stringify(token));
|
|
73
|
-
this.console.info('Access token updated.');
|
|
74
|
-
return token;
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
this.console.warn('Failed to fetch access token. Proceeding without authentication.');
|
|
78
|
-
return null;
|
|
65
|
+
if (this.__tokenRefreshPromise) {
|
|
66
|
+
this.console.trace('Token refresh already in progress, waiting...');
|
|
67
|
+
return this.__tokenRefreshPromise;
|
|
79
68
|
}
|
|
69
|
+
this.__tokenRefreshPromise = (() => __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
try {
|
|
71
|
+
this.console.trace('Fetching a new access token...');
|
|
72
|
+
const rawToken = yield API.getToken(this, true).catch(err => {
|
|
73
|
+
this.console.error(err.message);
|
|
74
|
+
return null;
|
|
75
|
+
});
|
|
76
|
+
if (rawToken) {
|
|
77
|
+
const token = Object.assign(Object.assign({}, rawToken), { date_created: new Date().toISOString() });
|
|
78
|
+
this.storage.setItem(API.ACCESS_TOKEN, JSON.stringify(token));
|
|
79
|
+
this.console.info('Access token updated.');
|
|
80
|
+
return token;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
this.console.warn('Failed to fetch access token. Proceeding without authentication.');
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
this.__tokenRefreshPromise = null;
|
|
89
|
+
}
|
|
90
|
+
}))();
|
|
91
|
+
return this.__tokenRefreshPromise;
|
|
80
92
|
});
|
|
81
93
|
const setHeaders = (accessToken) => {
|
|
82
94
|
if (!headers.get('Authorization') && accessToken)
|
|
@@ -86,6 +98,7 @@ export class API extends Core.API {
|
|
|
86
98
|
if (!headers.get('FOXY-API-VERSION'))
|
|
87
99
|
headers.set('FOXY-API-VERSION', this.version);
|
|
88
100
|
};
|
|
101
|
+
let token = JSON.parse((_a = this.storage.getItem(API.ACCESS_TOKEN)) !== null && _a !== void 0 ? _a : 'null');
|
|
89
102
|
if (token) {
|
|
90
103
|
const expiresAt = new Date(token.date_created).getTime() + token.expires_in * 1000;
|
|
91
104
|
const refreshAt = Date.now() + API.REFRESH_THRESHOLD;
|
package/package.json
CHANGED