@opengeoweb/api 4.21.0 → 4.22.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/index.js +831 -0
- package/package.json +8 -17
- package/index.esm.js +0 -594
- package/index.umd.js +0 -638
- /package/{index.d.ts → src/index.d.ts} +0 -0
- /package/{lib → src/lib}/components/ApiContext/ApiContext.d.ts +0 -0
- /package/{lib → src/lib}/components/ApiContext/ApiContext.spec.d.ts +0 -0
- /package/{lib → src/lib}/components/ApiContext/index.d.ts +0 -0
- /package/{lib → src/lib}/components/ApiContext/types.d.ts +0 -0
- /package/{lib → src/lib}/components/ApiContext/utils.d.ts +0 -0
- /package/{lib → src/lib}/components/ApiContext/utils.spec.d.ts +0 -0
- /package/{lib → src/lib}/components/apihooks/index.d.ts +0 -0
- /package/{lib → src/lib}/components/apihooks/useApi.d.ts +0 -0
- /package/{lib → src/lib}/components/apihooks/useApi.spec.d.ts +0 -0
- /package/{lib → src/lib}/components/apihooks/useDebounce.d.ts +0 -0
package/package.json
CHANGED
|
@@ -1,29 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengeoweb/api",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.22.1",
|
|
4
4
|
"description": "GeoWeb API library for the opengeoweb project",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git@gitlab.com:opengeoweb/opengeoweb.git"
|
|
9
9
|
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
10
|
+
"module": "./index.js",
|
|
11
|
+
"main": "./index.js",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"types": "./src/index.d.ts",
|
|
13
14
|
"dependencies": {},
|
|
14
15
|
"peerDependencies": {
|
|
15
|
-
"
|
|
16
|
-
"axios": "
|
|
17
|
-
"
|
|
18
|
-
"@mui/material": "^5.12.0",
|
|
19
|
-
"@opengeoweb/theme": "4.21.0",
|
|
20
|
-
"@mui/system": "^5.12.0",
|
|
21
|
-
"@mui/x-date-pickers": "^6.2.1",
|
|
22
|
-
"react-draggable": "^4.4.5",
|
|
23
|
-
"re-resizable": "^6.9.0",
|
|
24
|
-
"lodash": "^4.17.20",
|
|
25
|
-
"@redux-eggs/core": "^2.2.0",
|
|
26
|
-
"react-redux": "^8.0.4",
|
|
27
|
-
"date-fns": "^2.29.3"
|
|
16
|
+
"@opengeoweb/shared": "4.22.1",
|
|
17
|
+
"axios": "1.4.0",
|
|
18
|
+
"react": "18.2.0"
|
|
28
19
|
}
|
|
29
20
|
}
|
package/index.esm.js
DELETED
|
@@ -1,594 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import axios from 'axios';
|
|
3
|
-
import { useIsMounted } from '@opengeoweb/shared';
|
|
4
|
-
|
|
5
|
-
function _slicedToArray(arr, i) {
|
|
6
|
-
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function _arrayWithHoles(arr) {
|
|
10
|
-
if (Array.isArray(arr)) return arr;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function _iterableToArrayLimit(arr, i) {
|
|
14
|
-
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
|
|
15
|
-
var _arr = [];
|
|
16
|
-
var _n = true;
|
|
17
|
-
var _d = false;
|
|
18
|
-
var _e = undefined;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
|
22
|
-
_arr.push(_s.value);
|
|
23
|
-
|
|
24
|
-
if (i && _arr.length === i) break;
|
|
25
|
-
}
|
|
26
|
-
} catch (err) {
|
|
27
|
-
_d = true;
|
|
28
|
-
_e = err;
|
|
29
|
-
} finally {
|
|
30
|
-
try {
|
|
31
|
-
if (!_n && _i["return"] != null) _i["return"]();
|
|
32
|
-
} finally {
|
|
33
|
-
if (_d) throw _e;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return _arr;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function _unsupportedIterableToArray(o, minLen) {
|
|
41
|
-
if (!o) return;
|
|
42
|
-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
43
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
44
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
45
|
-
if (n === "Map" || n === "Set") return Array.from(o);
|
|
46
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function _arrayLikeToArray(arr, len) {
|
|
50
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
51
|
-
|
|
52
|
-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
53
|
-
|
|
54
|
-
return arr2;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function _nonIterableRest() {
|
|
58
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/*
|
|
62
|
-
Accepts a value and timeout and will debounce
|
|
63
|
-
|
|
64
|
-
@param {string} value: value of element
|
|
65
|
-
@param {timeout} number: timer
|
|
66
|
-
@return {value: string}
|
|
67
|
-
|
|
68
|
-
@example:
|
|
69
|
-
const debouncedName = useDebounce('testing', 2000)
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
var useDebounce = function useDebounce(value, timeout) {
|
|
73
|
-
var _React$useState = React.useState(value),
|
|
74
|
-
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
75
|
-
state = _React$useState2[0],
|
|
76
|
-
setState = _React$useState2[1];
|
|
77
|
-
|
|
78
|
-
React.useEffect(function () {
|
|
79
|
-
var handler = setTimeout(function () {
|
|
80
|
-
return setState(value);
|
|
81
|
-
}, timeout);
|
|
82
|
-
return function () {
|
|
83
|
-
return clearTimeout(handler);
|
|
84
|
-
};
|
|
85
|
-
}, [value, timeout]);
|
|
86
|
-
return state;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
/*! *****************************************************************************
|
|
90
|
-
Copyright (c) Microsoft Corporation.
|
|
91
|
-
|
|
92
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
93
|
-
purpose with or without fee is hereby granted.
|
|
94
|
-
|
|
95
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
96
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
97
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
98
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
99
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
100
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
101
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
102
|
-
***************************************************************************** */
|
|
103
|
-
|
|
104
|
-
function __rest(s, e) {
|
|
105
|
-
var t = {};
|
|
106
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
107
|
-
t[p] = s[p];
|
|
108
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
109
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
110
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
111
|
-
t[p[i]] = s[p[i]];
|
|
112
|
-
}
|
|
113
|
-
return t;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
117
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
118
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
119
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
120
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
121
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
122
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
var generateRandomId = function generateRandomId() {
|
|
127
|
-
return "-".concat(Math.random().toString(36).substr(2, 9));
|
|
128
|
-
}; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
var useApi = function useApi(apiCall) {
|
|
132
|
-
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
133
|
-
|
|
134
|
-
var _useIsMounted = useIsMounted(),
|
|
135
|
-
isMounted = _useIsMounted.isMounted;
|
|
136
|
-
|
|
137
|
-
var _React$useState = React.useState(generateRandomId()),
|
|
138
|
-
_React$useState2 = _slicedToArray(_React$useState, 1),
|
|
139
|
-
id = _React$useState2[0];
|
|
140
|
-
|
|
141
|
-
var _React$useState3 = React.useState(true),
|
|
142
|
-
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
|
143
|
-
isLoading = _React$useState4[0],
|
|
144
|
-
setIsloading = _React$useState4[1];
|
|
145
|
-
|
|
146
|
-
var _React$useState5 = React.useState(null),
|
|
147
|
-
_React$useState6 = _slicedToArray(_React$useState5, 2),
|
|
148
|
-
error = _React$useState6[0],
|
|
149
|
-
setError = _React$useState6[1];
|
|
150
|
-
|
|
151
|
-
var _React$useState7 = React.useState(null),
|
|
152
|
-
_React$useState8 = _slicedToArray(_React$useState7, 2),
|
|
153
|
-
result = _React$useState8[0],
|
|
154
|
-
setResult = _React$useState8[1];
|
|
155
|
-
|
|
156
|
-
var debouncedParams = useDebounce(JSON.stringify(params), 300);
|
|
157
|
-
|
|
158
|
-
var fetchApiData = function fetchApiData() {
|
|
159
|
-
var _params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
160
|
-
|
|
161
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
162
|
-
var response;
|
|
163
|
-
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
164
|
-
while (1) {
|
|
165
|
-
switch (_context.prev = _context.next) {
|
|
166
|
-
case 0:
|
|
167
|
-
_context.prev = 0;
|
|
168
|
-
setIsloading(true);
|
|
169
|
-
setError(null);
|
|
170
|
-
_context.next = 5;
|
|
171
|
-
return apiCall(_params, id);
|
|
172
|
-
|
|
173
|
-
case 5:
|
|
174
|
-
response = _context.sent;
|
|
175
|
-
|
|
176
|
-
if (isMounted.current) {
|
|
177
|
-
setResult(Array.isArray(response) ? response.map(function (d) {
|
|
178
|
-
return d.data;
|
|
179
|
-
}) : response.data);
|
|
180
|
-
setIsloading(false);
|
|
181
|
-
setError(null);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
_context.next = 12;
|
|
185
|
-
break;
|
|
186
|
-
|
|
187
|
-
case 9:
|
|
188
|
-
_context.prev = 9;
|
|
189
|
-
_context.t0 = _context["catch"](0);
|
|
190
|
-
|
|
191
|
-
if (isMounted.current) {
|
|
192
|
-
if (axios.isCancel(_context.t0)) {
|
|
193
|
-
setIsloading(false);
|
|
194
|
-
} else {
|
|
195
|
-
setError(_context.t0);
|
|
196
|
-
setIsloading(false);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
case 12:
|
|
201
|
-
case "end":
|
|
202
|
-
return _context.stop();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}, _callee, null, [[0, 9]]);
|
|
206
|
-
}));
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
React.useEffect(function () {
|
|
210
|
-
fetchApiData(params);
|
|
211
|
-
}, // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
212
|
-
[debouncedParams]);
|
|
213
|
-
|
|
214
|
-
var clearResults = function clearResults() {
|
|
215
|
-
setIsloading(true);
|
|
216
|
-
setResult(null);
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
return {
|
|
220
|
-
isLoading: isLoading,
|
|
221
|
-
error: error,
|
|
222
|
-
result: result,
|
|
223
|
-
clearResults: clearResults,
|
|
224
|
-
fetchApiData: fetchApiData
|
|
225
|
-
};
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
var ApiContext = /*#__PURE__*/React.createContext({
|
|
229
|
-
api: null
|
|
230
|
-
});
|
|
231
|
-
var registeredApis = new Map();
|
|
232
|
-
|
|
233
|
-
function registerApi(name, api) {
|
|
234
|
-
registeredApis.set(name, api);
|
|
235
|
-
} // eslint-disable-next-line no-unused-vars
|
|
236
|
-
|
|
237
|
-
function getApi(name) {
|
|
238
|
-
return registeredApis.get(name);
|
|
239
|
-
}
|
|
240
|
-
var ApiProvider = function ApiProvider(_a) {
|
|
241
|
-
var children = _a.children,
|
|
242
|
-
createApi = _a.createApi,
|
|
243
|
-
name = _a.name,
|
|
244
|
-
apiModuleProps = __rest(_a, ["children", "createApi", "name"]);
|
|
245
|
-
|
|
246
|
-
var handleNewApi = React.useCallback(function () {
|
|
247
|
-
var newApi = createApi(apiModuleProps);
|
|
248
|
-
|
|
249
|
-
if (name) {
|
|
250
|
-
registerApi(name, newApi);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return newApi; // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
254
|
-
}, [createApi, name]);
|
|
255
|
-
|
|
256
|
-
var _React$useState = React.useState(handleNewApi),
|
|
257
|
-
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
258
|
-
api = _React$useState2[0],
|
|
259
|
-
setApi = _React$useState2[1];
|
|
260
|
-
|
|
261
|
-
React.useEffect(function () {
|
|
262
|
-
setApi(handleNewApi);
|
|
263
|
-
return function () {
|
|
264
|
-
};
|
|
265
|
-
}, [handleNewApi, name]);
|
|
266
|
-
return /*#__PURE__*/React.createElement(ApiContext.Provider, {
|
|
267
|
-
value: {
|
|
268
|
-
api: api
|
|
269
|
-
}
|
|
270
|
-
}, children);
|
|
271
|
-
};
|
|
272
|
-
function useApiContext() {
|
|
273
|
-
return React.useContext(ApiContext);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
var DEFAULT_TIMEOUT = 15000;
|
|
277
|
-
var KEEP_ALIVE_IN_SECONDS = 60; // Number of seconds in between intervals to check with the token request if connection is stil intact
|
|
278
|
-
|
|
279
|
-
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
|
|
280
|
-
|
|
281
|
-
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.
|
|
282
|
-
|
|
283
|
-
var DEFAULT_TOKEN_EXPIRES_IN = 3600; // Number of seconds a token expires by default
|
|
284
|
-
|
|
285
|
-
var MILLISECOND_TO_SECOND = 1 / 1000;
|
|
286
|
-
/**
|
|
287
|
-
* Creates a Credentials object based on the axios response from the token service.
|
|
288
|
-
*
|
|
289
|
-
* It will calculate the expires_at attribute based on the expires_in property found in the JWT.
|
|
290
|
-
*
|
|
291
|
-
* @param tokenResponse Response of the tokenservice as axios response object.
|
|
292
|
-
* @returns Credentials object.
|
|
293
|
-
*/
|
|
294
|
-
|
|
295
|
-
var makeCredentialsFromTokenResponse = function makeCredentialsFromTokenResponse(tokenResponse) {
|
|
296
|
-
var token = tokenResponse.data.body || tokenResponse.data; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
297
|
-
|
|
298
|
-
var access_token = token.access_token,
|
|
299
|
-
refresh_token = token.refresh_token,
|
|
300
|
-
id_token = token.id_token,
|
|
301
|
-
expires_in = token.expires_in;
|
|
302
|
-
|
|
303
|
-
if (!access_token || !id_token) {
|
|
304
|
-
throw new Error('Login failed');
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
var userInfoString = atob( // convert base64url specific characters before parsing as base64
|
|
308
|
-
id_token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'));
|
|
309
|
-
var userInfo = JSON.parse(userInfoString);
|
|
310
|
-
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.
|
|
311
|
-
|
|
312
|
-
var pctTokenExpirationInSeconds = tokenExpirationInSeconds * (REFRESH_TOKEN_WHEN_PCT_EXPIRED / 100);
|
|
313
|
-
var epochTimeTokenExpirationInSeconds = pctTokenExpirationInSeconds + getCurrentTimeInSeconds();
|
|
314
|
-
var isAuthCognito = userInfo.iss.includes('cognito');
|
|
315
|
-
var newAuth = {
|
|
316
|
-
username: isAuthCognito ? userInfo['cognito:username'] : userInfo.email,
|
|
317
|
-
token: access_token,
|
|
318
|
-
refresh_token: refresh_token || '',
|
|
319
|
-
expires_at: epochTimeTokenExpirationInSeconds,
|
|
320
|
-
keep_session_alive_at: getCurrentTimeInSeconds() + KEEP_ALIVE_IN_SECONDS,
|
|
321
|
-
has_connection_issue: false
|
|
322
|
-
};
|
|
323
|
-
return newAuth;
|
|
324
|
-
};
|
|
325
|
-
var refreshAccessToken = function refreshAccessToken(_ref) {
|
|
326
|
-
var auth = _ref.auth,
|
|
327
|
-
_ref$config = _ref.config;
|
|
328
|
-
_ref$config = _ref$config === void 0 ? {} : _ref$config;
|
|
329
|
-
var authTokenURL = _ref$config.authTokenURL,
|
|
330
|
-
authClientId = _ref$config.authClientId,
|
|
331
|
-
appURL = _ref$config.appURL,
|
|
332
|
-
_ref$timeout = _ref.timeout,
|
|
333
|
-
timeout = _ref$timeout === void 0 ? DEFAULT_TIMEOUT : _ref$timeout;
|
|
334
|
-
// Refresh token request with a new axios instance
|
|
335
|
-
// without request interceptor
|
|
336
|
-
var tokenAxiosInstance = axios.create({
|
|
337
|
-
headers: {},
|
|
338
|
-
timeout: timeout
|
|
339
|
-
});
|
|
340
|
-
var refreshPayload = {
|
|
341
|
-
refresh_token: auth.refresh_token,
|
|
342
|
-
redirect_uri: "".concat(appURL, "/code"),
|
|
343
|
-
grant_type: 'refresh_token',
|
|
344
|
-
client_id: authClientId
|
|
345
|
-
};
|
|
346
|
-
/* Send data in the "application/x-www-form-urlencoded" format.
|
|
347
|
-
If only JSON is supported, use Axios' default content type ("application/x-www-form-urlencoded"). */
|
|
348
|
-
|
|
349
|
-
var useDefaultContentType = authTokenURL.includes('amazonaws.com');
|
|
350
|
-
var data = useDefaultContentType ? refreshPayload : new URLSearchParams(refreshPayload);
|
|
351
|
-
var axiosConfig = {
|
|
352
|
-
headers: {
|
|
353
|
-
'Content-Type': useDefaultContentType ? 'application/json' : 'application/x-www-form-urlencoded'
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
return tokenAxiosInstance.post(authTokenURL, data, axiosConfig);
|
|
357
|
-
};
|
|
358
|
-
var refreshAccessTokenAndSetAuthContext = function refreshAccessTokenAndSetAuthContext(_ref2) {
|
|
359
|
-
var auth = _ref2.auth,
|
|
360
|
-
onSetAuth = _ref2.onSetAuth,
|
|
361
|
-
config = _ref2.config,
|
|
362
|
-
_ref2$timeout = _ref2.timeout,
|
|
363
|
-
timeout = _ref2$timeout === void 0 ? DEFAULT_TIMEOUT : _ref2$timeout;
|
|
364
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
365
|
-
var refreshedToken, newAuth;
|
|
366
|
-
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
367
|
-
while (1) {
|
|
368
|
-
switch (_context.prev = _context.next) {
|
|
369
|
-
case 0:
|
|
370
|
-
_context.prev = 0;
|
|
371
|
-
_context.next = 3;
|
|
372
|
-
return refreshAccessToken({
|
|
373
|
-
auth: auth,
|
|
374
|
-
config: config,
|
|
375
|
-
timeout: timeout
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
case 3:
|
|
379
|
-
refreshedToken = _context.sent;
|
|
380
|
-
newAuth = makeCredentialsFromTokenResponse(refreshedToken); // Cognito does not send a new refresh token, but gitlab does. Set it here into the auth context.
|
|
381
|
-
|
|
382
|
-
if (!newAuth.refresh_token || newAuth.refresh_token.length === 0) {
|
|
383
|
-
newAuth.refresh_token = auth.refresh_token;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
onSetAuth(newAuth);
|
|
387
|
-
_context.next = 12;
|
|
388
|
-
break;
|
|
389
|
-
|
|
390
|
-
case 9:
|
|
391
|
-
_context.prev = 9;
|
|
392
|
-
_context.t0 = _context["catch"](0);
|
|
393
|
-
onSetAuth(Object.assign(Object.assign({}, auth), {
|
|
394
|
-
has_connection_issue: true
|
|
395
|
-
}));
|
|
396
|
-
|
|
397
|
-
case 12:
|
|
398
|
-
case "end":
|
|
399
|
-
return _context.stop();
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}, _callee, null, [[0, 9]]);
|
|
403
|
-
}));
|
|
404
|
-
};
|
|
405
|
-
var createApiInstance = function createApiInstance(_ref3) {
|
|
406
|
-
var auth = _ref3.auth,
|
|
407
|
-
onSetAuth = _ref3.onSetAuth,
|
|
408
|
-
_ref3$config = _ref3.config;
|
|
409
|
-
_ref3$config = _ref3$config === void 0 ? {} : _ref3$config;
|
|
410
|
-
var baseURL = _ref3$config.baseURL,
|
|
411
|
-
authTokenURL = _ref3$config.authTokenURL,
|
|
412
|
-
authClientId = _ref3$config.authClientId,
|
|
413
|
-
appURL = _ref3$config.appURL,
|
|
414
|
-
_ref3$timeout = _ref3.timeout,
|
|
415
|
-
timeout = _ref3$timeout === void 0 ? DEFAULT_TIMEOUT : _ref3$timeout;
|
|
416
|
-
var axiosInstance = axios.create({
|
|
417
|
-
baseURL: baseURL,
|
|
418
|
-
headers: {},
|
|
419
|
-
timeout: timeout
|
|
420
|
-
}); // Request interceptor for API calls done BEFORE the request is made.
|
|
421
|
-
|
|
422
|
-
axiosInstance.interceptors.request.use(function (axiosConfig) {
|
|
423
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
|
|
424
|
-
var timeInSecondsLeftBeforeExpiration, newConfig;
|
|
425
|
-
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
426
|
-
while (1) {
|
|
427
|
-
switch (_context2.prev = _context2.next) {
|
|
428
|
-
case 0:
|
|
429
|
-
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)
|
|
430
|
-
|
|
431
|
-
if (!(timeInSecondsLeftBeforeExpiration < 0)) {
|
|
432
|
-
_context2.next = 4;
|
|
433
|
-
break;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
_context2.next = 4;
|
|
437
|
-
return refreshAccessTokenAndSetAuthContext({
|
|
438
|
-
auth: auth,
|
|
439
|
-
onSetAuth: onSetAuth,
|
|
440
|
-
config: {
|
|
441
|
-
baseURL: baseURL,
|
|
442
|
-
authTokenURL: authTokenURL,
|
|
443
|
-
authClientId: authClientId,
|
|
444
|
-
appURL: appURL
|
|
445
|
-
},
|
|
446
|
-
timeout: timeout
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
case 4:
|
|
450
|
-
newConfig = Object.assign(Object.assign({}, axiosConfig), {
|
|
451
|
-
headers: Object.assign({
|
|
452
|
-
'Content-Type': 'application/json',
|
|
453
|
-
Authorization: "Bearer ".concat(auth.token),
|
|
454
|
-
Accept: 'application/json'
|
|
455
|
-
}, axiosConfig.headers)
|
|
456
|
-
});
|
|
457
|
-
return _context2.abrupt("return", newConfig);
|
|
458
|
-
|
|
459
|
-
case 6:
|
|
460
|
-
case "end":
|
|
461
|
-
return _context2.stop();
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
}, _callee2);
|
|
465
|
-
}));
|
|
466
|
-
}, function (error) {
|
|
467
|
-
Promise.reject(error);
|
|
468
|
-
}); // Response interceptor for API calls done AFTER the request is made.
|
|
469
|
-
|
|
470
|
-
axiosInstance.interceptors.response.use(function (response) {
|
|
471
|
-
return response;
|
|
472
|
-
}, function (error) {
|
|
473
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
|
|
474
|
-
var originalRequest;
|
|
475
|
-
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
476
|
-
while (1) {
|
|
477
|
-
switch (_context3.prev = _context3.next) {
|
|
478
|
-
case 0:
|
|
479
|
-
originalRequest = error.config;
|
|
480
|
-
|
|
481
|
-
if (!(error.response && error.response.status && (error.response.status === 401 || error.response.status === 403) && !originalRequest.inRetry)) {
|
|
482
|
-
_context3.next = 6;
|
|
483
|
-
break;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
originalRequest.inRetry = true;
|
|
487
|
-
_context3.next = 5;
|
|
488
|
-
return refreshAccessTokenAndSetAuthContext({
|
|
489
|
-
auth: auth,
|
|
490
|
-
onSetAuth: onSetAuth,
|
|
491
|
-
config: {
|
|
492
|
-
baseURL: baseURL,
|
|
493
|
-
authTokenURL: authTokenURL,
|
|
494
|
-
authClientId: authClientId,
|
|
495
|
-
appURL: appURL
|
|
496
|
-
},
|
|
497
|
-
timeout: timeout
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
case 5:
|
|
501
|
-
return _context3.abrupt("return", axiosInstance(originalRequest));
|
|
502
|
-
|
|
503
|
-
case 6:
|
|
504
|
-
return _context3.abrupt("return", Promise.reject(error));
|
|
505
|
-
|
|
506
|
-
case 7:
|
|
507
|
-
case "end":
|
|
508
|
-
return _context3.stop();
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
}, _callee3);
|
|
512
|
-
}));
|
|
513
|
-
});
|
|
514
|
-
return axiosInstance;
|
|
515
|
-
};
|
|
516
|
-
var createNonAuthApiInstance = function createNonAuthApiInstance(_ref4) {
|
|
517
|
-
var _ref4$config = _ref4.config;
|
|
518
|
-
_ref4$config = _ref4$config === void 0 ? {} : _ref4$config;
|
|
519
|
-
var baseURL = _ref4$config.baseURL,
|
|
520
|
-
_ref4$timeout = _ref4.timeout,
|
|
521
|
-
timeout = _ref4$timeout === void 0 ? DEFAULT_TIMEOUT : _ref4$timeout;
|
|
522
|
-
var axiosInstance = axios.create({
|
|
523
|
-
baseURL: baseURL,
|
|
524
|
-
headers: {},
|
|
525
|
-
timeout: timeout
|
|
526
|
-
}); // Request interceptor for API calls
|
|
527
|
-
|
|
528
|
-
axiosInstance.interceptors.request.use(function (config) {
|
|
529
|
-
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
|
|
530
|
-
var newConfig;
|
|
531
|
-
return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
532
|
-
while (1) {
|
|
533
|
-
switch (_context4.prev = _context4.next) {
|
|
534
|
-
case 0:
|
|
535
|
-
newConfig = Object.assign(Object.assign({}, config), {
|
|
536
|
-
headers: Object.assign({
|
|
537
|
-
'Content-Type': 'application/json',
|
|
538
|
-
Accept: 'application/json'
|
|
539
|
-
}, config.headers)
|
|
540
|
-
});
|
|
541
|
-
return _context4.abrupt("return", newConfig);
|
|
542
|
-
|
|
543
|
-
case 2:
|
|
544
|
-
case "end":
|
|
545
|
-
return _context4.stop();
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}, _callee4);
|
|
549
|
-
}));
|
|
550
|
-
}, function (error) {
|
|
551
|
-
Promise.reject(error);
|
|
552
|
-
});
|
|
553
|
-
return axiosInstance;
|
|
554
|
-
};
|
|
555
|
-
var fakeApiRequest = function fakeApiRequest(signal) {
|
|
556
|
-
return new Promise(function (resolve, reject) {
|
|
557
|
-
var timer = setTimeout(function () {
|
|
558
|
-
resolve();
|
|
559
|
-
}, 300);
|
|
560
|
-
signal === null || signal === void 0 ? void 0 : signal.signal.addEventListener('abort', function () {
|
|
561
|
-
clearTimeout(timer);
|
|
562
|
-
reject(new Error('canceled'));
|
|
563
|
-
});
|
|
564
|
-
});
|
|
565
|
-
};
|
|
566
|
-
var createFakeApiInstance = function createFakeApiInstance() {
|
|
567
|
-
return {
|
|
568
|
-
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars,
|
|
569
|
-
get: function get(_url, _params) {
|
|
570
|
-
return fakeApiRequest();
|
|
571
|
-
},
|
|
572
|
-
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars,
|
|
573
|
-
put: function put(_url, _params) {
|
|
574
|
-
return fakeApiRequest();
|
|
575
|
-
},
|
|
576
|
-
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars,
|
|
577
|
-
post: function post(_url, _params) {
|
|
578
|
-
return fakeApiRequest();
|
|
579
|
-
},
|
|
580
|
-
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
|
|
581
|
-
"delete": function _delete(_url, _params) {
|
|
582
|
-
return fakeApiRequest();
|
|
583
|
-
},
|
|
584
|
-
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars, @typescript-eslint/explicit-module-boundary-types
|
|
585
|
-
patch: function patch(_url, _params, _signal) {
|
|
586
|
-
return fakeApiRequest(_signal);
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
};
|
|
590
|
-
var getCurrentTimeInSeconds = function getCurrentTimeInSeconds() {
|
|
591
|
-
return Date.now() * MILLISECOND_TO_SECOND;
|
|
592
|
-
};
|
|
593
|
-
|
|
594
|
-
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 };
|