@opengeoweb/api 4.12.0 → 4.13.1
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 +0 -0
- package/index.d.ts +0 -0
- package/index.esm.js +170 -104
- package/index.umd.js +145 -63
- package/lib/components/ApiContext/ApiContext.d.ts +0 -0
- package/lib/components/ApiContext/ApiContext.spec.d.ts +0 -0
- package/lib/components/ApiContext/index.d.ts +0 -0
- package/lib/components/ApiContext/types.d.ts +2 -0
- package/lib/components/ApiContext/utils.d.ts +6 -1
- package/lib/components/ApiContext/utils.spec.d.ts +0 -0
- package/lib/components/apihooks/index.d.ts +0 -0
- package/lib/components/apihooks/useApi.d.ts +0 -0
- package/lib/components/apihooks/useApi.spec.d.ts +0 -0
- package/lib/components/apihooks/useDebounce.d.ts +0 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
File without changes
|
package/index.d.ts
CHANGED
|
File without changes
|
package/index.esm.js
CHANGED
|
@@ -281,6 +281,10 @@ function useApiContext() {
|
|
|
281
281
|
}
|
|
282
282
|
|
|
283
283
|
var DEFAULT_TIMEOUT = 15000;
|
|
284
|
+
var KEEP_ALIVE_IN_SECONDS = 60; // Number of seconds in between intervals to check with the token request if connection is stil intact
|
|
285
|
+
|
|
286
|
+
var KEEP_ALIVE_POLLER_IN_SECONDS = 10; // Number of milliseconds to check if connection is restored or to check if KEEP_ALIVE_IN_SECONDS has passed
|
|
287
|
+
|
|
284
288
|
var REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75; // Refresh token when 75% expired. Set to (10 / 3600) * 100 = 0.2777778% to test with 10 second interval.
|
|
285
289
|
|
|
286
290
|
var DEFAULT_TOKEN_EXPIRES_IN = 3600; // Number of seconds a token expires by default
|
|
@@ -312,108 +316,168 @@ var makeCredentialsFromTokenResponse = function makeCredentialsFromTokenResponse
|
|
|
312
316
|
var tokenExpirationInSeconds = expires_in && expires_in > 0 ? expires_in : DEFAULT_TOKEN_EXPIRES_IN; // If not set or zero in the token, assume valid for 1 hour.
|
|
313
317
|
|
|
314
318
|
var pctTokenExpirationInSeconds = tokenExpirationInSeconds * (REFRESH_TOKEN_WHEN_PCT_EXPIRED / 100);
|
|
315
|
-
var epochTimeTokenExpirationInSeconds = pctTokenExpirationInSeconds +
|
|
319
|
+
var epochTimeTokenExpirationInSeconds = pctTokenExpirationInSeconds + getCurrentTimeInSeconds();
|
|
316
320
|
var isAuthCognito = userInfo.iss.includes('cognito');
|
|
317
321
|
var newAuth = {
|
|
318
322
|
username: isAuthCognito ? userInfo['cognito:username'] : userInfo.email,
|
|
319
323
|
token: access_token,
|
|
320
324
|
refresh_token: refresh_token || '',
|
|
321
|
-
expires_at: epochTimeTokenExpirationInSeconds
|
|
325
|
+
expires_at: epochTimeTokenExpirationInSeconds,
|
|
326
|
+
keep_session_alive_at: getCurrentTimeInSeconds() + KEEP_ALIVE_IN_SECONDS
|
|
322
327
|
};
|
|
323
328
|
return newAuth;
|
|
324
329
|
};
|
|
325
|
-
var
|
|
330
|
+
var refreshAccessToken = function refreshAccessToken(_ref) {
|
|
326
331
|
var auth = _ref.auth,
|
|
327
|
-
onSetAuth = _ref.onSetAuth,
|
|
328
332
|
_ref$config = _ref.config;
|
|
329
333
|
_ref$config = _ref$config === void 0 ? {} : _ref$config;
|
|
330
|
-
var
|
|
331
|
-
appURL = _ref$config.appURL,
|
|
332
|
-
authTokenURL = _ref$config.authTokenURL,
|
|
334
|
+
var authTokenURL = _ref$config.authTokenURL,
|
|
333
335
|
authClientId = _ref$config.authClientId,
|
|
336
|
+
appURL = _ref$config.appURL,
|
|
334
337
|
_ref$timeout = _ref.timeout,
|
|
335
338
|
timeout = _ref$timeout === void 0 ? DEFAULT_TIMEOUT : _ref$timeout;
|
|
336
|
-
|
|
337
|
-
|
|
339
|
+
// Refresh token request with a new axios instance
|
|
340
|
+
// without request interceptor
|
|
341
|
+
var tokenAxiosInstance = axios.create({
|
|
338
342
|
headers: {},
|
|
339
343
|
timeout: timeout
|
|
340
344
|
});
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
345
|
+
var refreshPayload = {
|
|
346
|
+
refresh_token: auth.refresh_token,
|
|
347
|
+
redirect_uri: "".concat(appURL, "/code"),
|
|
348
|
+
grant_type: 'refresh_token',
|
|
349
|
+
client_id: authClientId
|
|
350
|
+
};
|
|
351
|
+
/* Send data in the "application/x-www-form-urlencoded" format.
|
|
352
|
+
If only JSON is supported, use Axios' default content type ("application/x-www-form-urlencoded"). */
|
|
353
|
+
|
|
354
|
+
var useDefaultContentType = authTokenURL.includes('amazonaws.com');
|
|
355
|
+
var data = useDefaultContentType ? refreshPayload : new URLSearchParams(refreshPayload);
|
|
356
|
+
var axiosConfig = {
|
|
357
|
+
headers: {
|
|
358
|
+
'Content-Type': useDefaultContentType ? 'application/json' : 'application/x-www-form-urlencoded'
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
return tokenAxiosInstance.post(authTokenURL, data, axiosConfig);
|
|
362
|
+
};
|
|
363
|
+
var refreshAccessTokenAndSetAuthContext = function refreshAccessTokenAndSetAuthContext(_ref2) {
|
|
364
|
+
var auth = _ref2.auth,
|
|
365
|
+
onSetAuth = _ref2.onSetAuth,
|
|
366
|
+
config = _ref2.config,
|
|
367
|
+
_ref2$timeout = _ref2.timeout,
|
|
368
|
+
timeout = _ref2$timeout === void 0 ? DEFAULT_TIMEOUT : _ref2$timeout;
|
|
369
|
+
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
370
|
+
var refreshedToken, newAuth;
|
|
371
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
372
|
+
while (1) {
|
|
373
|
+
switch (_context.prev = _context.next) {
|
|
374
|
+
case 0:
|
|
375
|
+
_context.prev = 0;
|
|
376
|
+
_context.next = 3;
|
|
377
|
+
return refreshAccessToken({
|
|
378
|
+
auth: auth,
|
|
379
|
+
config: config,
|
|
380
|
+
timeout: timeout
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
case 3:
|
|
384
|
+
refreshedToken = _context.sent;
|
|
385
|
+
newAuth = makeCredentialsFromTokenResponse(refreshedToken);
|
|
386
|
+
/*
|
|
387
|
+
Note: Should not use the spread operator here on the auth object to create a new object.
|
|
388
|
+
Creating a new object and setting that via onSetAuth might change the auth object too late.
|
|
389
|
+
Instead assign the new token directly to the property and then use the onSetAuth.
|
|
390
|
+
*/
|
|
391
|
+
// eslint-disable-next-line no-param-reassign
|
|
392
|
+
|
|
393
|
+
auth.token = newAuth.token; // eslint-disable-next-line no-param-reassign
|
|
394
|
+
|
|
395
|
+
auth.expires_at = newAuth.expires_at; // eslint-disable-next-line no-param-reassign
|
|
396
|
+
|
|
397
|
+
auth.refresh_token = newAuth.refresh_token || auth.refresh_token; // eslint-disable-next-line no-param-reassign
|
|
398
|
+
|
|
399
|
+
auth.keep_session_alive_at = newAuth.keep_session_alive_at;
|
|
400
|
+
onSetAuth(Object.assign(Object.assign({}, auth), {
|
|
401
|
+
has_connection_issue: false
|
|
402
|
+
}));
|
|
403
|
+
_context.next = 15;
|
|
404
|
+
break;
|
|
405
|
+
|
|
406
|
+
case 12:
|
|
407
|
+
_context.prev = 12;
|
|
408
|
+
_context.t0 = _context["catch"](0);
|
|
409
|
+
onSetAuth(Object.assign(Object.assign({}, auth), {
|
|
410
|
+
has_connection_issue: true
|
|
411
|
+
}));
|
|
412
|
+
|
|
413
|
+
case 15:
|
|
414
|
+
case "end":
|
|
415
|
+
return _context.stop();
|
|
416
|
+
}
|
|
363
417
|
}
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
418
|
+
}, _callee, null, [[0, 12]]);
|
|
419
|
+
}));
|
|
420
|
+
};
|
|
421
|
+
var createApiInstance = function createApiInstance(_ref3) {
|
|
422
|
+
var auth = _ref3.auth,
|
|
423
|
+
onSetAuth = _ref3.onSetAuth,
|
|
424
|
+
_ref3$config = _ref3.config;
|
|
425
|
+
_ref3$config = _ref3$config === void 0 ? {} : _ref3$config;
|
|
426
|
+
var baseURL = _ref3$config.baseURL,
|
|
427
|
+
authTokenURL = _ref3$config.authTokenURL,
|
|
428
|
+
authClientId = _ref3$config.authClientId,
|
|
429
|
+
appURL = _ref3$config.appURL,
|
|
430
|
+
_ref3$timeout = _ref3.timeout,
|
|
431
|
+
timeout = _ref3$timeout === void 0 ? DEFAULT_TIMEOUT : _ref3$timeout;
|
|
432
|
+
var axiosInstance = axios.create({
|
|
433
|
+
baseURL: baseURL,
|
|
434
|
+
headers: {},
|
|
435
|
+
timeout: timeout
|
|
436
|
+
}); // Request interceptor for API calls done BEFORE the request is made.
|
|
368
437
|
|
|
369
|
-
axiosInstance.interceptors.request.use(function (
|
|
370
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function
|
|
371
|
-
var timeInSecondsLeftBeforeExpiration,
|
|
372
|
-
return regeneratorRuntime.wrap(function
|
|
438
|
+
axiosInstance.interceptors.request.use(function (axiosConfig) {
|
|
439
|
+
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
|
|
440
|
+
var timeInSecondsLeftBeforeExpiration, newConfig;
|
|
441
|
+
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
373
442
|
while (1) {
|
|
374
|
-
switch (
|
|
443
|
+
switch (_context2.prev = _context2.next) {
|
|
375
444
|
case 0:
|
|
376
|
-
timeInSecondsLeftBeforeExpiration = auth && auth.expires_at ? auth.expires_at -
|
|
445
|
+
timeInSecondsLeftBeforeExpiration = auth && auth.expires_at ? auth.expires_at - getCurrentTimeInSeconds() : 0; // If expires_at is not set, don't do anything. (set timeInSecondsLeftBeforeExpiration = 0 will skip refresh)
|
|
377
446
|
|
|
378
447
|
if (!(timeInSecondsLeftBeforeExpiration < 0)) {
|
|
379
|
-
|
|
448
|
+
_context2.next = 4;
|
|
380
449
|
break;
|
|
381
450
|
}
|
|
382
451
|
|
|
383
|
-
|
|
384
|
-
return
|
|
452
|
+
_context2.next = 4;
|
|
453
|
+
return refreshAccessTokenAndSetAuthContext({
|
|
454
|
+
auth: auth,
|
|
455
|
+
onSetAuth: onSetAuth,
|
|
456
|
+
config: {
|
|
457
|
+
baseURL: baseURL,
|
|
458
|
+
authTokenURL: authTokenURL,
|
|
459
|
+
authClientId: authClientId,
|
|
460
|
+
appURL: appURL
|
|
461
|
+
},
|
|
462
|
+
timeout: timeout
|
|
463
|
+
});
|
|
385
464
|
|
|
386
465
|
case 4:
|
|
387
|
-
|
|
388
|
-
newAuth = makeCredentialsFromTokenResponse(refreshedToken);
|
|
389
|
-
/*
|
|
390
|
-
Note: Should not use the spread operator here on the auth object to create a new object.
|
|
391
|
-
Creating a new object and setting that via onSetAuth might change the auth object too late.
|
|
392
|
-
Instead assign the new token directly to the property and then use the onSetAuth.
|
|
393
|
-
*/
|
|
394
|
-
// eslint-disable-next-line no-param-reassign
|
|
395
|
-
|
|
396
|
-
auth.token = newAuth.token; // eslint-disable-next-line no-param-reassign
|
|
397
|
-
|
|
398
|
-
auth.expires_at = newAuth.expires_at;
|
|
399
|
-
onSetAuth(auth);
|
|
400
|
-
|
|
401
|
-
case 9:
|
|
402
|
-
newConfig = Object.assign(Object.assign({}, config), {
|
|
466
|
+
newConfig = Object.assign(Object.assign({}, axiosConfig), {
|
|
403
467
|
headers: Object.assign({
|
|
404
468
|
'Content-Type': 'application/json',
|
|
405
469
|
Authorization: "Bearer ".concat(auth.token),
|
|
406
470
|
Accept: 'application/json'
|
|
407
|
-
},
|
|
471
|
+
}, axiosConfig.headers)
|
|
408
472
|
});
|
|
409
|
-
return
|
|
473
|
+
return _context2.abrupt("return", newConfig);
|
|
410
474
|
|
|
411
|
-
case
|
|
475
|
+
case 6:
|
|
412
476
|
case "end":
|
|
413
|
-
return
|
|
477
|
+
return _context2.stop();
|
|
414
478
|
}
|
|
415
479
|
}
|
|
416
|
-
},
|
|
480
|
+
}, _callee2);
|
|
417
481
|
}));
|
|
418
482
|
}, function (error) {
|
|
419
483
|
Promise.reject(error);
|
|
@@ -422,56 +486,55 @@ var createApiInstance = function createApiInstance(_ref) {
|
|
|
422
486
|
axiosInstance.interceptors.response.use(function (response) {
|
|
423
487
|
return response;
|
|
424
488
|
}, function (error) {
|
|
425
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function
|
|
426
|
-
var originalRequest
|
|
427
|
-
return regeneratorRuntime.wrap(function
|
|
489
|
+
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
|
|
490
|
+
var originalRequest;
|
|
491
|
+
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
428
492
|
while (1) {
|
|
429
|
-
switch (
|
|
493
|
+
switch (_context3.prev = _context3.next) {
|
|
430
494
|
case 0:
|
|
431
495
|
originalRequest = error.config;
|
|
432
496
|
|
|
433
497
|
if (!(error.response && error.response.status && (error.response.status === 401 || error.response.status === 403) && !originalRequest.inRetry)) {
|
|
434
|
-
|
|
498
|
+
_context3.next = 6;
|
|
435
499
|
break;
|
|
436
500
|
}
|
|
437
501
|
|
|
438
502
|
originalRequest.inRetry = true;
|
|
439
|
-
|
|
440
|
-
return
|
|
503
|
+
_context3.next = 5;
|
|
504
|
+
return refreshAccessTokenAndSetAuthContext({
|
|
505
|
+
auth: auth,
|
|
506
|
+
onSetAuth: onSetAuth,
|
|
507
|
+
config: {
|
|
508
|
+
baseURL: baseURL,
|
|
509
|
+
authTokenURL: authTokenURL,
|
|
510
|
+
authClientId: authClientId,
|
|
511
|
+
appURL: appURL
|
|
512
|
+
},
|
|
513
|
+
timeout: timeout
|
|
514
|
+
});
|
|
441
515
|
|
|
442
516
|
case 5:
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
*/
|
|
450
|
-
// eslint-disable-next-line no-param-reassign
|
|
451
|
-
|
|
452
|
-
auth.token = newAuth.token;
|
|
453
|
-
onSetAuth(auth);
|
|
454
|
-
return _context2.abrupt("return", axiosInstance(originalRequest));
|
|
455
|
-
|
|
456
|
-
case 10:
|
|
457
|
-
return _context2.abrupt("return", Promise.reject(error));
|
|
458
|
-
|
|
459
|
-
case 11:
|
|
517
|
+
return _context3.abrupt("return", axiosInstance(originalRequest));
|
|
518
|
+
|
|
519
|
+
case 6:
|
|
520
|
+
return _context3.abrupt("return", Promise.reject(error));
|
|
521
|
+
|
|
522
|
+
case 7:
|
|
460
523
|
case "end":
|
|
461
|
-
return
|
|
524
|
+
return _context3.stop();
|
|
462
525
|
}
|
|
463
526
|
}
|
|
464
|
-
},
|
|
527
|
+
}, _callee3);
|
|
465
528
|
}));
|
|
466
529
|
});
|
|
467
530
|
return axiosInstance;
|
|
468
531
|
};
|
|
469
|
-
var createNonAuthApiInstance = function createNonAuthApiInstance(
|
|
470
|
-
var
|
|
471
|
-
|
|
472
|
-
var baseURL =
|
|
473
|
-
|
|
474
|
-
timeout =
|
|
532
|
+
var createNonAuthApiInstance = function createNonAuthApiInstance(_ref4) {
|
|
533
|
+
var _ref4$config = _ref4.config;
|
|
534
|
+
_ref4$config = _ref4$config === void 0 ? {} : _ref4$config;
|
|
535
|
+
var baseURL = _ref4$config.baseURL,
|
|
536
|
+
_ref4$timeout = _ref4.timeout,
|
|
537
|
+
timeout = _ref4$timeout === void 0 ? DEFAULT_TIMEOUT : _ref4$timeout;
|
|
475
538
|
var axiosInstance = axios.create({
|
|
476
539
|
baseURL: baseURL,
|
|
477
540
|
headers: {},
|
|
@@ -479,11 +542,11 @@ var createNonAuthApiInstance = function createNonAuthApiInstance(_ref2) {
|
|
|
479
542
|
}); // Request interceptor for API calls
|
|
480
543
|
|
|
481
544
|
axiosInstance.interceptors.request.use(function (config) {
|
|
482
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function
|
|
545
|
+
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
|
|
483
546
|
var newConfig;
|
|
484
|
-
return regeneratorRuntime.wrap(function
|
|
547
|
+
return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
485
548
|
while (1) {
|
|
486
|
-
switch (
|
|
549
|
+
switch (_context4.prev = _context4.next) {
|
|
487
550
|
case 0:
|
|
488
551
|
newConfig = Object.assign(Object.assign({}, config), {
|
|
489
552
|
headers: Object.assign({
|
|
@@ -491,14 +554,14 @@ var createNonAuthApiInstance = function createNonAuthApiInstance(_ref2) {
|
|
|
491
554
|
Accept: 'application/json'
|
|
492
555
|
}, config.headers)
|
|
493
556
|
});
|
|
494
|
-
return
|
|
557
|
+
return _context4.abrupt("return", newConfig);
|
|
495
558
|
|
|
496
559
|
case 2:
|
|
497
560
|
case "end":
|
|
498
|
-
return
|
|
561
|
+
return _context4.stop();
|
|
499
562
|
}
|
|
500
563
|
}
|
|
501
|
-
},
|
|
564
|
+
}, _callee4);
|
|
502
565
|
}));
|
|
503
566
|
}, function (error) {
|
|
504
567
|
Promise.reject(error);
|
|
@@ -540,5 +603,8 @@ var createFakeApiInstance = function createFakeApiInstance() {
|
|
|
540
603
|
}
|
|
541
604
|
};
|
|
542
605
|
};
|
|
606
|
+
var getCurrentTimeInSeconds = function getCurrentTimeInSeconds() {
|
|
607
|
+
return Date.now() * MILLISECOND_TO_SECOND;
|
|
608
|
+
};
|
|
543
609
|
|
|
544
|
-
export { ApiProvider, MILLISECOND_TO_SECOND, REFRESH_TOKEN_WHEN_PCT_EXPIRED, createApiInstance, createFakeApiInstance, createNonAuthApiInstance, fakeApiRequest, getApi, makeCredentialsFromTokenResponse, useApi, useApiContext, useDebounce };
|
|
610
|
+
export { ApiProvider, KEEP_ALIVE_IN_SECONDS, KEEP_ALIVE_POLLER_IN_SECONDS, MILLISECOND_TO_SECOND, REFRESH_TOKEN_WHEN_PCT_EXPIRED, createApiInstance, createFakeApiInstance, createNonAuthApiInstance, fakeApiRequest, getApi, getCurrentTimeInSeconds, makeCredentialsFromTokenResponse, refreshAccessToken, refreshAccessTokenAndSetAuthContext, useApi, useApiContext, useDebounce };
|
package/index.umd.js
CHANGED
|
@@ -308,6 +308,10 @@
|
|
|
308
308
|
}
|
|
309
309
|
|
|
310
310
|
var DEFAULT_TIMEOUT = 15000;
|
|
311
|
+
var KEEP_ALIVE_IN_SECONDS = 60; // Number of seconds in between intervals to check with the token request if connection is stil intact
|
|
312
|
+
|
|
313
|
+
var KEEP_ALIVE_POLLER_IN_SECONDS = 10; // Number of milliseconds to check if connection is restored or to check if KEEP_ALIVE_IN_SECONDS has passed
|
|
314
|
+
|
|
311
315
|
var REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75; // Refresh token when 75% expired. Set to (10 / 3600) * 100 = 0.2777778% to test with 10 second interval.
|
|
312
316
|
|
|
313
317
|
var DEFAULT_TOKEN_EXPIRES_IN = 3600; // Number of seconds a token expires by default
|
|
@@ -339,25 +343,122 @@
|
|
|
339
343
|
var tokenExpirationInSeconds = expires_in && expires_in > 0 ? expires_in : DEFAULT_TOKEN_EXPIRES_IN; // If not set or zero in the token, assume valid for 1 hour.
|
|
340
344
|
|
|
341
345
|
var pctTokenExpirationInSeconds = tokenExpirationInSeconds * (REFRESH_TOKEN_WHEN_PCT_EXPIRED / 100);
|
|
342
|
-
var epochTimeTokenExpirationInSeconds = pctTokenExpirationInSeconds +
|
|
346
|
+
var epochTimeTokenExpirationInSeconds = pctTokenExpirationInSeconds + getCurrentTimeInSeconds();
|
|
343
347
|
var isAuthCognito = userInfo.iss.includes('cognito');
|
|
344
348
|
var newAuth = {
|
|
345
349
|
username: isAuthCognito ? userInfo['cognito:username'] : userInfo.email,
|
|
346
350
|
token: access_token,
|
|
347
351
|
refresh_token: refresh_token || '',
|
|
348
|
-
expires_at: epochTimeTokenExpirationInSeconds
|
|
352
|
+
expires_at: epochTimeTokenExpirationInSeconds,
|
|
353
|
+
keep_session_alive_at: getCurrentTimeInSeconds() + KEEP_ALIVE_IN_SECONDS
|
|
349
354
|
};
|
|
350
355
|
return newAuth;
|
|
351
356
|
};
|
|
357
|
+
var refreshAccessToken = function refreshAccessToken(_a) {
|
|
358
|
+
var auth = _a.auth,
|
|
359
|
+
_b = _a.config,
|
|
360
|
+
_c = _b === void 0 ? {} : _b,
|
|
361
|
+
authTokenURL = _c.authTokenURL,
|
|
362
|
+
authClientId = _c.authClientId,
|
|
363
|
+
appURL = _c.appURL,
|
|
364
|
+
_d = _a.timeout,
|
|
365
|
+
timeout = _d === void 0 ? DEFAULT_TIMEOUT : _d; // Refresh token request with a new axios instance
|
|
366
|
+
// without request interceptor
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
var tokenAxiosInstance = axios__default["default"].create({
|
|
370
|
+
headers: {},
|
|
371
|
+
timeout: timeout
|
|
372
|
+
});
|
|
373
|
+
var refreshPayload = {
|
|
374
|
+
refresh_token: auth.refresh_token,
|
|
375
|
+
redirect_uri: appURL + "/code",
|
|
376
|
+
grant_type: 'refresh_token',
|
|
377
|
+
client_id: authClientId
|
|
378
|
+
};
|
|
379
|
+
/* Send data in the "application/x-www-form-urlencoded" format.
|
|
380
|
+
If only JSON is supported, use Axios' default content type ("application/x-www-form-urlencoded"). */
|
|
381
|
+
|
|
382
|
+
var useDefaultContentType = authTokenURL.includes('amazonaws.com');
|
|
383
|
+
var data = useDefaultContentType ? refreshPayload : new URLSearchParams(refreshPayload);
|
|
384
|
+
var axiosConfig = {
|
|
385
|
+
headers: {
|
|
386
|
+
'Content-Type': useDefaultContentType ? 'application/json' : 'application/x-www-form-urlencoded'
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
return tokenAxiosInstance.post(authTokenURL, data, axiosConfig);
|
|
390
|
+
};
|
|
391
|
+
var refreshAccessTokenAndSetAuthContext = function refreshAccessTokenAndSetAuthContext(_a) {
|
|
392
|
+
var auth = _a.auth,
|
|
393
|
+
onSetAuth = _a.onSetAuth,
|
|
394
|
+
config = _a.config,
|
|
395
|
+
_b = _a.timeout,
|
|
396
|
+
timeout = _b === void 0 ? DEFAULT_TIMEOUT : _b;
|
|
397
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
398
|
+
var refreshedToken, newAuth;
|
|
399
|
+
return __generator(this, function (_c) {
|
|
400
|
+
switch (_c.label) {
|
|
401
|
+
case 0:
|
|
402
|
+
_c.trys.push([0, 2,, 3]);
|
|
403
|
+
|
|
404
|
+
return [4
|
|
405
|
+
/*yield*/
|
|
406
|
+
, refreshAccessToken({
|
|
407
|
+
auth: auth,
|
|
408
|
+
config: config,
|
|
409
|
+
timeout: timeout
|
|
410
|
+
})];
|
|
411
|
+
|
|
412
|
+
case 1:
|
|
413
|
+
refreshedToken = _c.sent();
|
|
414
|
+
newAuth = makeCredentialsFromTokenResponse(refreshedToken);
|
|
415
|
+
/*
|
|
416
|
+
Note: Should not use the spread operator here on the auth object to create a new object.
|
|
417
|
+
Creating a new object and setting that via onSetAuth might change the auth object too late.
|
|
418
|
+
Instead assign the new token directly to the property and then use the onSetAuth.
|
|
419
|
+
*/
|
|
420
|
+
// eslint-disable-next-line no-param-reassign
|
|
421
|
+
|
|
422
|
+
auth.token = newAuth.token; // eslint-disable-next-line no-param-reassign
|
|
423
|
+
|
|
424
|
+
auth.expires_at = newAuth.expires_at; // eslint-disable-next-line no-param-reassign
|
|
425
|
+
|
|
426
|
+
auth.refresh_token = newAuth.refresh_token || auth.refresh_token; // eslint-disable-next-line no-param-reassign
|
|
427
|
+
|
|
428
|
+
auth.keep_session_alive_at = newAuth.keep_session_alive_at;
|
|
429
|
+
onSetAuth(__assign(__assign({}, auth), {
|
|
430
|
+
has_connection_issue: false
|
|
431
|
+
}));
|
|
432
|
+
return [3
|
|
433
|
+
/*break*/
|
|
434
|
+
, 3];
|
|
435
|
+
|
|
436
|
+
case 2:
|
|
437
|
+
_c.sent();
|
|
438
|
+
onSetAuth(__assign(__assign({}, auth), {
|
|
439
|
+
has_connection_issue: true
|
|
440
|
+
}));
|
|
441
|
+
return [3
|
|
442
|
+
/*break*/
|
|
443
|
+
, 3];
|
|
444
|
+
|
|
445
|
+
case 3:
|
|
446
|
+
return [2
|
|
447
|
+
/*return*/
|
|
448
|
+
];
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
};
|
|
352
453
|
var createApiInstance = function createApiInstance(_a) {
|
|
353
454
|
var auth = _a.auth,
|
|
354
455
|
onSetAuth = _a.onSetAuth,
|
|
355
456
|
_b = _a.config,
|
|
356
457
|
_c = _b === void 0 ? {} : _b,
|
|
357
458
|
baseURL = _c.baseURL,
|
|
358
|
-
appURL = _c.appURL,
|
|
359
459
|
authTokenURL = _c.authTokenURL,
|
|
360
460
|
authClientId = _c.authClientId,
|
|
461
|
+
appURL = _c.appURL,
|
|
361
462
|
_d = _a.timeout,
|
|
362
463
|
timeout = _d === void 0 ? DEFAULT_TIMEOUT : _d;
|
|
363
464
|
|
|
@@ -365,72 +466,44 @@
|
|
|
365
466
|
baseURL: baseURL,
|
|
366
467
|
headers: {},
|
|
367
468
|
timeout: timeout
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
var refreshAccessToken = function refreshAccessToken() {
|
|
371
|
-
// Refresh token request with a new axios instance
|
|
372
|
-
// without request interceptor
|
|
373
|
-
var tokenAxiosInstance = axios__default["default"].create({
|
|
374
|
-
headers: {},
|
|
375
|
-
timeout: timeout
|
|
376
|
-
});
|
|
377
|
-
var refreshPayload = {
|
|
378
|
-
refresh_token: auth.refresh_token,
|
|
379
|
-
redirect_uri: appURL + "/code",
|
|
380
|
-
grant_type: 'refresh_token',
|
|
381
|
-
client_id: authClientId
|
|
382
|
-
};
|
|
383
|
-
/* Send data in the "application/x-www-form-urlencoded" format.
|
|
384
|
-
If only JSON is supported, use Axios' default content type ("application/x-www-form-urlencoded"). */
|
|
385
|
-
|
|
386
|
-
var useDefaultContentType = authTokenURL.includes('amazonaws.com');
|
|
387
|
-
var data = useDefaultContentType ? refreshPayload : new URLSearchParams(refreshPayload);
|
|
388
|
-
var config = {
|
|
389
|
-
headers: {
|
|
390
|
-
'Content-Type': useDefaultContentType ? 'application/json' : 'application/x-www-form-urlencoded'
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
return tokenAxiosInstance.post(authTokenURL, data, config);
|
|
394
|
-
}; // Request interceptor for API calls done BEFORE the request is made.
|
|
395
|
-
|
|
469
|
+
}); // Request interceptor for API calls done BEFORE the request is made.
|
|
396
470
|
|
|
397
|
-
axiosInstance.interceptors.request.use(function (
|
|
471
|
+
axiosInstance.interceptors.request.use(function (axiosConfig) {
|
|
398
472
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
399
|
-
var timeInSecondsLeftBeforeExpiration,
|
|
473
|
+
var timeInSecondsLeftBeforeExpiration, newConfig;
|
|
400
474
|
return __generator(this, function (_a) {
|
|
401
475
|
switch (_a.label) {
|
|
402
476
|
case 0:
|
|
403
|
-
timeInSecondsLeftBeforeExpiration = auth && auth.expires_at ? auth.expires_at -
|
|
477
|
+
timeInSecondsLeftBeforeExpiration = auth && auth.expires_at ? auth.expires_at - getCurrentTimeInSeconds() : 0;
|
|
404
478
|
if (!(timeInSecondsLeftBeforeExpiration < 0)) return [3
|
|
405
479
|
/*break*/
|
|
406
480
|
, 2];
|
|
407
481
|
return [4
|
|
408
482
|
/*yield*/
|
|
409
|
-
,
|
|
483
|
+
, refreshAccessTokenAndSetAuthContext({
|
|
484
|
+
auth: auth,
|
|
485
|
+
onSetAuth: onSetAuth,
|
|
486
|
+
config: {
|
|
487
|
+
baseURL: baseURL,
|
|
488
|
+
authTokenURL: authTokenURL,
|
|
489
|
+
authClientId: authClientId,
|
|
490
|
+
appURL: appURL
|
|
491
|
+
},
|
|
492
|
+
timeout: timeout
|
|
493
|
+
})];
|
|
410
494
|
|
|
411
495
|
case 1:
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
/*
|
|
415
|
-
Note: Should not use the spread operator here on the auth object to create a new object.
|
|
416
|
-
Creating a new object and setting that via onSetAuth might change the auth object too late.
|
|
417
|
-
Instead assign the new token directly to the property and then use the onSetAuth.
|
|
418
|
-
*/
|
|
419
|
-
// eslint-disable-next-line no-param-reassign
|
|
420
|
-
|
|
421
|
-
auth.token = newAuth.token; // eslint-disable-next-line no-param-reassign
|
|
422
|
-
|
|
423
|
-
auth.expires_at = newAuth.expires_at;
|
|
424
|
-
onSetAuth(auth);
|
|
496
|
+
_a.sent();
|
|
497
|
+
|
|
425
498
|
_a.label = 2;
|
|
426
499
|
|
|
427
500
|
case 2:
|
|
428
|
-
newConfig = __assign(__assign({},
|
|
501
|
+
newConfig = __assign(__assign({}, axiosConfig), {
|
|
429
502
|
headers: __assign({
|
|
430
503
|
'Content-Type': 'application/json',
|
|
431
504
|
Authorization: "Bearer " + auth.token,
|
|
432
505
|
Accept: 'application/json'
|
|
433
|
-
},
|
|
506
|
+
}, axiosConfig.headers)
|
|
434
507
|
});
|
|
435
508
|
return [2
|
|
436
509
|
/*return*/
|
|
@@ -446,7 +519,7 @@
|
|
|
446
519
|
return response;
|
|
447
520
|
}, function (error) {
|
|
448
521
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
449
|
-
var originalRequest
|
|
522
|
+
var originalRequest;
|
|
450
523
|
return __generator(this, function (_a) {
|
|
451
524
|
switch (_a.label) {
|
|
452
525
|
case 0:
|
|
@@ -457,20 +530,21 @@
|
|
|
457
530
|
originalRequest.inRetry = true;
|
|
458
531
|
return [4
|
|
459
532
|
/*yield*/
|
|
460
|
-
,
|
|
533
|
+
, refreshAccessTokenAndSetAuthContext({
|
|
534
|
+
auth: auth,
|
|
535
|
+
onSetAuth: onSetAuth,
|
|
536
|
+
config: {
|
|
537
|
+
baseURL: baseURL,
|
|
538
|
+
authTokenURL: authTokenURL,
|
|
539
|
+
authClientId: authClientId,
|
|
540
|
+
appURL: appURL
|
|
541
|
+
},
|
|
542
|
+
timeout: timeout
|
|
543
|
+
})];
|
|
461
544
|
|
|
462
545
|
case 1:
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
/*
|
|
466
|
-
Note: Should not use the spread operator here on the auth object to create a new object.
|
|
467
|
-
Creating a new object and setting that via onSetAuth might change the auth object too late.
|
|
468
|
-
Instead assign the new token directly to the property and then use the onSetAuth.
|
|
469
|
-
*/
|
|
470
|
-
// eslint-disable-next-line no-param-reassign
|
|
471
|
-
|
|
472
|
-
auth.token = newAuth.token;
|
|
473
|
-
onSetAuth(auth);
|
|
546
|
+
_a.sent();
|
|
547
|
+
|
|
474
548
|
return [2
|
|
475
549
|
/*return*/
|
|
476
550
|
, axiosInstance(originalRequest)];
|
|
@@ -553,8 +627,13 @@
|
|
|
553
627
|
}
|
|
554
628
|
};
|
|
555
629
|
};
|
|
630
|
+
var getCurrentTimeInSeconds = function getCurrentTimeInSeconds() {
|
|
631
|
+
return Date.now() * MILLISECOND_TO_SECOND;
|
|
632
|
+
};
|
|
556
633
|
|
|
557
634
|
exports.ApiProvider = ApiProvider;
|
|
635
|
+
exports.KEEP_ALIVE_IN_SECONDS = KEEP_ALIVE_IN_SECONDS;
|
|
636
|
+
exports.KEEP_ALIVE_POLLER_IN_SECONDS = KEEP_ALIVE_POLLER_IN_SECONDS;
|
|
558
637
|
exports.MILLISECOND_TO_SECOND = MILLISECOND_TO_SECOND;
|
|
559
638
|
exports.REFRESH_TOKEN_WHEN_PCT_EXPIRED = REFRESH_TOKEN_WHEN_PCT_EXPIRED;
|
|
560
639
|
exports.createApiInstance = createApiInstance;
|
|
@@ -562,7 +641,10 @@
|
|
|
562
641
|
exports.createNonAuthApiInstance = createNonAuthApiInstance;
|
|
563
642
|
exports.fakeApiRequest = fakeApiRequest;
|
|
564
643
|
exports.getApi = getApi;
|
|
644
|
+
exports.getCurrentTimeInSeconds = getCurrentTimeInSeconds;
|
|
565
645
|
exports.makeCredentialsFromTokenResponse = makeCredentialsFromTokenResponse;
|
|
646
|
+
exports.refreshAccessToken = refreshAccessToken;
|
|
647
|
+
exports.refreshAccessTokenAndSetAuthContext = refreshAccessTokenAndSetAuthContext;
|
|
566
648
|
exports.useApi = useApi;
|
|
567
649
|
exports.useApiContext = useApiContext;
|
|
568
650
|
exports.useDebounce = useDebounce;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
2
|
import { CreateApiProps, Credentials } from './types';
|
|
3
|
+
export declare const KEEP_ALIVE_IN_SECONDS = 60;
|
|
4
|
+
export declare const KEEP_ALIVE_POLLER_IN_SECONDS = 10;
|
|
3
5
|
export declare const REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75;
|
|
4
6
|
export declare const MILLISECOND_TO_SECOND: number;
|
|
5
7
|
/**
|
|
@@ -11,7 +13,10 @@ export declare const MILLISECOND_TO_SECOND: number;
|
|
|
11
13
|
* @returns Credentials object.
|
|
12
14
|
*/
|
|
13
15
|
export declare const makeCredentialsFromTokenResponse: (tokenResponse: AxiosResponse) => Credentials;
|
|
14
|
-
export declare const
|
|
16
|
+
export declare const refreshAccessToken: ({ auth, config: { authTokenURL, authClientId, appURL }, timeout, }: CreateApiProps) => Promise<AxiosResponse>;
|
|
17
|
+
export declare const refreshAccessTokenAndSetAuthContext: ({ auth, onSetAuth, config, timeout, }: CreateApiProps) => Promise<void>;
|
|
18
|
+
export declare const createApiInstance: ({ auth, onSetAuth, config: { baseURL, authTokenURL, authClientId, appURL }, timeout, }: CreateApiProps) => AxiosInstance;
|
|
15
19
|
export declare const createNonAuthApiInstance: ({ config: { baseURL }, timeout, }: CreateApiProps) => AxiosInstance;
|
|
16
20
|
export declare const fakeApiRequest: (signal?: AbortController | undefined) => Promise<void>;
|
|
17
21
|
export declare const createFakeApiInstance: () => AxiosInstance;
|
|
22
|
+
export declare const getCurrentTimeInSeconds: () => number;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengeoweb/api",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.13.1",
|
|
4
4
|
"description": "GeoWeb API library for the opengeoweb project",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -14,14 +14,14 @@
|
|
|
14
14
|
"peerDependencies": {
|
|
15
15
|
"react": "^18.2.0",
|
|
16
16
|
"axios": "^0.25.0",
|
|
17
|
-
"@opengeoweb/shared": "4.
|
|
17
|
+
"@opengeoweb/shared": "4.13.1",
|
|
18
18
|
"@mui/material": "^5.10.8",
|
|
19
|
-
"@opengeoweb/theme": "4.
|
|
19
|
+
"@opengeoweb/theme": "4.13.1",
|
|
20
20
|
"react-draggable": "^4.4.5",
|
|
21
21
|
"re-resizable": "^6.9.0",
|
|
22
22
|
"@redux-eggs/core": "^2.2.0",
|
|
23
23
|
"react-redux": "^8.0.4",
|
|
24
|
-
"@opengeoweb/form-fields": "4.
|
|
24
|
+
"@opengeoweb/form-fields": "4.13.1",
|
|
25
25
|
"react-hook-form": "^7.41.5",
|
|
26
26
|
"moment": "^2.29.0",
|
|
27
27
|
"moment-timezone": "^0.5.31",
|