@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.esm.js CHANGED
@@ -118,7 +118,7 @@ function assertAllowedPropertyName(name) {
118
118
  throw new Error("Property name \"".concat(name, "\" is not allowed"));
119
119
  }
120
120
 
121
- var version = "3.0.2";
121
+ var version = "3.0.3";
122
122
 
123
123
  var Subscription = /** @class */ (function () {
124
124
  function Subscription(listeners, listener) {
@@ -2270,6 +2270,26 @@ var Builder = /** @class */ (function () {
2270
2270
  enumerable: false,
2271
2271
  configurable: true
2272
2272
  });
2273
+ Builder.prototype.makeFetchApiCall = function (url, requestOptions) {
2274
+ return getFetch()(url, requestOptions);
2275
+ };
2276
+ Builder.prototype.flattenMongoQuery = function (obj, _current, _res) {
2277
+ if (_res === void 0) { _res = {}; }
2278
+ for (var key in obj) {
2279
+ var value = obj[key];
2280
+ var newKey = _current ? _current + '.' + key : key;
2281
+ if (value &&
2282
+ typeof value === 'object' &&
2283
+ !Array.isArray(value) &&
2284
+ !Object.keys(value).find(function (item) { return item.startsWith('$'); })) {
2285
+ this.flattenMongoQuery(value, newKey, _res);
2286
+ }
2287
+ else {
2288
+ _res[newKey] = value;
2289
+ }
2290
+ }
2291
+ return _res;
2292
+ };
2273
2293
  Builder.prototype.flushGetContentQueue = function (usePastQueue, useQueue) {
2274
2294
  var _this = this;
2275
2295
  if (usePastQueue === void 0) { usePastQueue = false; }
@@ -2288,6 +2308,7 @@ var Builder = /** @class */ (function () {
2288
2308
  return;
2289
2309
  }
2290
2310
  var queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];
2311
+ var apiEndpoint = queue[0].apiEndpoint || 'query';
2291
2312
  // TODO: do this on every request send?
2292
2313
  this.getOverridesFromQueryString();
2293
2314
  var queryParams = __assign(__assign({
@@ -2364,8 +2385,9 @@ var Builder = /** @class */ (function () {
2364
2385
  }
2365
2386
  for (var _i = 0, queue_2 = queue; _i < queue_2.length; _i++) {
2366
2387
  var options = queue_2[_i];
2367
- if (options.format) {
2368
- queryParams.format = options.format;
2388
+ var format_1 = options.format;
2389
+ if (format_1) {
2390
+ queryParams.format = format_1;
2369
2391
  }
2370
2392
  // TODO: remove me and make permodel
2371
2393
  if (options.static) {
@@ -2396,9 +2418,14 @@ var Builder = /** @class */ (function () {
2396
2418
  var key = properties_1[_a];
2397
2419
  var value = options[key];
2398
2420
  if (value !== undefined) {
2399
- queryParams.options = queryParams.options || {};
2400
- queryParams.options[options.key] = queryParams.options[options.key] || {};
2401
- queryParams.options[options.key][key] = JSON.stringify(value);
2421
+ if (apiEndpoint === 'query') {
2422
+ queryParams.options = queryParams.options || {};
2423
+ queryParams.options[options.key] = queryParams.options[options.key] || {};
2424
+ queryParams.options[options.key][key] = JSON.stringify(value);
2425
+ }
2426
+ else {
2427
+ queryParams[key] = JSON.stringify(value);
2428
+ }
2402
2429
  }
2403
2430
  }
2404
2431
  }
@@ -2413,18 +2440,36 @@ var Builder = /** @class */ (function () {
2413
2440
  var params = omit(QueryString.parse(this.overrideParams), 'apiKey');
2414
2441
  assign(queryParams, params);
2415
2442
  }
2416
- var queryStr = QueryString.stringifyDeep(queryParams);
2417
2443
  var format = queryParams.format;
2444
+ var isApiCallForCodegen = format === 'solid' || format === 'react';
2445
+ var isApiCallForCodegenOrQuery = isApiCallForCodegen || apiEndpoint === 'query';
2446
+ if (apiEndpoint === 'content') {
2447
+ queryParams.enrich = true;
2448
+ if (queue[0].query) {
2449
+ var flattened = this.flattenMongoQuery({ query: queue[0].query });
2450
+ for (var key in flattened) {
2451
+ queryParams[key] = flattened[key];
2452
+ }
2453
+ delete queryParams.query;
2454
+ }
2455
+ }
2456
+ var queryStr = QueryString.stringifyDeep(queryParams);
2418
2457
  var requestOptions = { headers: {} };
2419
2458
  if (this.authToken) {
2420
2459
  requestOptions.headers = __assign(__assign({}, requestOptions.headers), { Authorization: "Bearer ".concat(this.authToken) });
2421
2460
  }
2422
- var fn = format === 'solid' || format === 'react' ? 'codegen' : 'query';
2423
- // NOTE: this is a hack to get around the fact that the codegen endpoint is not yet available in v3
2424
- var apiVersionBasedOnFn = fn === 'query' ? this.apiVersion : 'v1';
2425
- var url = "".concat(host, "/api/").concat(apiVersionBasedOnFn, "/").concat(fn, "/").concat(this.apiKey, "/").concat(keyNames) +
2426
- (queryParams && hasParams ? "?".concat(queryStr) : '');
2427
- var promise = getFetch()(url, requestOptions)
2461
+ var url;
2462
+ if (isApiCallForCodegen) {
2463
+ url = "".concat(host, "/api/v1/codegen/").concat(this.apiKey, "/").concat(keyNames);
2464
+ }
2465
+ else if (apiEndpoint === 'query') {
2466
+ url = "".concat(host, "/api/v3/query/").concat(this.apiKey, "/").concat(keyNames);
2467
+ }
2468
+ else {
2469
+ url = "".concat(host, "/api/v3/content/").concat(queue[0].model);
2470
+ }
2471
+ url = url + (queryParams && hasParams ? "?".concat(queryStr) : '');
2472
+ var promise = this.makeFetchApiCall(url, requestOptions)
2428
2473
  .then(function (res) { return res.json(); })
2429
2474
  .then(function (result) {
2430
2475
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
@@ -2442,7 +2487,7 @@ var Builder = /** @class */ (function () {
2442
2487
  if (!observer) {
2443
2488
  return;
2444
2489
  }
2445
- var data = result[keyName];
2490
+ var data = isApiCallForCodegenOrQuery ? result[keyName] : result.results;
2446
2491
  var sorted = data; // sortBy(data, item => item.priority);
2447
2492
  if (data) {
2448
2493
  var testModifiedResults = Builder.isServer