@builder.io/sdk 3.0.2 → 3.0.3

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/dist/index.umd.js CHANGED
@@ -152,7 +152,7 @@
152
152
  throw new Error("Property name \"".concat(name, "\" is not allowed"));
153
153
  }
154
154
 
155
- var version = "3.0.2";
155
+ var version = "3.0.3";
156
156
 
157
157
  var Subscription = /** @class */ (function () {
158
158
  function Subscription(listeners, listener) {
@@ -2372,6 +2372,26 @@
2372
2372
  enumerable: false,
2373
2373
  configurable: true
2374
2374
  });
2375
+ Builder.prototype.makeFetchApiCall = function (url, requestOptions) {
2376
+ return getFetch()(url, requestOptions);
2377
+ };
2378
+ Builder.prototype.flattenMongoQuery = function (obj, _current, _res) {
2379
+ if (_res === void 0) { _res = {}; }
2380
+ for (var key in obj) {
2381
+ var value = obj[key];
2382
+ var newKey = _current ? _current + '.' + key : key;
2383
+ if (value &&
2384
+ typeof value === 'object' &&
2385
+ !Array.isArray(value) &&
2386
+ !Object.keys(value).find(function (item) { return item.startsWith('$'); })) {
2387
+ this.flattenMongoQuery(value, newKey, _res);
2388
+ }
2389
+ else {
2390
+ _res[newKey] = value;
2391
+ }
2392
+ }
2393
+ return _res;
2394
+ };
2375
2395
  Builder.prototype.flushGetContentQueue = function (usePastQueue, useQueue) {
2376
2396
  var _this = this;
2377
2397
  if (usePastQueue === void 0) { usePastQueue = false; }
@@ -2390,6 +2410,7 @@
2390
2410
  return;
2391
2411
  }
2392
2412
  var queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];
2413
+ var apiEndpoint = queue[0].apiEndpoint || 'query';
2393
2414
  // TODO: do this on every request send?
2394
2415
  this.getOverridesFromQueryString();
2395
2416
  var queryParams = __assign(__assign({
@@ -2466,8 +2487,9 @@
2466
2487
  }
2467
2488
  for (var _i = 0, queue_2 = queue; _i < queue_2.length; _i++) {
2468
2489
  var options = queue_2[_i];
2469
- if (options.format) {
2470
- queryParams.format = options.format;
2490
+ var format_1 = options.format;
2491
+ if (format_1) {
2492
+ queryParams.format = format_1;
2471
2493
  }
2472
2494
  // TODO: remove me and make permodel
2473
2495
  if (options.static) {
@@ -2498,9 +2520,14 @@
2498
2520
  var key = properties_1[_a];
2499
2521
  var value = options[key];
2500
2522
  if (value !== undefined) {
2501
- queryParams.options = queryParams.options || {};
2502
- queryParams.options[options.key] = queryParams.options[options.key] || {};
2503
- queryParams.options[options.key][key] = JSON.stringify(value);
2523
+ if (apiEndpoint === 'query') {
2524
+ queryParams.options = queryParams.options || {};
2525
+ queryParams.options[options.key] = queryParams.options[options.key] || {};
2526
+ queryParams.options[options.key][key] = JSON.stringify(value);
2527
+ }
2528
+ else {
2529
+ queryParams[key] = JSON.stringify(value);
2530
+ }
2504
2531
  }
2505
2532
  }
2506
2533
  }
@@ -2515,18 +2542,36 @@
2515
2542
  var params = omit(QueryString.parse(this.overrideParams), 'apiKey');
2516
2543
  assign(queryParams, params);
2517
2544
  }
2518
- var queryStr = QueryString.stringifyDeep(queryParams);
2519
2545
  var format = queryParams.format;
2546
+ var isApiCallForCodegen = format === 'solid' || format === 'react';
2547
+ var isApiCallForCodegenOrQuery = isApiCallForCodegen || apiEndpoint === 'query';
2548
+ if (apiEndpoint === 'content') {
2549
+ queryParams.enrich = true;
2550
+ if (queue[0].query) {
2551
+ var flattened = this.flattenMongoQuery({ query: queue[0].query });
2552
+ for (var key in flattened) {
2553
+ queryParams[key] = flattened[key];
2554
+ }
2555
+ delete queryParams.query;
2556
+ }
2557
+ }
2558
+ var queryStr = QueryString.stringifyDeep(queryParams);
2520
2559
  var requestOptions = { headers: {} };
2521
2560
  if (this.authToken) {
2522
2561
  requestOptions.headers = __assign(__assign({}, requestOptions.headers), { Authorization: "Bearer ".concat(this.authToken) });
2523
2562
  }
2524
- var fn = format === 'solid' || format === 'react' ? 'codegen' : 'query';
2525
- // NOTE: this is a hack to get around the fact that the codegen endpoint is not yet available in v3
2526
- var apiVersionBasedOnFn = fn === 'query' ? this.apiVersion : 'v1';
2527
- var url = "".concat(host, "/api/").concat(apiVersionBasedOnFn, "/").concat(fn, "/").concat(this.apiKey, "/").concat(keyNames) +
2528
- (queryParams && hasParams ? "?".concat(queryStr) : '');
2529
- var promise = getFetch()(url, requestOptions)
2563
+ var url;
2564
+ if (isApiCallForCodegen) {
2565
+ url = "".concat(host, "/api/v1/codegen/").concat(this.apiKey, "/").concat(keyNames);
2566
+ }
2567
+ else if (apiEndpoint === 'query') {
2568
+ url = "".concat(host, "/api/v3/query/").concat(this.apiKey, "/").concat(keyNames);
2569
+ }
2570
+ else {
2571
+ url = "".concat(host, "/api/v3/content/").concat(queue[0].model);
2572
+ }
2573
+ url = url + (queryParams && hasParams ? "?".concat(queryStr) : '');
2574
+ var promise = this.makeFetchApiCall(url, requestOptions)
2530
2575
  .then(function (res) { return res.json(); })
2531
2576
  .then(function (result) {
2532
2577
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
@@ -2544,7 +2589,7 @@
2544
2589
  if (!observer) {
2545
2590
  return;
2546
2591
  }
2547
- var data = result[keyName];
2592
+ var data = isApiCallForCodegenOrQuery ? result[keyName] : result.results;
2548
2593
  var sorted = data; // sortBy(data, item => item.priority);
2549
2594
  if (data) {
2550
2595
  var testModifiedResults = Builder.isServer