@next-core/brick-kit 2.107.4 → 2.108.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.108.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.107.4...@next-core/brick-kit@2.108.0) (2022-03-18)
7
+
8
+
9
+ ### Features
10
+
11
+ * React组件支持调用provider ([1fe6096](https://github.com/easyops-cn/next-core/commit/1fe60963f96b4cddd889837da3ae5dd19f472d46))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [2.107.4](https://github.com/easyops-cn/next-core/compare/@next-core/brick-kit@2.107.3...@next-core/brick-kit@2.107.4) (2022-03-15)
7
18
 
8
19
  **Note:** Version bump only for package @next-core/brick-kit
@@ -2647,7 +2647,7 @@
2647
2647
  }) : internalOptions;
2648
2648
  }
2649
2649
 
2650
- var _excluded$4 = ["children"],
2650
+ var _excluded$6 = ["children"],
2651
2651
  _excluded2$1 = ["items", "app"];
2652
2652
  var symbolAppId = Symbol("appId");
2653
2653
  // Caching menu requests to avoid flicker.
@@ -2859,7 +2859,7 @@
2859
2859
  var {
2860
2860
  children
2861
2861
  } = _ref,
2862
- rest = _objectWithoutProperties__default["default"](_ref, _excluded$4);
2862
+ rest = _objectWithoutProperties__default["default"](_ref, _excluded$6);
2863
2863
 
2864
2864
  return _objectSpread__default["default"](_objectSpread__default["default"]({}, yield computeRealValueWithOverrideApp(rest, rest[symbolAppId], context, kernel)), {}, {
2865
2865
  children: children && (yield computeMenuItemsWithOverrideApp(children, context, kernel))
@@ -3420,7 +3420,7 @@
3420
3420
  return kernel.loadDynamicBricksInBrickConf(brickConf);
3421
3421
  }
3422
3422
 
3423
- var _excluded$3 = ["extraQuery", "clear", "keepHash"];
3423
+ var _excluded$5 = ["extraQuery", "clear", "keepHash"];
3424
3424
  function historyExtended(browserHistory) {
3425
3425
  var {
3426
3426
  push: originalPush,
@@ -3436,7 +3436,7 @@
3436
3436
  clear,
3437
3437
  keepHash
3438
3438
  } = options,
3439
- state = _objectWithoutProperties__default["default"](options, _excluded$3);
3439
+ state = _objectWithoutProperties__default["default"](options, _excluded$5);
3440
3440
 
3441
3441
  var urlSearchParams = new URLSearchParams(clear ? "" : browserHistory.location.search);
3442
3442
  var params = {};
@@ -6606,7 +6606,7 @@
6606
6606
  }
6607
6607
  }
6608
6608
 
6609
- var _excluded$2 = ["feature_flags"];
6609
+ var _excluded$4 = ["feature_flags"];
6610
6610
  function standaloneBootstrap() {
6611
6611
  return _standaloneBootstrap.apply(this, arguments);
6612
6612
  }
@@ -6642,7 +6642,7 @@
6642
6642
  var {
6643
6643
  feature_flags: featureFlags
6644
6644
  } = sys_settings,
6645
- rest = _objectWithoutProperties__default["default"](sys_settings, _excluded$2);
6645
+ rest = _objectWithoutProperties__default["default"](sys_settings, _excluded$4);
6646
6646
 
6647
6647
  settings = _objectSpread__default["default"]({
6648
6648
  featureFlags
@@ -10334,7 +10334,7 @@
10334
10334
  }
10335
10335
  }
10336
10336
 
10337
- var _excluded$1 = ["properties", "slots"],
10337
+ var _excluded$3 = ["properties", "slots"],
10338
10338
  _excluded2 = ["ref", "slots"];
10339
10339
  function expandCustomTemplate(brickConf, proxyBrick, context) {
10340
10340
  var tplContext = new CustomTemplateContext(proxyBrick);
@@ -10375,7 +10375,7 @@
10375
10375
  properties: templateProperties,
10376
10376
  slots: externalSlots
10377
10377
  } = brickConf,
10378
- restBrickConf = _objectWithoutProperties__default["default"](brickConf, _excluded$1);
10378
+ restBrickConf = _objectWithoutProperties__default["default"](brickConf, _excluded$3);
10379
10379
 
10380
10380
  var newBrickConf = restBrickConf; // Get a copy of proxy for each instance of custom template.
10381
10381
 
@@ -12811,7 +12811,7 @@
12811
12811
  checkoutTplContext: lodash.noop
12812
12812
  };
12813
12813
 
12814
- var _excluded = ["type"];
12814
+ var _excluded$2 = ["type"];
12815
12815
  // Inspired by [LitElement](https://github.com/Polymer/lit-element)
12816
12816
 
12817
12817
  /**
@@ -13081,7 +13081,7 @@
13081
13081
  var {
13082
13082
  type
13083
13083
  } = options,
13084
- eventInit = _objectWithoutProperties__default["default"](options, _excluded);
13084
+ eventInit = _objectWithoutProperties__default["default"](options, _excluded$2);
13085
13085
 
13086
13086
  this.__dev_only_definedEvents.add(type); // Make event emitter readonly.
13087
13087
 
@@ -13173,6 +13173,242 @@
13173
13173
  };
13174
13174
  }, UpdatingElement);
13175
13175
 
13176
+ var useProviderArgsDefaults = {
13177
+ provider: "",
13178
+ customOptions: {
13179
+ onError: () => {
13180
+ /* Do nothing.. */
13181
+ },
13182
+ transform: (oldData, newData) => newData,
13183
+ data: undefined,
13184
+ loading: false,
13185
+ suspense: false
13186
+ },
13187
+ dependencies: undefined
13188
+ };
13189
+ var defaults = Object.entries(useProviderArgsDefaults).reduce((acc, _ref) => {
13190
+ var [key, value] = _ref;
13191
+ if (Object.prototype.toString.call(value) === "[object Object]") return _objectSpread__default["default"](_objectSpread__default["default"]({}, acc), value);
13192
+ return _objectSpread__default["default"](_objectSpread__default["default"]({}, acc), {}, {
13193
+ [key]: value
13194
+ });
13195
+ }, {});
13196
+
13197
+ var _excluded$1 = ["body"];
13198
+ function useProviderArgs(providerOrParamsOrGlobalOptions, gloabalOptionsOrDeps, deps) {
13199
+ var provider = React.useMemo(() => {
13200
+ if (typeof providerOrParamsOrGlobalOptions === "string") {
13201
+ return providerOrParamsOrGlobalOptions;
13202
+ }
13203
+
13204
+ return useProviderArgsDefaults.provider;
13205
+ }, [providerOrParamsOrGlobalOptions, gloabalOptionsOrDeps]);
13206
+ var options = React.useMemo(() => {
13207
+ var localOptions = {};
13208
+
13209
+ if (lodash.isObject(providerOrParamsOrGlobalOptions)) {
13210
+ localOptions = providerOrParamsOrGlobalOptions;
13211
+ } else if (lodash.isObject(gloabalOptionsOrDeps)) {
13212
+ localOptions = gloabalOptionsOrDeps;
13213
+ }
13214
+
13215
+ return _objectSpread__default["default"](_objectSpread__default["default"]({}, defaults), localOptions);
13216
+ }, [providerOrParamsOrGlobalOptions, gloabalOptionsOrDeps]);
13217
+ var requestInit = React.useMemo(() => {
13218
+ var customOptionKeys = [...Object.keys(useProviderArgsDefaults), ...Object.keys(useProviderArgsDefaults.customOptions)];
13219
+
13220
+ var _reduce = Object.keys(options).reduce((acc, key) => {
13221
+ if (!customOptionKeys.includes(key)) acc[key] = options[key];
13222
+ return acc;
13223
+ }, {}),
13224
+ {
13225
+ body = null
13226
+ } = _reduce,
13227
+ restOptions = _objectWithoutProperties__default["default"](_reduce, _excluded$1);
13228
+
13229
+ return {
13230
+ options: _objectSpread__default["default"]({}, restOptions),
13231
+ body
13232
+ };
13233
+ }, [options]);
13234
+ var dependencies = React.useMemo(() => {
13235
+ if (Array.isArray(gloabalOptionsOrDeps)) return gloabalOptionsOrDeps;
13236
+ if (Array.isArray(deps)) return deps;
13237
+ return defaults.dependencies;
13238
+ }, [gloabalOptionsOrDeps, deps]);
13239
+ var loading = options.loading || Array.isArray(dependencies);
13240
+ var customOptions = React.useMemo(() => {
13241
+ var customOptionKeys = Object.keys(useProviderArgsDefaults.customOptions);
13242
+ var customOptions = customOptionKeys.reduce((opts, key) => {
13243
+ opts[key] = options[key];
13244
+ return opts;
13245
+ }, {});
13246
+ return _objectSpread__default["default"](_objectSpread__default["default"]({}, customOptions), {}, {
13247
+ loading
13248
+ });
13249
+ }, [options]);
13250
+ return {
13251
+ provider,
13252
+ customOptions,
13253
+ requestInit,
13254
+ dependencies
13255
+ };
13256
+ }
13257
+
13258
+ var cache = new Map();
13259
+ function fetchProviderArgs(_x) {
13260
+ return _fetchProviderArgs.apply(this, arguments);
13261
+ }
13262
+
13263
+ function _fetchProviderArgs() {
13264
+ _fetchProviderArgs = _asyncToGenerator__default["default"](function* (provider) {
13265
+ for (var _len = arguments.length, originalArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
13266
+ originalArgs[_key - 1] = arguments[_key];
13267
+ }
13268
+
13269
+ var cacheKey = JSON.stringify({
13270
+ provider,
13271
+ originalArgs
13272
+ });
13273
+ if (cache.has(cacheKey)) return cache.get(cacheKey);
13274
+ var args = yield getArgsOfCustomApi(provider, originalArgs);
13275
+ cache.set(cacheKey, args);
13276
+ return args;
13277
+ });
13278
+ return _fetchProviderArgs.apply(this, arguments);
13279
+ }
13280
+
13281
+ var _excluded = ["onError", "transform", "suspense"];
13282
+ function useProvider() {
13283
+ var {
13284
+ provider,
13285
+ customOptions,
13286
+ dependencies,
13287
+ requestInit
13288
+ } = useProviderArgs(...arguments);
13289
+
13290
+ var {
13291
+ onError,
13292
+ transform,
13293
+ suspense
13294
+ } = customOptions,
13295
+ defaults = _objectWithoutProperties__default["default"](customOptions, _excluded);
13296
+
13297
+ var [loading, setLoading] = React.useState(defaults.loading);
13298
+ var suspenseStatus = React.useRef("pending");
13299
+ var suspender = React.useRef();
13300
+ var mounted = React.useRef(false);
13301
+ var error = React.useRef();
13302
+ var response = React.useRef();
13303
+ var data = React.useRef(defaults.data);
13304
+ var forceUpdate = React.useReducer(() => ({}), [])[1];
13305
+ var doFetch = React.useCallback( /*#__PURE__*/_asyncToGenerator__default["default"](function* () {
13306
+ try {
13307
+ error.current = undefined;
13308
+ if (!suspense) setLoading(true);
13309
+ var newRes = yield CustomApi(...arguments);
13310
+ response.current = newRes;
13311
+ data.current = transform(data.current, newRes);
13312
+ } catch (e) {
13313
+ error.current = e;
13314
+ }
13315
+
13316
+ if (!suspense) setLoading(false);
13317
+ if (error.current) onError(error.current);
13318
+ return data.current;
13319
+ }), [provider, customOptions, dependencies, requestInit, suspense, transform, defaults.data, onError]);
13320
+ var makeFetch = React.useCallback( /*#__PURE__*/function () {
13321
+ var _ref2 = _asyncToGenerator__default["default"](function* (providerOrBody, body) {
13322
+ var providerStr = provider;
13323
+ var data = {};
13324
+
13325
+ if (lodash.isString(providerOrBody)) {
13326
+ providerStr = providerOrBody;
13327
+ }
13328
+
13329
+ if (lodash.isObject(providerOrBody)) {
13330
+ data = providerOrBody;
13331
+ } else if (lodash.isObject(body)) {
13332
+ data = body;
13333
+ }
13334
+
13335
+ var args = yield fetchProviderArgs(providerStr, data, requestInit.options);
13336
+
13337
+ if (suspense) {
13338
+ return _asyncToGenerator__default["default"](function* () {
13339
+ suspender.current = doFetch(...args).then(newData => {
13340
+ suspenseStatus.current = "success";
13341
+ return newData;
13342
+ }, error => {
13343
+ suspenseStatus.current = "error";
13344
+ error.current = error;
13345
+ return error;
13346
+ });
13347
+ forceUpdate();
13348
+ return yield suspender.current;
13349
+ })();
13350
+ }
13351
+
13352
+ return doFetch(...args);
13353
+ });
13354
+
13355
+ return function (_x, _x2) {
13356
+ return _ref2.apply(this, arguments);
13357
+ };
13358
+ }(), [doFetch]);
13359
+ var request = React.useMemo(() => Object.defineProperties({
13360
+ query: makeFetch
13361
+ }, {
13362
+ loading: {
13363
+ get() {
13364
+ return loading;
13365
+ }
13366
+
13367
+ },
13368
+ data: {
13369
+ get() {
13370
+ return data.current;
13371
+ }
13372
+
13373
+ },
13374
+ error: {
13375
+ get() {
13376
+ return error.current;
13377
+ }
13378
+
13379
+ }
13380
+ }), [makeFetch]); // onMount/onUpdate
13381
+
13382
+ React.useEffect(() => {
13383
+ mounted.current = true;
13384
+
13385
+ if (Array.isArray(dependencies) && provider) {
13386
+ request.query(provider, requestInit.body);
13387
+ }
13388
+
13389
+ return () => mounted.current = false;
13390
+ }, dependencies);
13391
+
13392
+ if (suspense && suspender.current) {
13393
+ switch (suspenseStatus.current) {
13394
+ case "pending":
13395
+ throw suspender.current;
13396
+
13397
+ case "error":
13398
+ throw error.current;
13399
+ }
13400
+ }
13401
+
13402
+ return Object.assign([request, response.current, loading, error.current], _objectSpread__default["default"](_objectSpread__default["default"]({
13403
+ request
13404
+ }, request), {}, {
13405
+ response: response.current,
13406
+ data: data.current,
13407
+ loading,
13408
+ error: error.current
13409
+ }));
13410
+ }
13411
+
13176
13412
  exports.BrickAsComponent = BrickAsComponent;
13177
13413
  exports.BrickWrapper = BrickWrapper;
13178
13414
  exports.DisplayByFeatureFlags = DisplayByFeatureFlags;
@@ -13222,6 +13458,7 @@
13222
13458
  exports.useCurrentTheme = useCurrentTheme;
13223
13459
  exports.useFeatureFlags = useFeatureFlags;
13224
13460
  exports.useLocation = useLocation;
13461
+ exports.useProvider = useProvider;
13225
13462
  exports.useRecentApps = useRecentApps;
13226
13463
 
13227
13464
  Object.defineProperty(exports, '__esModule', { value: true });