@frontegg/rest-api 3.0.1 → 3.0.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.
Files changed (68) hide show
  1. package/ContextHolder.js +64 -100
  2. package/account-settings/index.js +4 -43
  3. package/audits/index.js +16 -69
  4. package/auth/enums.js +2 -2
  5. package/auth/index.js +352 -2045
  6. package/auth/interfaces.js +1 -1
  7. package/auth/secutiry-poilicy/index.js +45 -254
  8. package/connectivity/index.js +79 -73
  9. package/constants.js +1 -1
  10. package/error.js +9 -33
  11. package/fetch.js +159 -519
  12. package/index.js +26 -26
  13. package/jwt.js +19 -45
  14. package/metadata/index.js +18 -160
  15. package/node/ContextHolder.js +68 -104
  16. package/node/account-settings/index.js +4 -46
  17. package/node/audits/index.js +15 -68
  18. package/node/auth/enums.js +2 -2
  19. package/node/auth/index.js +481 -2099
  20. package/node/auth/interfaces.js +2 -2
  21. package/node/auth/secutiry-poilicy/index.js +36 -240
  22. package/node/connectivity/index.js +78 -71
  23. package/node/constants.js +1 -1
  24. package/node/error.js +9 -38
  25. package/node/fetch.js +166 -521
  26. package/node/index.js +47 -49
  27. package/node/jwt.js +19 -51
  28. package/node/metadata/index.js +19 -163
  29. package/node/notifications/index.js +18 -94
  30. package/node/reports/index.js +62 -171
  31. package/node/roles/index.js +34 -172
  32. package/node/routers.js +2 -2
  33. package/node/sub-tenants/index.js +12 -126
  34. package/node/subscriptions/enums.js +4 -4
  35. package/node/subscriptions/index.js +10 -10
  36. package/node/subscriptions/interfaces.js +1 -1
  37. package/node/subscriptions/invoices.js +10 -70
  38. package/node/subscriptions/managedSubscriptions.js +16 -111
  39. package/node/subscriptions/paymentMethods.js +15 -69
  40. package/node/subscriptions/paymentProviders.js +2 -26
  41. package/node/subscriptions/plans.js +4 -46
  42. package/node/subscriptions/providers/index.js +1 -1
  43. package/node/subscriptions/providers/stripe/index.js +10 -106
  44. package/node/subscriptions/subscriptions.js +16 -111
  45. package/node/subscriptions/summaries.js +2 -26
  46. package/node/subscriptions/tenantConfiguration.js +4 -46
  47. package/node/teams/index.js +57 -406
  48. package/node/tenants/index.js +8 -86
  49. package/node/vendor/index.js +2 -26
  50. package/notifications/index.js +18 -93
  51. package/package.json +1 -1
  52. package/reports/index.js +65 -175
  53. package/roles/index.js +34 -175
  54. package/routers.js +2 -2
  55. package/sub-tenants/index.js +12 -127
  56. package/subscriptions/enums.js +4 -4
  57. package/subscriptions/invoices.js +12 -70
  58. package/subscriptions/managedSubscriptions.js +20 -115
  59. package/subscriptions/paymentMethods.js +16 -70
  60. package/subscriptions/paymentProviders.js +2 -22
  61. package/subscriptions/plans.js +4 -43
  62. package/subscriptions/providers/stripe/index.js +10 -106
  63. package/subscriptions/subscriptions.js +20 -115
  64. package/subscriptions/summaries.js +2 -22
  65. package/subscriptions/tenantConfiguration.js +4 -43
  66. package/teams/index.js +55 -421
  67. package/tenants/index.js +8 -85
  68. package/vendor/index.js +2 -23
package/fetch.js CHANGED
@@ -1,18 +1,8 @@
1
- import _typeof from "@babel/runtime/helpers/esm/typeof";
2
1
  import _extends from "@babel/runtime/helpers/esm/extends";
3
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
-
5
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
6
-
7
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
-
9
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
10
-
11
- import _regeneratorRuntime from "@babel/runtime/regenerator";
12
2
  import { ContextHolder } from './ContextHolder';
13
3
  import { FronteggApiError } from './error';
14
4
  export function getBaseUrl(context, url) {
15
- var baseUrl;
5
+ let baseUrl;
16
6
 
17
7
  if (typeof context.baseUrl === 'function') {
18
8
  baseUrl = context.baseUrl(url);
@@ -20,7 +10,7 @@ export function getBaseUrl(context, url) {
20
10
  baseUrl = context.baseUrl;
21
11
  }
22
12
 
23
- var prefix = context.urlPrefix || 'frontegg';
13
+ const prefix = context.urlPrefix || 'frontegg';
24
14
 
25
15
  if (!baseUrl.endsWith('/')) {
26
16
  baseUrl += '/';
@@ -33,525 +23,175 @@ export function getBaseUrl(context, url) {
33
23
  return baseUrl;
34
24
  }
35
25
 
36
- function prepareUrl(_x, _x2, _x3) {
37
- return _prepareUrl.apply(this, arguments);
38
- }
26
+ async function prepareUrl(context, url, params) {
27
+ const baseUrl = getBaseUrl(context, url);
28
+ const paramsToSend = await buildQueryParams(context, params);
29
+ let finalUrl = url.startsWith('http') ? url : `${baseUrl}${url}`;
30
+ const hasKeys = Object.keys(paramsToSend).length > 0;
39
31
 
40
- function _prepareUrl() {
41
- _prepareUrl = _asyncToGenerator(_regeneratorRuntime.mark(function _callee8(context, url, params) {
42
- var baseUrl, paramsToSend, finalUrl, hasKeys, urlParams;
43
- return _regeneratorRuntime.wrap(function _callee8$(_context8) {
44
- while (1) {
45
- switch (_context8.prev = _context8.next) {
46
- case 0:
47
- baseUrl = getBaseUrl(context, url);
48
- _context8.next = 3;
49
- return buildQueryParams(context, params);
50
-
51
- case 3:
52
- paramsToSend = _context8.sent;
53
- finalUrl = url.startsWith('http') ? url : "".concat(baseUrl).concat(url);
54
- hasKeys = Object.keys(paramsToSend).length > 0;
55
-
56
- if (paramsToSend && hasKeys) {
57
- urlParams = new URLSearchParams(paramsToSend);
58
- finalUrl += "?".concat(urlParams);
59
- }
60
-
61
- return _context8.abrupt("return", finalUrl);
62
-
63
- case 8:
64
- case "end":
65
- return _context8.stop();
66
- }
67
- }
68
- }, _callee8);
69
- }));
70
- return _prepareUrl.apply(this, arguments);
71
- }
32
+ if (paramsToSend && hasKeys) {
33
+ const urlParams = new URLSearchParams(paramsToSend);
34
+ finalUrl += `?${urlParams}`;
35
+ }
72
36
 
73
- function buildRequestHeaders(_x4, _x5) {
74
- return _buildRequestHeaders.apply(this, arguments);
37
+ return finalUrl;
75
38
  }
76
39
 
77
- function _buildRequestHeaders() {
78
- _buildRequestHeaders = _asyncToGenerator(_regeneratorRuntime.mark(function _callee9(context, contentType) {
79
- var _context$tokenResolve;
80
-
81
- var authToken, headers, _iterator, _step, additionalHeader;
82
-
83
- return _regeneratorRuntime.wrap(function _callee9$(_context9) {
84
- while (1) {
85
- switch (_context9.prev = _context9.next) {
86
- case 0:
87
- _context9.next = 2;
88
- return ((_context$tokenResolve = context == null ? void 0 : context.tokenResolver) != null ? _context$tokenResolve : ContextHolder.getAccessToken)();
89
-
90
- case 2:
91
- authToken = _context9.sent;
92
- headers = {};
93
-
94
- if (authToken) {
95
- headers.Authorization = "Bearer ".concat(authToken);
96
- }
97
-
98
- if (contentType) {
99
- headers['Content-Type'] = contentType;
100
- }
101
-
102
- _context9.t0 = _createForOfIteratorHelper;
103
- _context9.next = 9;
104
- return getAdditionalHeaders(context);
105
-
106
- case 9:
107
- _context9.t1 = _context9.sent;
108
- _iterator = (0, _context9.t0)(_context9.t1);
109
-
110
- try {
111
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
112
- additionalHeader = _step.value;
113
- headers["".concat(additionalHeader.key)] = "".concat(additionalHeader.value);
114
- }
115
- } catch (err) {
116
- _iterator.e(err);
117
- } finally {
118
- _iterator.f();
119
- }
120
-
121
- headers['x-frontegg-source'] = 'frontegg-react';
122
- return _context9.abrupt("return", headers);
123
-
124
- case 14:
125
- case "end":
126
- return _context9.stop();
127
- }
128
- }
129
- }, _callee9);
130
- }));
131
- return _buildRequestHeaders.apply(this, arguments);
132
- }
40
+ async function buildRequestHeaders(context, contentType) {
41
+ const authToken = await (context?.tokenResolver ?? ContextHolder.getAccessToken)();
42
+ const headers = {};
133
43
 
134
- function buildQueryParams(_x6, _x7) {
135
- return _buildQueryParams.apply(this, arguments);
136
- }
44
+ if (authToken) {
45
+ headers.Authorization = `Bearer ${authToken}`;
46
+ }
137
47
 
138
- function _buildQueryParams() {
139
- _buildQueryParams = _asyncToGenerator(_regeneratorRuntime.mark(function _callee10(context, params) {
140
- var additionalQueryParams, _iterator2, _step2, queryParam, keys, _i, _keys, key, value;
141
-
142
- return _regeneratorRuntime.wrap(function _callee10$(_context10) {
143
- while (1) {
144
- switch (_context10.prev = _context10.next) {
145
- case 0:
146
- if (!params) {
147
- params = {};
148
- }
149
-
150
- _context10.next = 3;
151
- return getAdditionalQueryParams(context);
152
-
153
- case 3:
154
- additionalQueryParams = _context10.sent;
155
- _iterator2 = _createForOfIteratorHelper(additionalQueryParams);
156
-
157
- try {
158
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
159
- queryParam = _step2.value;
160
- params[queryParam.key] = queryParam.value;
161
- }
162
- } catch (err) {
163
- _iterator2.e(err);
164
- } finally {
165
- _iterator2.f();
166
- }
167
-
168
- keys = Object.keys(params);
169
-
170
- for (_i = 0, _keys = keys; _i < _keys.length; _i++) {
171
- key = _keys[_i];
172
- value = params[key];
173
- params[key] = _typeof(value) === 'object' ? JSON.stringify(value) : value;
174
- }
175
-
176
- return _context10.abrupt("return", params);
177
-
178
- case 9:
179
- case "end":
180
- return _context10.stop();
181
- }
182
- }
183
- }, _callee10);
184
- }));
185
- return _buildQueryParams.apply(this, arguments);
186
- }
48
+ if (contentType) {
49
+ headers['Content-Type'] = contentType;
50
+ }
187
51
 
188
- function getAdditionalQueryParams(_x8) {
189
- return _getAdditionalQueryParams.apply(this, arguments);
52
+ for (const additionalHeader of await getAdditionalHeaders(context)) {
53
+ headers[`${additionalHeader.key}`] = `${additionalHeader.value}`;
54
+ }
55
+
56
+ headers['x-frontegg-source'] = 'frontegg-react';
57
+ return headers;
190
58
  }
191
59
 
192
- function _getAdditionalQueryParams() {
193
- _getAdditionalQueryParams = _asyncToGenerator(_regeneratorRuntime.mark(function _callee11(context) {
194
- var output;
195
- return _regeneratorRuntime.wrap(function _callee11$(_context11) {
196
- while (1) {
197
- switch (_context11.prev = _context11.next) {
198
- case 0:
199
- output = [];
200
-
201
- if (!context.additionalQueryParamsResolver) {
202
- _context11.next = 5;
203
- break;
204
- }
205
-
206
- _context11.next = 4;
207
- return context.additionalQueryParamsResolver();
208
-
209
- case 4:
210
- output = _context11.sent;
211
-
212
- case 5:
213
- return _context11.abrupt("return", output);
214
-
215
- case 6:
216
- case "end":
217
- return _context11.stop();
218
- }
219
- }
220
- }, _callee11);
221
- }));
222
- return _getAdditionalQueryParams.apply(this, arguments);
60
+ async function buildQueryParams(context, params) {
61
+ if (!params) {
62
+ params = {};
63
+ }
64
+
65
+ const additionalQueryParams = await getAdditionalQueryParams(context);
66
+
67
+ for (const queryParam of additionalQueryParams) {
68
+ params[queryParam.key] = queryParam.value;
69
+ }
70
+
71
+ const keys = Object.keys(params);
72
+
73
+ for (const key of keys) {
74
+ const value = params[key];
75
+ params[key] = typeof value === 'object' ? JSON.stringify(value) : value;
76
+ }
77
+
78
+ return params;
223
79
  }
224
80
 
225
- function getAdditionalHeaders(_x9) {
226
- return _getAdditionalHeaders.apply(this, arguments);
81
+ async function getAdditionalQueryParams(context) {
82
+ let output = [];
83
+
84
+ if (context.additionalQueryParamsResolver) {
85
+ output = await context.additionalQueryParamsResolver();
86
+ }
87
+
88
+ return output;
227
89
  }
228
90
 
229
- function _getAdditionalHeaders() {
230
- _getAdditionalHeaders = _asyncToGenerator(_regeneratorRuntime.mark(function _callee12(context) {
231
- var output;
232
- return _regeneratorRuntime.wrap(function _callee12$(_context12) {
233
- while (1) {
234
- switch (_context12.prev = _context12.next) {
235
- case 0:
236
- output = [];
237
-
238
- if (!context.additionalHeadersResolver) {
239
- _context12.next = 5;
240
- break;
241
- }
242
-
243
- _context12.next = 4;
244
- return context.additionalHeadersResolver();
245
-
246
- case 4:
247
- output = _context12.sent;
248
-
249
- case 5:
250
- return _context12.abrupt("return", output);
251
-
252
- case 6:
253
- case "end":
254
- return _context12.stop();
255
- }
256
- }
257
- }, _callee12);
258
- }));
259
- return _getAdditionalHeaders.apply(this, arguments);
91
+ async function getAdditionalHeaders(context) {
92
+ let output = [];
93
+
94
+ if (context.additionalHeadersResolver) {
95
+ output = await context.additionalHeadersResolver();
96
+ }
97
+
98
+ return output;
260
99
  }
261
100
 
262
- var sendRequest = function () {
263
- var _ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(opts) {
264
- var _opts$method, _ref2, _opts$credentials;
265
-
266
- var context, headers, url, response, _context$logLevel, _context$logLevel2, errorMessage, outputFileName;
267
-
268
- return _regeneratorRuntime.wrap(function _callee$(_context) {
269
- while (1) {
270
- switch (_context.prev = _context.next) {
271
- case 0:
272
- context = ContextHolder.getContext();
273
- _context.next = 3;
274
- return buildRequestHeaders(context, opts.contentType);
275
-
276
- case 3:
277
- headers = _context.sent;
278
- _context.next = 6;
279
- return prepareUrl(context, opts.url, opts.params);
280
-
281
- case 6:
282
- url = _context.sent;
283
- _context.next = 9;
284
- return fetch(url, {
285
- body: opts.body ? opts.contentType === 'application/json' ? JSON.stringify(opts.body) : opts.body : null,
286
- method: (_opts$method = opts.method) != null ? _opts$method : 'GET',
287
- headers: _extends({}, headers, opts.headers),
288
- credentials: (_ref2 = (_opts$credentials = opts.credentials) != null ? _opts$credentials : context.requestCredentials) != null ? _ref2 : 'same-origin'
289
- });
290
-
291
- case 9:
292
- response = _context.sent;
293
-
294
- if (!(response.status === 302)) {
295
- _context.next = 15;
296
- break;
297
- }
298
-
299
- _context.next = 13;
300
- return response.text();
301
-
302
- case 13:
303
- window.location.href = _context.sent;
304
- return _context.abrupt("return", new Promise(function () {}));
305
-
306
- case 15:
307
- if (response.ok) {
308
- _context.next = 30;
309
- break;
310
- }
311
-
312
- if (!(response.status === 413)) {
313
- _context.next = 18;
314
- break;
315
- }
316
-
317
- throw new FronteggApiError('Error request is too large', response.status);
318
-
319
- case 18:
320
- _context.prev = 18;
321
- _context.next = 21;
322
- return response.text();
323
-
324
- case 21:
325
- errorMessage = _context.sent;
326
- errorMessage = JSON.parse(errorMessage);
327
- _context.next = 27;
328
- break;
329
-
330
- case 25:
331
- _context.prev = 25;
332
- _context.t0 = _context["catch"](18);
333
-
334
- case 27:
335
- if (errorMessage.errors) {
336
- errorMessage = errorMessage.errors.join(', ');
337
- } else if (typeof errorMessage !== 'string') {
338
- errorMessage = "Error ".concat(response.status, " - ").concat(response.statusText);
339
- }
340
-
341
- if (response.status >= 400 && response.status < 500 && ['warn'].includes((_context$logLevel = context.logLevel) != null ? _context$logLevel : '')) console.warn(errorMessage);else if (response.status === 500 && ['warn', 'error'].includes((_context$logLevel2 = context.logLevel) != null ? _context$logLevel2 : '')) console.error(errorMessage);
342
- throw new FronteggApiError(errorMessage, response.status);
343
-
344
- case 30:
345
- if (!(!opts.responseType || opts.responseType === 'json')) {
346
- _context.next = 42;
347
- break;
348
- }
349
-
350
- _context.prev = 31;
351
- _context.next = 34;
352
- return response.json();
353
-
354
- case 34:
355
- return _context.abrupt("return", _context.sent);
356
-
357
- case 37:
358
- _context.prev = 37;
359
- _context.t1 = _context["catch"](31);
360
- return _context.abrupt("return", {});
361
-
362
- case 40:
363
- _context.next = 52;
364
- break;
365
-
366
- case 42:
367
- if (!(opts.responseType === 'blob')) {
368
- _context.next = 49;
369
- break;
370
- }
371
-
372
- outputFileName = opts.params.outputFileName;
373
- _context.next = 46;
374
- return response.blob().then(function (blob) {
375
- return URL.createObjectURL(blob);
376
- }).then(function (url) {
377
- var tempLink = document.createElement('a');
378
- tempLink.href = url;
379
- tempLink.setAttribute('download', outputFileName || 'output');
380
- tempLink.click();
381
- });
382
-
383
- case 46:
384
- return _context.abrupt("return", _context.sent);
385
-
386
- case 49:
387
- _context.next = 51;
388
- return response.text();
389
-
390
- case 51:
391
- return _context.abrupt("return", _context.sent);
392
-
393
- case 52:
394
- case "end":
395
- return _context.stop();
396
- }
397
- }
398
- }, _callee, null, [[18, 25], [31, 37]]);
399
- }));
400
-
401
- return function sendRequest(_x10) {
402
- return _ref.apply(this, arguments);
403
- };
404
- }();
405
-
406
- export var Get = function () {
407
- var _ref3 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2(url, params, opts) {
408
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
409
- while (1) {
410
- switch (_context2.prev = _context2.next) {
411
- case 0:
412
- return _context2.abrupt("return", sendRequest(_extends({
413
- url: url,
414
- method: 'GET',
415
- contentType: 'application/json',
416
- params: params
417
- }, opts)));
418
-
419
- case 1:
420
- case "end":
421
- return _context2.stop();
422
- }
423
- }
424
- }, _callee2);
425
- }));
426
-
427
- return function Get(_x11, _x12, _x13) {
428
- return _ref3.apply(this, arguments);
429
- };
430
- }();
431
- export var Post = function () {
432
- var _ref4 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee3(url, body, opts) {
433
- return _regeneratorRuntime.wrap(function _callee3$(_context3) {
434
- while (1) {
435
- switch (_context3.prev = _context3.next) {
436
- case 0:
437
- return _context3.abrupt("return", sendRequest(_extends({
438
- url: url,
439
- method: 'POST',
440
- contentType: 'application/json',
441
- body: body
442
- }, opts)));
443
-
444
- case 1:
445
- case "end":
446
- return _context3.stop();
447
- }
448
- }
449
- }, _callee3);
450
- }));
451
-
452
- return function Post(_x14, _x15, _x16) {
453
- return _ref4.apply(this, arguments);
454
- };
455
- }();
456
- export var Patch = function () {
457
- var _ref5 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee4(url, body, opts) {
458
- return _regeneratorRuntime.wrap(function _callee4$(_context4) {
459
- while (1) {
460
- switch (_context4.prev = _context4.next) {
461
- case 0:
462
- return _context4.abrupt("return", sendRequest(_extends({
463
- url: url,
464
- method: 'PATCH',
465
- contentType: 'application/json',
466
- body: body
467
- }, opts)));
468
-
469
- case 1:
470
- case "end":
471
- return _context4.stop();
472
- }
473
- }
474
- }, _callee4);
475
- }));
476
-
477
- return function Patch(_x17, _x18, _x19) {
478
- return _ref5.apply(this, arguments);
479
- };
480
- }();
481
- export var Put = function () {
482
- var _ref6 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee5(url, body, opts) {
483
- return _regeneratorRuntime.wrap(function _callee5$(_context5) {
484
- while (1) {
485
- switch (_context5.prev = _context5.next) {
486
- case 0:
487
- return _context5.abrupt("return", sendRequest(_extends({
488
- url: url,
489
- method: 'PUT',
490
- contentType: 'application/json',
491
- body: body
492
- }, opts)));
493
-
494
- case 1:
495
- case "end":
496
- return _context5.stop();
497
- }
498
- }
499
- }, _callee5);
500
- }));
501
-
502
- return function Put(_x20, _x21, _x22) {
503
- return _ref6.apply(this, arguments);
504
- };
505
- }();
506
- export var Delete = function () {
507
- var _ref7 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee6(url, body, opts) {
508
- return _regeneratorRuntime.wrap(function _callee6$(_context6) {
509
- while (1) {
510
- switch (_context6.prev = _context6.next) {
511
- case 0:
512
- return _context6.abrupt("return", sendRequest(_extends({
513
- url: url,
514
- method: 'DELETE',
515
- contentType: 'application/json',
516
- body: body
517
- }, opts)));
518
-
519
- case 1:
520
- case "end":
521
- return _context6.stop();
522
- }
523
- }
524
- }, _callee6);
525
- }));
526
-
527
- return function Delete(_x23, _x24, _x25) {
528
- return _ref7.apply(this, arguments);
529
- };
530
- }();
531
- export var PostDownload = function () {
532
- var _ref8 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee7(url, body, params, opts) {
533
- return _regeneratorRuntime.wrap(function _callee7$(_context7) {
534
- while (1) {
535
- switch (_context7.prev = _context7.next) {
536
- case 0:
537
- return _context7.abrupt("return", sendRequest(_extends({
538
- url: url,
539
- method: 'POST',
540
- contentType: 'application/json',
541
- responseType: 'blob',
542
- body: body,
543
- params: params
544
- }, opts)));
545
-
546
- case 1:
547
- case "end":
548
- return _context7.stop();
549
- }
550
- }
551
- }, _callee7);
552
- }));
553
-
554
- return function PostDownload(_x26, _x27, _x28, _x29) {
555
- return _ref8.apply(this, arguments);
556
- };
557
- }();
101
+ const sendRequest = async opts => {
102
+ const context = ContextHolder.getContext();
103
+ const headers = await buildRequestHeaders(context, opts.contentType);
104
+ const url = await prepareUrl(context, opts.url, opts.params);
105
+ const response = await fetch(url, {
106
+ body: opts.body ? opts.contentType === 'application/json' ? JSON.stringify(opts.body) : opts.body : null,
107
+ method: opts.method ?? 'GET',
108
+ headers: _extends({}, headers, opts.headers),
109
+ credentials: opts.credentials ?? context.requestCredentials ?? 'same-origin'
110
+ });
111
+
112
+ if (response.status === 302) {
113
+ window.location.href = await response.text();
114
+ return new Promise(() => {});
115
+ }
116
+
117
+ if (!response.ok) {
118
+ if (response.status === 413) {
119
+ throw new FronteggApiError('Error request is too large', response.status);
120
+ }
121
+
122
+ let errorMessage;
123
+
124
+ try {
125
+ errorMessage = await response.text();
126
+ errorMessage = JSON.parse(errorMessage);
127
+ } catch (e) {}
128
+
129
+ if (errorMessage.errors) {
130
+ errorMessage = errorMessage.errors.join(', ');
131
+ } else if (typeof errorMessage !== 'string') {
132
+ errorMessage = `Error ${response.status} - ${response.statusText}`;
133
+ }
134
+
135
+ if (response.status >= 400 && response.status < 500 && ['warn'].includes(context.logLevel ?? '')) console.warn(errorMessage);else if (response.status === 500 && ['warn', 'error'].includes(context.logLevel ?? '')) console.error(errorMessage);
136
+ throw new FronteggApiError(errorMessage, response.status);
137
+ }
138
+
139
+ if (!opts.responseType || opts.responseType === 'json') {
140
+ try {
141
+ return await response.json();
142
+ } catch (e) {
143
+ return {};
144
+ }
145
+ } else if (opts.responseType === 'blob') {
146
+ const {
147
+ outputFileName
148
+ } = opts.params;
149
+ return await response.blob().then(blob => URL.createObjectURL(blob)).then(url => {
150
+ const tempLink = document.createElement('a');
151
+ tempLink.href = url;
152
+ tempLink.setAttribute('download', outputFileName || 'output');
153
+ tempLink.click();
154
+ });
155
+ } else {
156
+ return await response.text();
157
+ }
158
+ };
159
+
160
+ export const Get = async (url, params, opts) => sendRequest(_extends({
161
+ url,
162
+ method: 'GET',
163
+ contentType: 'application/json',
164
+ params
165
+ }, opts));
166
+ export const Post = async (url, body, opts) => sendRequest(_extends({
167
+ url,
168
+ method: 'POST',
169
+ contentType: 'application/json',
170
+ body
171
+ }, opts));
172
+ export const Patch = async (url, body, opts) => sendRequest(_extends({
173
+ url,
174
+ method: 'PATCH',
175
+ contentType: 'application/json',
176
+ body
177
+ }, opts));
178
+ export const Put = async (url, body, opts) => sendRequest(_extends({
179
+ url,
180
+ method: 'PUT',
181
+ contentType: 'application/json',
182
+ body
183
+ }, opts));
184
+ export const Delete = async (url, body, opts) => sendRequest(_extends({
185
+ url,
186
+ method: 'DELETE',
187
+ contentType: 'application/json',
188
+ body
189
+ }, opts));
190
+ export const PostDownload = async (url, body, params, opts) => sendRequest(_extends({
191
+ url,
192
+ method: 'POST',
193
+ contentType: 'application/json',
194
+ responseType: 'blob',
195
+ body,
196
+ params
197
+ }, opts));