@frontegg/rest-api 3.0.1 → 3.0.4

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 (69) hide show
  1. package/ContextHolder.js +67 -101
  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.d.ts +4 -0
  6. package/auth/index.js +354 -2044
  7. package/auth/interfaces.js +1 -1
  8. package/auth/secutiry-poilicy/index.js +45 -254
  9. package/connectivity/index.js +79 -73
  10. package/constants.js +1 -1
  11. package/error.js +9 -33
  12. package/fetch.js +165 -519
  13. package/index.js +26 -26
  14. package/jwt.js +19 -45
  15. package/metadata/index.js +19 -159
  16. package/node/ContextHolder.js +68 -104
  17. package/node/account-settings/index.js +4 -46
  18. package/node/audits/index.js +15 -68
  19. package/node/auth/enums.js +2 -2
  20. package/node/auth/index.js +487 -2099
  21. package/node/auth/interfaces.js +2 -2
  22. package/node/auth/secutiry-poilicy/index.js +36 -240
  23. package/node/connectivity/index.js +78 -71
  24. package/node/constants.js +1 -1
  25. package/node/error.js +9 -38
  26. package/node/fetch.js +166 -521
  27. package/node/index.js +47 -49
  28. package/node/jwt.js +19 -51
  29. package/node/metadata/index.js +19 -163
  30. package/node/notifications/index.js +18 -94
  31. package/node/reports/index.js +62 -171
  32. package/node/roles/index.js +34 -172
  33. package/node/routers.js +2 -2
  34. package/node/sub-tenants/index.js +12 -126
  35. package/node/subscriptions/enums.js +4 -4
  36. package/node/subscriptions/index.js +10 -10
  37. package/node/subscriptions/interfaces.js +1 -1
  38. package/node/subscriptions/invoices.js +10 -70
  39. package/node/subscriptions/managedSubscriptions.js +16 -111
  40. package/node/subscriptions/paymentMethods.js +15 -69
  41. package/node/subscriptions/paymentProviders.js +2 -26
  42. package/node/subscriptions/plans.js +4 -46
  43. package/node/subscriptions/providers/index.js +1 -1
  44. package/node/subscriptions/providers/stripe/index.js +10 -106
  45. package/node/subscriptions/subscriptions.js +16 -111
  46. package/node/subscriptions/summaries.js +2 -26
  47. package/node/subscriptions/tenantConfiguration.js +4 -46
  48. package/node/teams/index.js +57 -406
  49. package/node/tenants/index.js +8 -86
  50. package/node/vendor/index.js +2 -26
  51. package/notifications/index.js +18 -93
  52. package/package.json +1 -1
  53. package/reports/index.js +65 -175
  54. package/roles/index.js +34 -175
  55. package/routers.js +2 -2
  56. package/sub-tenants/index.js +12 -127
  57. package/subscriptions/enums.js +4 -4
  58. package/subscriptions/invoices.js +12 -70
  59. package/subscriptions/managedSubscriptions.js +20 -115
  60. package/subscriptions/paymentMethods.js +16 -70
  61. package/subscriptions/paymentProviders.js +2 -22
  62. package/subscriptions/plans.js +4 -43
  63. package/subscriptions/providers/stripe/index.js +10 -106
  64. package/subscriptions/subscriptions.js +20 -115
  65. package/subscriptions/summaries.js +2 -22
  66. package/subscriptions/tenantConfiguration.js +4 -43
  67. package/teams/index.js +56 -420
  68. package/tenants/index.js +8 -85
  69. 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,181 @@ 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
+ var _context$tokenResolve;
133
42
 
134
- function buildQueryParams(_x6, _x7) {
135
- return _buildQueryParams.apply(this, arguments);
136
- }
43
+ const authToken = await ((_context$tokenResolve = context == null ? void 0 : context.tokenResolver) != null ? _context$tokenResolve : ContextHolder.getAccessToken)();
44
+ const headers = {};
137
45
 
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
- }
46
+ if (authToken) {
47
+ headers.Authorization = `Bearer ${authToken}`;
48
+ }
49
+
50
+ if (contentType) {
51
+ headers['Content-Type'] = contentType;
52
+ }
53
+
54
+ for (const additionalHeader of await getAdditionalHeaders(context)) {
55
+ headers[`${additionalHeader.key}`] = `${additionalHeader.value}`;
56
+ }
187
57
 
188
- function getAdditionalQueryParams(_x8) {
189
- return _getAdditionalQueryParams.apply(this, arguments);
58
+ headers['x-frontegg-source'] = 'frontegg-react';
59
+ return headers;
190
60
  }
191
61
 
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);
62
+ async function buildQueryParams(context, params) {
63
+ if (!params) {
64
+ params = {};
65
+ }
66
+
67
+ const additionalQueryParams = await getAdditionalQueryParams(context);
68
+
69
+ for (const queryParam of additionalQueryParams) {
70
+ params[queryParam.key] = queryParam.value;
71
+ }
72
+
73
+ const keys = Object.keys(params);
74
+
75
+ for (const key of keys) {
76
+ const value = params[key];
77
+ params[key] = typeof value === 'object' ? JSON.stringify(value) : value;
78
+ }
79
+
80
+ return params;
223
81
  }
224
82
 
225
- function getAdditionalHeaders(_x9) {
226
- return _getAdditionalHeaders.apply(this, arguments);
83
+ async function getAdditionalQueryParams(context) {
84
+ let output = [];
85
+
86
+ if (context.additionalQueryParamsResolver) {
87
+ output = await context.additionalQueryParamsResolver();
88
+ }
89
+
90
+ return output;
227
91
  }
228
92
 
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);
93
+ async function getAdditionalHeaders(context) {
94
+ let output = [];
95
+
96
+ if (context.additionalHeadersResolver) {
97
+ output = await context.additionalHeadersResolver();
98
+ }
99
+
100
+ return output;
260
101
  }
261
102
 
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
- }();
103
+ const sendRequest = async opts => {
104
+ var _opts$method, _ref, _opts$credentials;
105
+
106
+ const context = ContextHolder.getContext();
107
+ const headers = await buildRequestHeaders(context, opts.contentType);
108
+ const url = await prepareUrl(context, opts.url, opts.params);
109
+ const response = await fetch(url, {
110
+ body: opts.body ? opts.contentType === 'application/json' ? JSON.stringify(opts.body) : opts.body : null,
111
+ method: (_opts$method = opts.method) != null ? _opts$method : 'GET',
112
+ headers: _extends({}, headers, opts.headers),
113
+ credentials: (_ref = (_opts$credentials = opts.credentials) != null ? _opts$credentials : context.requestCredentials) != null ? _ref : 'same-origin'
114
+ });
115
+
116
+ if (response.status === 302) {
117
+ window.location.href = await response.text();
118
+ return new Promise(() => {});
119
+ }
120
+
121
+ if (!response.ok) {
122
+ var _context$logLevel, _context$logLevel2;
123
+
124
+ if (response.status === 413) {
125
+ throw new FronteggApiError('Error request is too large', response.status);
126
+ }
127
+
128
+ let errorMessage;
129
+
130
+ try {
131
+ errorMessage = await response.text();
132
+ errorMessage = JSON.parse(errorMessage);
133
+ } catch (e) {}
134
+
135
+ if (errorMessage.errors) {
136
+ errorMessage = errorMessage.errors.join(', ');
137
+ } else if (typeof errorMessage !== 'string') {
138
+ errorMessage = `Error ${response.status} - ${response.statusText}`;
139
+ }
140
+
141
+ 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);
142
+ throw new FronteggApiError(errorMessage, response.status);
143
+ }
144
+
145
+ if (!opts.responseType || opts.responseType === 'json') {
146
+ try {
147
+ return await response.json();
148
+ } catch (e) {
149
+ return {};
150
+ }
151
+ } else if (opts.responseType === 'blob') {
152
+ const {
153
+ outputFileName
154
+ } = opts.params;
155
+ return await response.blob().then(blob => URL.createObjectURL(blob)).then(url => {
156
+ const tempLink = document.createElement('a');
157
+ tempLink.href = url;
158
+ tempLink.setAttribute('download', outputFileName || 'output');
159
+ tempLink.click();
160
+ });
161
+ } else {
162
+ return await response.text();
163
+ }
164
+ };
165
+
166
+ export const Get = async (url, params, opts) => sendRequest(_extends({
167
+ url,
168
+ method: 'GET',
169
+ contentType: 'application/json',
170
+ params
171
+ }, opts));
172
+ export const Post = async (url, body, opts) => sendRequest(_extends({
173
+ url,
174
+ method: 'POST',
175
+ contentType: 'application/json',
176
+ body
177
+ }, opts));
178
+ export const Patch = async (url, body, opts) => sendRequest(_extends({
179
+ url,
180
+ method: 'PATCH',
181
+ contentType: 'application/json',
182
+ body
183
+ }, opts));
184
+ export const Put = async (url, body, opts) => sendRequest(_extends({
185
+ url,
186
+ method: 'PUT',
187
+ contentType: 'application/json',
188
+ body
189
+ }, opts));
190
+ export const Delete = async (url, body, opts) => sendRequest(_extends({
191
+ url,
192
+ method: 'DELETE',
193
+ contentType: 'application/json',
194
+ body
195
+ }, opts));
196
+ export const PostDownload = async (url, body, params, opts) => sendRequest(_extends({
197
+ url,
198
+ method: 'POST',
199
+ contentType: 'application/json',
200
+ responseType: 'blob',
201
+ body,
202
+ params
203
+ }, opts));