@opengeoweb/api 4.10.0
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 +220 -0
- package/index.d.ts +2 -0
- package/index.esm.js +544 -0
- package/index.umd.js +572 -0
- package/lib/components/ApiContext/ApiContext.d.ts +16 -0
- package/lib/components/ApiContext/ApiContext.spec.d.ts +1 -0
- package/lib/components/ApiContext/index.d.ts +3 -0
- package/lib/components/ApiContext/types.d.ts +28 -0
- package/lib/components/ApiContext/utils.d.ts +17 -0
- package/lib/components/ApiContext/utils.spec.d.ts +1 -0
- package/lib/components/apihooks/index.d.ts +2 -0
- package/lib/components/apihooks/useApi.d.ts +8 -0
- package/lib/components/apihooks/useApi.spec.d.ts +1 -0
- package/lib/components/apihooks/useDebounce.d.ts +1 -0
- package/package.json +32 -0
package/index.umd.js
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('axios'), require('@opengeoweb/shared')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'react', 'axios', '@opengeoweb/shared'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Api = {}, global.React, global.axios, global.shared));
|
|
5
|
+
})(this, (function (exports, React, axios, shared) { 'use strict';
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
function _interopNamespace(e) {
|
|
10
|
+
if (e && e.__esModule) return e;
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n["default"] = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
28
|
+
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
|
|
29
|
+
|
|
30
|
+
/*! *****************************************************************************
|
|
31
|
+
Copyright (c) Microsoft Corporation.
|
|
32
|
+
|
|
33
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
34
|
+
purpose with or without fee is hereby granted.
|
|
35
|
+
|
|
36
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
37
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
38
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
39
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
40
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
41
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
42
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
43
|
+
***************************************************************************** */
|
|
44
|
+
|
|
45
|
+
var __assign = function() {
|
|
46
|
+
__assign = Object.assign || function __assign(t) {
|
|
47
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
48
|
+
s = arguments[i];
|
|
49
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
50
|
+
}
|
|
51
|
+
return t;
|
|
52
|
+
};
|
|
53
|
+
return __assign.apply(this, arguments);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function __rest(s, e) {
|
|
57
|
+
var t = {};
|
|
58
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
59
|
+
t[p] = s[p];
|
|
60
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
61
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
62
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
63
|
+
t[p[i]] = s[p[i]];
|
|
64
|
+
}
|
|
65
|
+
return t;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
69
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
70
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
71
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
72
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
73
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
74
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function __generator(thisArg, body) {
|
|
79
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
80
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
81
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
82
|
+
function step(op) {
|
|
83
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
84
|
+
while (_) try {
|
|
85
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
86
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
87
|
+
switch (op[0]) {
|
|
88
|
+
case 0: case 1: t = op; break;
|
|
89
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
90
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
91
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
92
|
+
default:
|
|
93
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
94
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
95
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
96
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
97
|
+
if (t[2]) _.ops.pop();
|
|
98
|
+
_.trys.pop(); continue;
|
|
99
|
+
}
|
|
100
|
+
op = body.call(thisArg, _);
|
|
101
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
102
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function __read(o, n) {
|
|
107
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
108
|
+
if (!m) return o;
|
|
109
|
+
var i = m.call(o), r, ar = [], e;
|
|
110
|
+
try {
|
|
111
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
112
|
+
}
|
|
113
|
+
catch (error) { e = { error: error }; }
|
|
114
|
+
finally {
|
|
115
|
+
try {
|
|
116
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
117
|
+
}
|
|
118
|
+
finally { if (e) throw e.error; }
|
|
119
|
+
}
|
|
120
|
+
return ar;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/*
|
|
124
|
+
Accepts a value and timeout and will debounce
|
|
125
|
+
|
|
126
|
+
@param {string} value: value of element
|
|
127
|
+
@param {timeout} number: timer
|
|
128
|
+
@return {value: string}
|
|
129
|
+
|
|
130
|
+
@example:
|
|
131
|
+
const debouncedName = useDebounce('testing', 2000)
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
var useDebounce = function useDebounce(value, timeout) {
|
|
135
|
+
var _a = __read(React__namespace.useState(value), 2),
|
|
136
|
+
state = _a[0],
|
|
137
|
+
setState = _a[1];
|
|
138
|
+
|
|
139
|
+
React__namespace.useEffect(function () {
|
|
140
|
+
var handler = setTimeout(function () {
|
|
141
|
+
return setState(value);
|
|
142
|
+
}, timeout);
|
|
143
|
+
return function () {
|
|
144
|
+
return clearTimeout(handler);
|
|
145
|
+
};
|
|
146
|
+
}, [value, timeout]);
|
|
147
|
+
return state;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
var generateRandomId = function generateRandomId() {
|
|
151
|
+
return "-" + Math.random().toString(36).substr(2, 9);
|
|
152
|
+
}; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
var useApi = function useApi(apiCall, params) {
|
|
156
|
+
if (params === void 0) {
|
|
157
|
+
params = null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
var isMounted = shared.useIsMounted().isMounted;
|
|
161
|
+
|
|
162
|
+
var _a = __read(React__namespace.useState(generateRandomId()), 1),
|
|
163
|
+
id = _a[0];
|
|
164
|
+
|
|
165
|
+
var _b = __read(React__namespace.useState(true), 2),
|
|
166
|
+
isLoading = _b[0],
|
|
167
|
+
setIsloading = _b[1];
|
|
168
|
+
|
|
169
|
+
var _c = __read(React__namespace.useState(null), 2),
|
|
170
|
+
error = _c[0],
|
|
171
|
+
setError = _c[1];
|
|
172
|
+
|
|
173
|
+
var _d = __read(React__namespace.useState(null), 2),
|
|
174
|
+
result = _d[0],
|
|
175
|
+
setResult = _d[1];
|
|
176
|
+
|
|
177
|
+
var debouncedParams = useDebounce(JSON.stringify(params), 300);
|
|
178
|
+
|
|
179
|
+
var fetchApiData = function fetchApiData(_params) {
|
|
180
|
+
if (_params === void 0) {
|
|
181
|
+
_params = null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
185
|
+
var response, newError_1;
|
|
186
|
+
return __generator(this, function (_a) {
|
|
187
|
+
switch (_a.label) {
|
|
188
|
+
case 0:
|
|
189
|
+
_a.trys.push([0, 2,, 3]);
|
|
190
|
+
|
|
191
|
+
setIsloading(true);
|
|
192
|
+
setError(null);
|
|
193
|
+
return [4
|
|
194
|
+
/*yield*/
|
|
195
|
+
, apiCall(_params, id)];
|
|
196
|
+
|
|
197
|
+
case 1:
|
|
198
|
+
response = _a.sent();
|
|
199
|
+
|
|
200
|
+
if (isMounted.current) {
|
|
201
|
+
setResult(Array.isArray(response) ? response.map(function (d) {
|
|
202
|
+
return d.data;
|
|
203
|
+
}) : response.data);
|
|
204
|
+
setIsloading(false);
|
|
205
|
+
setError(null);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return [3
|
|
209
|
+
/*break*/
|
|
210
|
+
, 3];
|
|
211
|
+
|
|
212
|
+
case 2:
|
|
213
|
+
newError_1 = _a.sent();
|
|
214
|
+
|
|
215
|
+
if (isMounted.current) {
|
|
216
|
+
if (axios__default["default"].isCancel(newError_1)) {
|
|
217
|
+
setIsloading(false);
|
|
218
|
+
} else {
|
|
219
|
+
setError(newError_1);
|
|
220
|
+
setIsloading(false);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return [3
|
|
225
|
+
/*break*/
|
|
226
|
+
, 3];
|
|
227
|
+
|
|
228
|
+
case 3:
|
|
229
|
+
return [2
|
|
230
|
+
/*return*/
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
React__namespace.useEffect(function () {
|
|
238
|
+
fetchApiData(params);
|
|
239
|
+
}, // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
240
|
+
[debouncedParams]);
|
|
241
|
+
|
|
242
|
+
var clearResults = function clearResults() {
|
|
243
|
+
setIsloading(true);
|
|
244
|
+
setResult(null);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
isLoading: isLoading,
|
|
249
|
+
error: error,
|
|
250
|
+
result: result,
|
|
251
|
+
clearResults: clearResults,
|
|
252
|
+
fetchApiData: fetchApiData
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
var ApiContext = /*#__PURE__*/React__namespace.createContext({
|
|
257
|
+
api: null
|
|
258
|
+
});
|
|
259
|
+
var registeredApis = new Map();
|
|
260
|
+
|
|
261
|
+
function registerApi(name, api) {
|
|
262
|
+
registeredApis.set(name, api);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function unRegisterApi(name) {
|
|
266
|
+
registeredApis["delete"](name);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function getApi(name) {
|
|
270
|
+
return registeredApis.get(name);
|
|
271
|
+
}
|
|
272
|
+
var ApiProvider = function ApiProvider(_a) {
|
|
273
|
+
var children = _a.children,
|
|
274
|
+
createApi = _a.createApi,
|
|
275
|
+
name = _a.name,
|
|
276
|
+
apiModuleProps = __rest(_a, ["children", "createApi", "name"]);
|
|
277
|
+
|
|
278
|
+
var handleNewApi = React__namespace.useCallback(function () {
|
|
279
|
+
var newApi = createApi(apiModuleProps);
|
|
280
|
+
|
|
281
|
+
if (name) {
|
|
282
|
+
registerApi(name, newApi);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return newApi; // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
286
|
+
}, [createApi, name]);
|
|
287
|
+
|
|
288
|
+
var _b = __read(React__namespace.useState(handleNewApi), 2),
|
|
289
|
+
api = _b[0],
|
|
290
|
+
setApi = _b[1];
|
|
291
|
+
|
|
292
|
+
React__namespace.useEffect(function () {
|
|
293
|
+
setApi(handleNewApi);
|
|
294
|
+
return function () {
|
|
295
|
+
if (name) {
|
|
296
|
+
unRegisterApi(name);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
}, [handleNewApi, name]);
|
|
300
|
+
return /*#__PURE__*/React__namespace.createElement(ApiContext.Provider, {
|
|
301
|
+
value: {
|
|
302
|
+
api: api
|
|
303
|
+
}
|
|
304
|
+
}, children);
|
|
305
|
+
};
|
|
306
|
+
function useApiContext() {
|
|
307
|
+
return React__namespace.useContext(ApiContext);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
var DEFAULT_TIMEOUT = 15000;
|
|
311
|
+
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
|
+
|
|
313
|
+
var DEFAULT_TOKEN_EXPIRES_IN = 3600; // Number of seconds a token expires by default
|
|
314
|
+
|
|
315
|
+
var MILLISECOND_TO_SECOND = 1 / 1000;
|
|
316
|
+
/**
|
|
317
|
+
* Creates a Credentials object based on the axios response from the token service.
|
|
318
|
+
*
|
|
319
|
+
* It will calculate the expires_at attribute based on the expires_in property found in the JWT.
|
|
320
|
+
*
|
|
321
|
+
* @param tokenResponse Response of the tokenservice as axios response object.
|
|
322
|
+
* @returns Credentials object.
|
|
323
|
+
*/
|
|
324
|
+
|
|
325
|
+
var makeCredentialsFromTokenResponse = function makeCredentialsFromTokenResponse(tokenResponse) {
|
|
326
|
+
var token = tokenResponse.data.body || tokenResponse.data; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
327
|
+
|
|
328
|
+
var access_token = token.access_token,
|
|
329
|
+
refresh_token = token.refresh_token,
|
|
330
|
+
id_token = token.id_token,
|
|
331
|
+
expires_in = token.expires_in;
|
|
332
|
+
|
|
333
|
+
if (!access_token || !id_token) {
|
|
334
|
+
throw new Error('Login failed');
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
var userInfoString = atob(id_token.split('.')[1]);
|
|
338
|
+
var userInfo = JSON.parse(userInfoString);
|
|
339
|
+
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
|
+
|
|
341
|
+
var pctTokenExpirationInSeconds = tokenExpirationInSeconds * (REFRESH_TOKEN_WHEN_PCT_EXPIRED / 100);
|
|
342
|
+
var epochTimeTokenExpirationInSeconds = pctTokenExpirationInSeconds + Date.now() * MILLISECOND_TO_SECOND;
|
|
343
|
+
var isAuthCognito = userInfo.iss.includes('cognito');
|
|
344
|
+
var newAuth = {
|
|
345
|
+
username: isAuthCognito ? userInfo['cognito:username'] : userInfo.email,
|
|
346
|
+
token: access_token,
|
|
347
|
+
refresh_token: refresh_token || '',
|
|
348
|
+
expires_at: epochTimeTokenExpirationInSeconds
|
|
349
|
+
};
|
|
350
|
+
return newAuth;
|
|
351
|
+
};
|
|
352
|
+
var createApiInstance = function createApiInstance(_a) {
|
|
353
|
+
var auth = _a.auth,
|
|
354
|
+
onSetAuth = _a.onSetAuth,
|
|
355
|
+
_b = _a.config,
|
|
356
|
+
_c = _b === void 0 ? {} : _b,
|
|
357
|
+
baseURL = _c.baseURL,
|
|
358
|
+
appURL = _c.appURL,
|
|
359
|
+
authTokenURL = _c.authTokenURL,
|
|
360
|
+
authClientId = _c.authClientId,
|
|
361
|
+
_d = _a.timeout,
|
|
362
|
+
timeout = _d === void 0 ? DEFAULT_TIMEOUT : _d;
|
|
363
|
+
|
|
364
|
+
var axiosInstance = axios__default["default"].create({
|
|
365
|
+
baseURL: baseURL,
|
|
366
|
+
headers: {},
|
|
367
|
+
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
|
+
|
|
396
|
+
|
|
397
|
+
axiosInstance.interceptors.request.use(function (config) {
|
|
398
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
399
|
+
var timeInSecondsLeftBeforeExpiration, refreshedToken, newAuth, newConfig;
|
|
400
|
+
return __generator(this, function (_a) {
|
|
401
|
+
switch (_a.label) {
|
|
402
|
+
case 0:
|
|
403
|
+
timeInSecondsLeftBeforeExpiration = auth && auth.expires_at ? auth.expires_at - Date.now() * MILLISECOND_TO_SECOND : 0;
|
|
404
|
+
if (!(timeInSecondsLeftBeforeExpiration < 0)) return [3
|
|
405
|
+
/*break*/
|
|
406
|
+
, 2];
|
|
407
|
+
return [4
|
|
408
|
+
/*yield*/
|
|
409
|
+
, refreshAccessToken()];
|
|
410
|
+
|
|
411
|
+
case 1:
|
|
412
|
+
refreshedToken = _a.sent();
|
|
413
|
+
newAuth = makeCredentialsFromTokenResponse(refreshedToken);
|
|
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);
|
|
425
|
+
_a.label = 2;
|
|
426
|
+
|
|
427
|
+
case 2:
|
|
428
|
+
newConfig = __assign(__assign({}, config), {
|
|
429
|
+
headers: __assign({
|
|
430
|
+
'Content-Type': 'application/json',
|
|
431
|
+
Authorization: "Bearer " + auth.token,
|
|
432
|
+
Accept: 'application/json'
|
|
433
|
+
}, config.headers)
|
|
434
|
+
});
|
|
435
|
+
return [2
|
|
436
|
+
/*return*/
|
|
437
|
+
, newConfig];
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
}, function (error) {
|
|
442
|
+
Promise.reject(error);
|
|
443
|
+
}); // Response interceptor for API calls done AFTER the request is made.
|
|
444
|
+
|
|
445
|
+
axiosInstance.interceptors.response.use(function (response) {
|
|
446
|
+
return response;
|
|
447
|
+
}, function (error) {
|
|
448
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
449
|
+
var originalRequest, refreshedToken, newAuth;
|
|
450
|
+
return __generator(this, function (_a) {
|
|
451
|
+
switch (_a.label) {
|
|
452
|
+
case 0:
|
|
453
|
+
originalRequest = error.config;
|
|
454
|
+
if (!(error.response && error.response.status && (error.response.status === 401 || error.response.status === 403) && !originalRequest.inRetry)) return [3
|
|
455
|
+
/*break*/
|
|
456
|
+
, 2];
|
|
457
|
+
originalRequest.inRetry = true;
|
|
458
|
+
return [4
|
|
459
|
+
/*yield*/
|
|
460
|
+
, refreshAccessToken()];
|
|
461
|
+
|
|
462
|
+
case 1:
|
|
463
|
+
refreshedToken = _a.sent();
|
|
464
|
+
newAuth = makeCredentialsFromTokenResponse(refreshedToken);
|
|
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);
|
|
474
|
+
return [2
|
|
475
|
+
/*return*/
|
|
476
|
+
, axiosInstance(originalRequest)];
|
|
477
|
+
|
|
478
|
+
case 2:
|
|
479
|
+
return [2
|
|
480
|
+
/*return*/
|
|
481
|
+
, Promise.reject(error)];
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
return axiosInstance;
|
|
487
|
+
};
|
|
488
|
+
var createNonAuthApiInstance = function createNonAuthApiInstance(_a) {
|
|
489
|
+
var _b = _a.config,
|
|
490
|
+
_c = _b === void 0 ? {} : _b,
|
|
491
|
+
baseURL = _c.baseURL,
|
|
492
|
+
_d = _a.timeout,
|
|
493
|
+
timeout = _d === void 0 ? DEFAULT_TIMEOUT : _d;
|
|
494
|
+
|
|
495
|
+
var axiosInstance = axios__default["default"].create({
|
|
496
|
+
baseURL: baseURL,
|
|
497
|
+
headers: {},
|
|
498
|
+
timeout: timeout
|
|
499
|
+
}); // Request interceptor for API calls
|
|
500
|
+
|
|
501
|
+
axiosInstance.interceptors.request.use(function (config) {
|
|
502
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
503
|
+
var newConfig;
|
|
504
|
+
return __generator(this, function (_a) {
|
|
505
|
+
newConfig = __assign(__assign({}, config), {
|
|
506
|
+
headers: __assign({
|
|
507
|
+
'Content-Type': 'application/json',
|
|
508
|
+
Accept: 'application/json'
|
|
509
|
+
}, config.headers)
|
|
510
|
+
});
|
|
511
|
+
return [2
|
|
512
|
+
/*return*/
|
|
513
|
+
, newConfig];
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
}, function (error) {
|
|
517
|
+
Promise.reject(error);
|
|
518
|
+
});
|
|
519
|
+
return axiosInstance;
|
|
520
|
+
};
|
|
521
|
+
var fakeApiRequest = function fakeApiRequest(signal) {
|
|
522
|
+
return new Promise(function (resolve, reject) {
|
|
523
|
+
var timer = setTimeout(function () {
|
|
524
|
+
resolve();
|
|
525
|
+
}, 300);
|
|
526
|
+
signal === null || signal === void 0 ? void 0 : signal.signal.addEventListener('abort', function () {
|
|
527
|
+
clearTimeout(timer);
|
|
528
|
+
reject(new Error('canceled'));
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
};
|
|
532
|
+
var createFakeApiInstance = function createFakeApiInstance() {
|
|
533
|
+
return {
|
|
534
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars,
|
|
535
|
+
get: function get(_url, _params) {
|
|
536
|
+
return fakeApiRequest();
|
|
537
|
+
},
|
|
538
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars,
|
|
539
|
+
put: function put(_url, _params) {
|
|
540
|
+
return fakeApiRequest();
|
|
541
|
+
},
|
|
542
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars,
|
|
543
|
+
post: function post(_url, _params) {
|
|
544
|
+
return fakeApiRequest();
|
|
545
|
+
},
|
|
546
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
|
|
547
|
+
"delete": function _delete(_url, _params) {
|
|
548
|
+
return fakeApiRequest();
|
|
549
|
+
},
|
|
550
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars, @typescript-eslint/explicit-module-boundary-types
|
|
551
|
+
patch: function patch(_url, _params, _signal) {
|
|
552
|
+
return fakeApiRequest(_signal);
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
exports.ApiProvider = ApiProvider;
|
|
558
|
+
exports.MILLISECOND_TO_SECOND = MILLISECOND_TO_SECOND;
|
|
559
|
+
exports.REFRESH_TOKEN_WHEN_PCT_EXPIRED = REFRESH_TOKEN_WHEN_PCT_EXPIRED;
|
|
560
|
+
exports.createApiInstance = createApiInstance;
|
|
561
|
+
exports.createFakeApiInstance = createFakeApiInstance;
|
|
562
|
+
exports.createNonAuthApiInstance = createNonAuthApiInstance;
|
|
563
|
+
exports.fakeApiRequest = fakeApiRequest;
|
|
564
|
+
exports.getApi = getApi;
|
|
565
|
+
exports.makeCredentialsFromTokenResponse = makeCredentialsFromTokenResponse;
|
|
566
|
+
exports.useApi = useApi;
|
|
567
|
+
exports.useApiContext = useApiContext;
|
|
568
|
+
exports.useDebounce = useDebounce;
|
|
569
|
+
|
|
570
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
571
|
+
|
|
572
|
+
}));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ApiModule, CreateApiFn } from './types';
|
|
3
|
+
interface ApiContextState<ApiType> {
|
|
4
|
+
api: ApiType;
|
|
5
|
+
}
|
|
6
|
+
declare const ApiContext: React.Context<{
|
|
7
|
+
api: never;
|
|
8
|
+
}>;
|
|
9
|
+
interface ApiProviderProps extends ApiModule {
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
createApi: CreateApiFn;
|
|
12
|
+
}
|
|
13
|
+
export declare function getApi<Type>(name: string): Type;
|
|
14
|
+
export declare const ApiProvider: React.FC<ApiProviderProps>;
|
|
15
|
+
export declare function useApiContext<ApiType>(): ApiContextState<ApiType>;
|
|
16
|
+
export default ApiContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface GeoWebJWT {
|
|
2
|
+
access_token: string;
|
|
3
|
+
refresh_token?: string;
|
|
4
|
+
id_token: string;
|
|
5
|
+
expires_in: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Credentials {
|
|
8
|
+
username: string;
|
|
9
|
+
token: string;
|
|
10
|
+
refresh_token: string;
|
|
11
|
+
expires_at?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare type ApiUrls = {
|
|
14
|
+
baseURL?: string;
|
|
15
|
+
appURL?: string;
|
|
16
|
+
authTokenURL?: string;
|
|
17
|
+
authClientId?: string;
|
|
18
|
+
};
|
|
19
|
+
export interface ApiModule {
|
|
20
|
+
auth?: Credentials;
|
|
21
|
+
onSetAuth?: (cred: Credentials) => void;
|
|
22
|
+
config?: ApiUrls;
|
|
23
|
+
name?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface CreateApiProps extends ApiModule {
|
|
26
|
+
timeout?: number;
|
|
27
|
+
}
|
|
28
|
+
export declare type CreateApiFn = (props: CreateApiProps) => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosResponse } from 'axios';
|
|
2
|
+
import { CreateApiProps, Credentials } from './types';
|
|
3
|
+
export declare const REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75;
|
|
4
|
+
export declare const MILLISECOND_TO_SECOND: number;
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Credentials object based on the axios response from the token service.
|
|
7
|
+
*
|
|
8
|
+
* It will calculate the expires_at attribute based on the expires_in property found in the JWT.
|
|
9
|
+
*
|
|
10
|
+
* @param tokenResponse Response of the tokenservice as axios response object.
|
|
11
|
+
* @returns Credentials object.
|
|
12
|
+
*/
|
|
13
|
+
export declare const makeCredentialsFromTokenResponse: (tokenResponse: AxiosResponse) => Credentials;
|
|
14
|
+
export declare const createApiInstance: ({ auth, onSetAuth, config: { baseURL, appURL, authTokenURL, authClientId }, timeout, }: CreateApiProps) => AxiosInstance;
|
|
15
|
+
export declare const createNonAuthApiInstance: ({ config: { baseURL }, timeout, }: CreateApiProps) => AxiosInstance;
|
|
16
|
+
export declare const fakeApiRequest: (signal?: AbortController | undefined) => Promise<void>;
|
|
17
|
+
export declare const createFakeApiInstance: () => AxiosInstance;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useDebounce: (value: string, timeout: number) => string;
|