contentful-management 7.41.0 → 7.42.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.
package/README.md CHANGED
@@ -336,6 +336,12 @@ initialized with provided configuration.
336
336
  > The Adapter will take precedence over the other options. Therefore, ensure you're providing the Adapter all the
337
337
  > information it needs to issue the request (e.g., host or auth headers)
338
338
 
339
+ #### throttle (default: `0`)
340
+ Maximum number of requests per second.
341
+ - `1`-`30` (fixed number of limit),
342
+ - `'auto'` (calculated limit based on your plan),
343
+ - `'0%'` - `'100%'` (calculated % limit based on your plan)
344
+
339
345
  ### Reference documentation
340
346
 
341
347
  The [Contentful's JS library reference](https://contentful.github.io/contentful-management.js) documents what objects and methods are exposed by this library, what arguments they expect and what kind of data is returned.
@@ -2294,10 +2294,13 @@ __webpack_require__.r(__webpack_exports__);
2294
2294
  /* harmony import */ var fast_copy__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(fast_copy__WEBPACK_IMPORTED_MODULE_0__);
2295
2295
  /* harmony import */ var qs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! qs */ "../node_modules/qs/lib/index.js");
2296
2296
  /* harmony import */ var qs__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(qs__WEBPACK_IMPORTED_MODULE_1__);
2297
- /* harmony import */ var os__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! os */ 2);
2298
- /* harmony import */ var os__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(os__WEBPACK_IMPORTED_MODULE_2__);
2299
- /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! lodash */ "../node_modules/lodash/lodash.js");
2300
- /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_3__);
2297
+ /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! lodash */ "../node_modules/lodash/lodash.js");
2298
+ /* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_2__);
2299
+ /* harmony import */ var p_throttle__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! p-throttle */ "../node_modules/p-throttle/index.js");
2300
+ /* harmony import */ var p_throttle__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(p_throttle__WEBPACK_IMPORTED_MODULE_3__);
2301
+ /* harmony import */ var os__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! os */ 2);
2302
+ /* harmony import */ var os__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(os__WEBPACK_IMPORTED_MODULE_4__);
2303
+
2301
2304
 
2302
2305
 
2303
2306
 
@@ -2357,6 +2360,69 @@ function _typeof(obj) {
2357
2360
  return _typeof(obj);
2358
2361
  }
2359
2362
 
2363
+ function _wrapRegExp() {
2364
+ _wrapRegExp = function (re, groups) {
2365
+ return new BabelRegExp(re, undefined, groups);
2366
+ };
2367
+
2368
+ var _super = RegExp.prototype;
2369
+
2370
+ var _groups = new WeakMap();
2371
+
2372
+ function BabelRegExp(re, flags, groups) {
2373
+ var _this = new RegExp(re, flags);
2374
+
2375
+ _groups.set(_this, groups || _groups.get(re));
2376
+
2377
+ return _setPrototypeOf(_this, BabelRegExp.prototype);
2378
+ }
2379
+
2380
+ _inherits(BabelRegExp, RegExp);
2381
+
2382
+ BabelRegExp.prototype.exec = function (str) {
2383
+ var result = _super.exec.call(this, str);
2384
+
2385
+ if (result) result.groups = buildGroups(result, this);
2386
+ return result;
2387
+ };
2388
+
2389
+ BabelRegExp.prototype[Symbol.replace] = function (str, substitution) {
2390
+ if (typeof substitution === "string") {
2391
+ var groups = _groups.get(this);
2392
+
2393
+ return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) {
2394
+ return "$" + groups[name];
2395
+ }));
2396
+ } else if (typeof substitution === "function") {
2397
+ var _this = this;
2398
+
2399
+ return _super[Symbol.replace].call(this, str, function () {
2400
+ var args = arguments;
2401
+
2402
+ if (typeof args[args.length - 1] !== "object") {
2403
+ args = [].slice.call(args);
2404
+ args.push(buildGroups(args, _this));
2405
+ }
2406
+
2407
+ return substitution.apply(this, args);
2408
+ });
2409
+ } else {
2410
+ return _super[Symbol.replace].call(this, str, substitution);
2411
+ }
2412
+ };
2413
+
2414
+ function buildGroups(result, re) {
2415
+ var g = _groups.get(re);
2416
+
2417
+ return Object.keys(g).reduce(function (groups, name) {
2418
+ groups[name] = result[g[name]];
2419
+ return groups;
2420
+ }, Object.create(null));
2421
+ }
2422
+
2423
+ return _wrapRegExp.apply(this, arguments);
2424
+ }
2425
+
2360
2426
  function _defineProperty(obj, key, value) {
2361
2427
  if (key in obj) {
2362
2428
  Object.defineProperty(obj, key, {
@@ -2372,6 +2438,30 @@ function _defineProperty(obj, key, value) {
2372
2438
  return obj;
2373
2439
  }
2374
2440
 
2441
+ function _inherits(subClass, superClass) {
2442
+ if (typeof superClass !== "function" && superClass !== null) {
2443
+ throw new TypeError("Super expression must either be null or a function");
2444
+ }
2445
+
2446
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
2447
+ constructor: {
2448
+ value: subClass,
2449
+ writable: true,
2450
+ configurable: true
2451
+ }
2452
+ });
2453
+ if (superClass) _setPrototypeOf(subClass, superClass);
2454
+ }
2455
+
2456
+ function _setPrototypeOf(o, p) {
2457
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
2458
+ o.__proto__ = p;
2459
+ return o;
2460
+ };
2461
+
2462
+ return _setPrototypeOf(o, p);
2463
+ }
2464
+
2375
2465
  function _slicedToArray(arr, i) {
2376
2466
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
2377
2467
  }
@@ -2488,13 +2578,82 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
2488
2578
  };
2489
2579
  }
2490
2580
 
2491
- var attempts = {};
2492
- var networkErrorAttempts = 0;
2581
+ var PERCENTAGE_REGEX = /*#__PURE__*/_wrapRegExp(/([0-9]+)(%)/, {
2582
+ value: 1
2583
+ });
2584
+
2585
+ function calculateLimit(type) {
2586
+ var max = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 7;
2587
+ var limit = max;
2588
+
2589
+ if (PERCENTAGE_REGEX.test(type)) {
2590
+ var _type$match;
2493
2591
 
2494
- function noop() {
2495
- return undefined;
2592
+ var groups = (_type$match = type.match(PERCENTAGE_REGEX)) === null || _type$match === void 0 ? void 0 : _type$match.groups;
2593
+
2594
+ if (groups && groups.value) {
2595
+ var percentage = parseInt(groups.value) / 100;
2596
+ limit = Math.round(max * percentage);
2597
+ }
2598
+ }
2599
+
2600
+ return Math.min(30, Math.max(1, limit));
2496
2601
  }
2497
2602
 
2603
+ function createThrottle(limit, logger) {
2604
+ logger('info', "Throttle request to ".concat(limit, "/s"));
2605
+ return p_throttle__WEBPACK_IMPORTED_MODULE_3___default()({
2606
+ limit: limit,
2607
+ interval: 1000,
2608
+ strict: false
2609
+ });
2610
+ }
2611
+
2612
+ var rateLimitThrottle = (function (axiosInstance) {
2613
+ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'auto';
2614
+ var _axiosInstance$defaul = axiosInstance.defaults.logHandler,
2615
+ logHandler = _axiosInstance$defaul === void 0 ? lodash__WEBPACK_IMPORTED_MODULE_2__["noop"] : _axiosInstance$defaul;
2616
+ var limit = Object(lodash__WEBPACK_IMPORTED_MODULE_2__["isString"])(type) ? calculateLimit(type) : calculateLimit('auto', type);
2617
+ var throttle = createThrottle(limit, logHandler);
2618
+ var isCalculated = false;
2619
+ var requestInterceptorId = axiosInstance.interceptors.request.use(function (config) {
2620
+ return throttle(function () {
2621
+ return config;
2622
+ })();
2623
+ }, Promise.reject);
2624
+ var responseInterceptorId = axiosInstance.interceptors.response.use(function (response) {
2625
+ if (!isCalculated && Object(lodash__WEBPACK_IMPORTED_MODULE_2__["isString"])(type) && (type === 'auto' || PERCENTAGE_REGEX.test(type)) && response.headers && response.headers['x-contentful-ratelimit-second-limit']) {
2626
+ var rawLimit = parseInt(response.headers['x-contentful-ratelimit-second-limit']);
2627
+ var nextLimit = calculateLimit(type, rawLimit);
2628
+
2629
+ if (nextLimit !== limit) {
2630
+ if (requestInterceptorId) {
2631
+ axiosInstance.interceptors.request.eject(requestInterceptorId);
2632
+ }
2633
+
2634
+ limit = nextLimit;
2635
+ throttle = createThrottle(nextLimit, logHandler);
2636
+ requestInterceptorId = axiosInstance.interceptors.request.use(function (config) {
2637
+ return throttle(function () {
2638
+ return config;
2639
+ })();
2640
+ }, Promise.reject);
2641
+ }
2642
+
2643
+ isCalculated = true;
2644
+ }
2645
+
2646
+ return response;
2647
+ }, Promise.reject);
2648
+ return function () {
2649
+ axiosInstance.interceptors.request.eject(requestInterceptorId);
2650
+ axiosInstance.interceptors.response.eject(responseInterceptorId);
2651
+ };
2652
+ });
2653
+
2654
+ var attempts = {};
2655
+ var networkErrorAttempts = 0;
2656
+
2498
2657
  var delay = function delay(ms) {
2499
2658
  return new Promise(function (resolve) {
2500
2659
  setTimeout(resolve, ms);
@@ -2505,9 +2664,9 @@ function rateLimit(instance) {
2505
2664
  var maxRetry = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
2506
2665
  var _instance$defaults = instance.defaults,
2507
2666
  _instance$defaults$re = _instance$defaults.responseLogger,
2508
- responseLogger = _instance$defaults$re === void 0 ? noop : _instance$defaults$re,
2667
+ responseLogger = _instance$defaults$re === void 0 ? lodash__WEBPACK_IMPORTED_MODULE_2__["noop"] : _instance$defaults$re,
2509
2668
  _instance$defaults$re2 = _instance$defaults.requestLogger,
2510
- requestLogger = _instance$defaults$re2 === void 0 ? noop : _instance$defaults$re2;
2669
+ requestLogger = _instance$defaults$re2 === void 0 ? lodash__WEBPACK_IMPORTED_MODULE_2__["noop"] : _instance$defaults$re2;
2511
2670
  instance.interceptors.request.use(function (config) {
2512
2671
  requestLogger(config);
2513
2672
  return config;
@@ -2529,7 +2688,7 @@ function rateLimit(instance) {
2529
2688
  }
2530
2689
 
2531
2690
  var retryErrorType = null;
2532
- var wait = 0; // Errors without response did not recieve anything from the server
2691
+ var wait = 0; // Errors without response did not receive anything from the server
2533
2692
 
2534
2693
  if (!response) {
2535
2694
  retryErrorType = 'Connection';
@@ -2652,6 +2811,7 @@ function createHttpClient(axios, options) {
2652
2811
  httpAgent: false,
2653
2812
  httpsAgent: false,
2654
2813
  timeout: 30000,
2814
+ throttle: 0,
2655
2815
  proxy: false,
2656
2816
  basePath: '',
2657
2817
  adapter: undefined,
@@ -2756,6 +2916,10 @@ function createHttpClient(axios, options) {
2756
2916
  asyncToken(instance, config.accessToken);
2757
2917
  }
2758
2918
 
2919
+ if (config.throttle) {
2920
+ rateLimitThrottle(instance, config.throttle);
2921
+ }
2922
+
2759
2923
  rateLimit(instance, config.retryLimit);
2760
2924
 
2761
2925
  if (config.onError) {
@@ -2855,8 +3019,8 @@ function getBrowserOS() {
2855
3019
  }
2856
3020
 
2857
3021
  function getNodeOS() {
2858
- var platform = os__WEBPACK_IMPORTED_MODULE_2___default.a.platform() || 'linux';
2859
- var version = os__WEBPACK_IMPORTED_MODULE_2___default.a.release() || '0.0.0';
3022
+ var platform = os__WEBPACK_IMPORTED_MODULE_4___default.a.platform() || 'linux';
3023
+ var version = os__WEBPACK_IMPORTED_MODULE_4___default.a.release() || '0.0.0';
2860
3024
  var platformMap = {
2861
3025
  android: 'Android',
2862
3026
  aix: 'Linux',
@@ -2953,7 +3117,7 @@ function errorHandler(errorResponse) {
2953
3117
  config.headers['Authorization'] = "Bearer ".concat(token);
2954
3118
  }
2955
3119
 
2956
- if (!Object(lodash__WEBPACK_IMPORTED_MODULE_3__["isPlainObject"])(response) || !Object(lodash__WEBPACK_IMPORTED_MODULE_3__["isPlainObject"])(config)) {
3120
+ if (!Object(lodash__WEBPACK_IMPORTED_MODULE_2__["isPlainObject"])(response) || !Object(lodash__WEBPACK_IMPORTED_MODULE_2__["isPlainObject"])(config)) {
2957
3121
  throw errorResponse;
2958
3122
  }
2959
3123
 
@@ -2965,7 +3129,7 @@ function errorHandler(errorResponse) {
2965
3129
  details: {}
2966
3130
  };
2967
3131
 
2968
- if (Object(lodash__WEBPACK_IMPORTED_MODULE_3__["isPlainObject"])(config)) {
3132
+ if (Object(lodash__WEBPACK_IMPORTED_MODULE_2__["isPlainObject"])(config)) {
2969
3133
  errorData.request = {
2970
3134
  url: config.url,
2971
3135
  headers: config.headers,
@@ -2974,7 +3138,7 @@ function errorHandler(errorResponse) {
2974
3138
  };
2975
3139
  }
2976
3140
 
2977
- if (data && Object(lodash__WEBPACK_IMPORTED_MODULE_3__["isPlainObject"])(data)) {
3141
+ if (data && Object(lodash__WEBPACK_IMPORTED_MODULE_2__["isPlainObject"])(data)) {
2978
3142
  if ('requestId' in data) {
2979
3143
  errorData.requestId = data.requestId || 'UNKNOWN';
2980
3144
  }
@@ -21581,6 +21745,120 @@ function arrObjKeys(obj, inspect) {
21581
21745
  }
21582
21746
 
21583
21747
 
21748
+ /***/ }),
21749
+
21750
+ /***/ "../node_modules/p-throttle/index.js":
21751
+ /*!*******************************************!*\
21752
+ !*** ../node_modules/p-throttle/index.js ***!
21753
+ \*******************************************/
21754
+ /*! no static exports found */
21755
+ /***/ (function(module, exports, __webpack_require__) {
21756
+
21757
+ "use strict";
21758
+
21759
+
21760
+ class AbortError extends Error {
21761
+ constructor() {
21762
+ super('Throttled function aborted');
21763
+ this.name = 'AbortError';
21764
+ }
21765
+ }
21766
+
21767
+ const pThrottle = ({limit, interval, strict}) => {
21768
+ if (!Number.isFinite(limit)) {
21769
+ throw new TypeError('Expected `limit` to be a finite number');
21770
+ }
21771
+
21772
+ if (!Number.isFinite(interval)) {
21773
+ throw new TypeError('Expected `interval` to be a finite number');
21774
+ }
21775
+
21776
+ const queue = new Map();
21777
+
21778
+ let currentTick = 0;
21779
+ let activeCount = 0;
21780
+
21781
+ function windowedDelay() {
21782
+ const now = Date.now();
21783
+
21784
+ if ((now - currentTick) > interval) {
21785
+ activeCount = 1;
21786
+ currentTick = now;
21787
+ return 0;
21788
+ }
21789
+
21790
+ if (activeCount < limit) {
21791
+ activeCount++;
21792
+ } else {
21793
+ currentTick += interval;
21794
+ activeCount = 1;
21795
+ }
21796
+
21797
+ return currentTick - now;
21798
+ }
21799
+
21800
+ const strictTicks = [];
21801
+
21802
+ function strictDelay() {
21803
+ const now = Date.now();
21804
+
21805
+ if (strictTicks.length < limit) {
21806
+ strictTicks.push(now);
21807
+ return 0;
21808
+ }
21809
+
21810
+ const earliestTime = strictTicks.shift() + interval;
21811
+
21812
+ if (now >= earliestTime) {
21813
+ strictTicks.push(now);
21814
+ return 0;
21815
+ }
21816
+
21817
+ strictTicks.push(earliestTime);
21818
+ return earliestTime - now;
21819
+ }
21820
+
21821
+ const getDelay = strict ? strictDelay : windowedDelay;
21822
+
21823
+ return function_ => {
21824
+ const throttled = function (...args) {
21825
+ if (!throttled.isEnabled) {
21826
+ return (async () => function_.apply(this, args))();
21827
+ }
21828
+
21829
+ let timeout;
21830
+ return new Promise((resolve, reject) => {
21831
+ const execute = () => {
21832
+ resolve(function_.apply(this, args));
21833
+ queue.delete(timeout);
21834
+ };
21835
+
21836
+ timeout = setTimeout(execute, getDelay());
21837
+
21838
+ queue.set(timeout, reject);
21839
+ });
21840
+ };
21841
+
21842
+ throttled.abort = () => {
21843
+ for (const timeout of queue.keys()) {
21844
+ clearTimeout(timeout);
21845
+ queue.get(timeout)(new AbortError());
21846
+ }
21847
+
21848
+ queue.clear();
21849
+ strictTicks.splice(0, strictTicks.length);
21850
+ };
21851
+
21852
+ throttled.isEnabled = true;
21853
+
21854
+ return throttled;
21855
+ };
21856
+ };
21857
+
21858
+ module.exports = pThrottle;
21859
+ module.exports.AbortError = AbortError;
21860
+
21861
+
21584
21862
  /***/ }),
21585
21863
 
21586
21864
  /***/ "../node_modules/process/browser.js":
@@ -26336,7 +26614,7 @@ function createClient(params) {
26336
26614
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
26337
26615
  var sdkMain = opts.type === 'plain' ? 'contentful-management-plain.js' : 'contentful-management.js';
26338
26616
  var userAgent = Object(contentful_sdk_core__WEBPACK_IMPORTED_MODULE_0__["getUserAgentHeader"])( // @ts-expect-error
26339
- "".concat(sdkMain, "/").concat("7.41.0"), params.application, params.integration, params.feature);
26617
+ "".concat(sdkMain, "/").concat("7.42.2"), params.application, params.integration, params.feature);
26340
26618
  var adapter = Object(_create_adapter__WEBPACK_IMPORTED_MODULE_1__["createAdapter"])(params); // Parameters<?> and ReturnType<?> only return the types of the last overload
26341
26619
  // https://github.com/microsoft/TypeScript/issues/26591
26342
26620
  // @ts-expect-error