@builder.io/sdk 3.0.2 → 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.
package/dist/index.cjs.js CHANGED
@@ -126,7 +126,7 @@ function assertAllowedPropertyName(name) {
126
126
  throw new Error("Property name \"".concat(name, "\" is not allowed"));
127
127
  }
128
128
 
129
- var version = "3.0.2";
129
+ var version = "3.0.4";
130
130
 
131
131
  var Subscription = /** @class */ (function () {
132
132
  function Subscription(listeners, listener) {
@@ -2278,6 +2278,26 @@ var Builder = /** @class */ (function () {
2278
2278
  enumerable: false,
2279
2279
  configurable: true
2280
2280
  });
2281
+ Builder.prototype.makeFetchApiCall = function (url, requestOptions) {
2282
+ return getFetch()(url, requestOptions);
2283
+ };
2284
+ Builder.prototype.flattenMongoQuery = function (obj, _current, _res) {
2285
+ if (_res === void 0) { _res = {}; }
2286
+ for (var key in obj) {
2287
+ var value = obj[key];
2288
+ var newKey = _current ? _current + '.' + key : key;
2289
+ if (value &&
2290
+ typeof value === 'object' &&
2291
+ !Array.isArray(value) &&
2292
+ !Object.keys(value).find(function (item) { return item.startsWith('$'); })) {
2293
+ this.flattenMongoQuery(value, newKey, _res);
2294
+ }
2295
+ else {
2296
+ _res[newKey] = value;
2297
+ }
2298
+ }
2299
+ return _res;
2300
+ };
2281
2301
  Builder.prototype.flushGetContentQueue = function (usePastQueue, useQueue) {
2282
2302
  var _this = this;
2283
2303
  if (usePastQueue === void 0) { usePastQueue = false; }
@@ -2296,6 +2316,7 @@ var Builder = /** @class */ (function () {
2296
2316
  return;
2297
2317
  }
2298
2318
  var queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];
2319
+ var apiEndpoint = queue[0].apiEndpoint || 'query';
2299
2320
  // TODO: do this on every request send?
2300
2321
  this.getOverridesFromQueryString();
2301
2322
  var queryParams = tslib.__assign(tslib.__assign({
@@ -2372,8 +2393,9 @@ var Builder = /** @class */ (function () {
2372
2393
  }
2373
2394
  for (var _i = 0, queue_2 = queue; _i < queue_2.length; _i++) {
2374
2395
  var options = queue_2[_i];
2375
- if (options.format) {
2376
- queryParams.format = options.format;
2396
+ var format_1 = options.format;
2397
+ if (format_1) {
2398
+ queryParams.format = format_1;
2377
2399
  }
2378
2400
  // TODO: remove me and make permodel
2379
2401
  if (options.static) {
@@ -2404,9 +2426,14 @@ var Builder = /** @class */ (function () {
2404
2426
  var key = properties_1[_a];
2405
2427
  var value = options[key];
2406
2428
  if (value !== undefined) {
2407
- queryParams.options = queryParams.options || {};
2408
- queryParams.options[options.key] = queryParams.options[options.key] || {};
2409
- queryParams.options[options.key][key] = JSON.stringify(value);
2429
+ if (apiEndpoint === 'query') {
2430
+ queryParams.options = queryParams.options || {};
2431
+ queryParams.options[options.key] = queryParams.options[options.key] || {};
2432
+ queryParams.options[options.key][key] = JSON.stringify(value);
2433
+ }
2434
+ else {
2435
+ queryParams[key] = JSON.stringify(value);
2436
+ }
2410
2437
  }
2411
2438
  }
2412
2439
  }
@@ -2421,18 +2448,36 @@ var Builder = /** @class */ (function () {
2421
2448
  var params = omit(QueryString.parse(this.overrideParams), 'apiKey');
2422
2449
  assign(queryParams, params);
2423
2450
  }
2424
- var queryStr = QueryString.stringifyDeep(queryParams);
2425
2451
  var format = queryParams.format;
2452
+ var isApiCallForCodegen = format === 'solid' || format === 'react';
2453
+ var isApiCallForCodegenOrQuery = isApiCallForCodegen || apiEndpoint === 'query';
2454
+ if (apiEndpoint === 'content') {
2455
+ queryParams.enrich = true;
2456
+ if (queue[0].query) {
2457
+ var flattened = this.flattenMongoQuery({ query: queue[0].query });
2458
+ for (var key in flattened) {
2459
+ queryParams[key] = flattened[key];
2460
+ }
2461
+ delete queryParams.query;
2462
+ }
2463
+ }
2464
+ var queryStr = QueryString.stringifyDeep(queryParams);
2426
2465
  var requestOptions = { headers: {} };
2427
2466
  if (this.authToken) {
2428
2467
  requestOptions.headers = tslib.__assign(tslib.__assign({}, requestOptions.headers), { Authorization: "Bearer ".concat(this.authToken) });
2429
2468
  }
2430
- var fn = format === 'solid' || format === 'react' ? 'codegen' : 'query';
2431
- // NOTE: this is a hack to get around the fact that the codegen endpoint is not yet available in v3
2432
- var apiVersionBasedOnFn = fn === 'query' ? this.apiVersion : 'v1';
2433
- var url = "".concat(host, "/api/").concat(apiVersionBasedOnFn, "/").concat(fn, "/").concat(this.apiKey, "/").concat(keyNames) +
2434
- (queryParams && hasParams ? "?".concat(queryStr) : '');
2435
- var promise = getFetch()(url, requestOptions)
2469
+ var url;
2470
+ if (isApiCallForCodegen) {
2471
+ url = "".concat(host, "/api/v1/codegen/").concat(this.apiKey, "/").concat(keyNames);
2472
+ }
2473
+ else if (apiEndpoint === 'query') {
2474
+ url = "".concat(host, "/api/v3/query/").concat(this.apiKey, "/").concat(keyNames);
2475
+ }
2476
+ else {
2477
+ url = "".concat(host, "/api/v3/content/").concat(queue[0].model);
2478
+ }
2479
+ url = url + (queryParams && hasParams ? "?".concat(queryStr) : '');
2480
+ var promise = this.makeFetchApiCall(url, requestOptions)
2436
2481
  .then(function (res) { return res.json(); })
2437
2482
  .then(function (result) {
2438
2483
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
@@ -2450,7 +2495,7 @@ var Builder = /** @class */ (function () {
2450
2495
  if (!observer) {
2451
2496
  return;
2452
2497
  }
2453
- var data = result[keyName];
2498
+ var data = isApiCallForCodegenOrQuery ? result[keyName] : result.results;
2454
2499
  var sorted = data; // sortBy(data, item => item.priority);
2455
2500
  if (data) {
2456
2501
  var testModifiedResults = Builder.isServer