@builder.io/sdk 3.0.6 → 4.0.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/dist/index.esm.js CHANGED
@@ -950,7 +950,7 @@ function toError(err) {
950
950
 
951
951
  var DEFAULT_API_VERSION = 'v3';
952
952
 
953
- var SDK_VERSION = '3.0.6';
953
+ var SDK_VERSION = '4.0.0';
954
954
 
955
955
  function datePlusMinutes(minutes) {
956
956
  if (minutes === void 0) { minutes = 30; }
@@ -1440,9 +1440,7 @@ var Builder = /** @class */ (function () {
1440
1440
  getFetch()("".concat(host, "/api/v1/track"), {
1441
1441
  method: 'POST',
1442
1442
  body: JSON.stringify({ events: events }),
1443
- headers: {
1444
- 'content-type': 'application/json',
1445
- },
1443
+ headers: __assign({ 'content-type': 'application/json' }, this.getSdkHeaders()),
1446
1444
  mode: 'cors',
1447
1445
  }).catch(function () {
1448
1446
  // Not the end of the world
@@ -2241,7 +2239,7 @@ var Builder = /** @class */ (function () {
2241
2239
  // even though we only use `fetch()` now, we prefer to keep the old behavior and use the `fetch` that comes from
2242
2240
  // the core SDK for consistency
2243
2241
  Builder.prototype.requestUrl = function (url, options) {
2244
- return getFetch()(url, options).then(function (res) { return res.json(); });
2242
+ return getFetch()(url, this.addSdkHeaders(options)).then(function (res) { return res.json(); });
2245
2243
  };
2246
2244
  Object.defineProperty(Builder.prototype, "host", {
2247
2245
  get: function () {
@@ -2270,8 +2268,21 @@ var Builder = /** @class */ (function () {
2270
2268
  enumerable: false,
2271
2269
  configurable: true
2272
2270
  });
2271
+ Builder.prototype.getSdkHeaders = function () {
2272
+ if (!Builder.sdkInfo) {
2273
+ return {};
2274
+ }
2275
+ return {
2276
+ 'X-Builder-SDK': Builder.sdkInfo.name,
2277
+ 'X-Builder-SDK-GEN': '1',
2278
+ 'X-Builder-SDK-Version': Builder.sdkInfo.version,
2279
+ };
2280
+ };
2281
+ Builder.prototype.addSdkHeaders = function (fetchOptions) {
2282
+ return __assign(__assign({}, fetchOptions), { headers: __assign(__assign({}, fetchOptions.headers), this.getSdkHeaders()) });
2283
+ };
2273
2284
  Builder.prototype.makeFetchApiCall = function (url, requestOptions) {
2274
- return getFetch()(url, requestOptions);
2285
+ return getFetch()(url, this.addSdkHeaders(requestOptions));
2275
2286
  };
2276
2287
  Builder.prototype.flattenMongoQuery = function (obj, _current, _res) {
2277
2288
  if (_res === void 0) { _res = {}; }
@@ -2292,6 +2303,7 @@ var Builder = /** @class */ (function () {
2292
2303
  };
2293
2304
  Builder.prototype.flushGetContentQueue = function (usePastQueue, useQueue) {
2294
2305
  var _this = this;
2306
+ var _a, _b;
2295
2307
  if (usePastQueue === void 0) { usePastQueue = false; }
2296
2308
  if (!this.apiKey) {
2297
2309
  throw new Error("Fetching content failed, expected apiKey to be defined instead got: ".concat(this.apiKey));
@@ -2308,7 +2320,6 @@ var Builder = /** @class */ (function () {
2308
2320
  return;
2309
2321
  }
2310
2322
  var queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];
2311
- var apiEndpoint = queue[0].apiEndpoint || 'query';
2312
2323
  // TODO: do this on every request send?
2313
2324
  this.getOverridesFromQueryString();
2314
2325
  var queryParams = __assign(__assign({
@@ -2383,6 +2394,7 @@ var Builder = /** @class */ (function () {
2383
2394
  }
2384
2395
  }
2385
2396
  }
2397
+ var isApiCallForCodegen = ((_a = queue[0].options) === null || _a === void 0 ? void 0 : _a.format) === 'solid' || ((_b = queue[0].options) === null || _b === void 0 ? void 0 : _b.format) === 'react';
2386
2398
  for (var _i = 0, queue_2 = queue; _i < queue_2.length; _i++) {
2387
2399
  var options = queue_2[_i];
2388
2400
  var format_1 = options.format;
@@ -2414,11 +2426,11 @@ var Builder = /** @class */ (function () {
2414
2426
  'rev',
2415
2427
  'static',
2416
2428
  ];
2417
- for (var _a = 0, properties_1 = properties; _a < properties_1.length; _a++) {
2418
- var key = properties_1[_a];
2429
+ for (var _c = 0, properties_1 = properties; _c < properties_1.length; _c++) {
2430
+ var key = properties_1[_c];
2419
2431
  var value = options[key];
2420
2432
  if (value !== undefined) {
2421
- if (apiEndpoint === 'query') {
2433
+ if (isApiCallForCodegen) {
2422
2434
  queryParams.options = queryParams.options || {};
2423
2435
  queryParams.options[options.key] = queryParams.options[options.key] || {};
2424
2436
  queryParams.options[options.key][key] = JSON.stringify(value);
@@ -2440,10 +2452,8 @@ var Builder = /** @class */ (function () {
2440
2452
  var params = omit(QueryString.parse(this.overrideParams), 'apiKey');
2441
2453
  assign(queryParams, params);
2442
2454
  }
2443
- var format = queryParams.format;
2444
- var isApiCallForCodegen = format === 'solid' || format === 'react';
2445
- var isApiCallForCodegenOrQuery = isApiCallForCodegen || apiEndpoint === 'query';
2446
- if (apiEndpoint === 'content') {
2455
+ queryParams.format;
2456
+ if (!isApiCallForCodegen) {
2447
2457
  queryParams.enrich = true;
2448
2458
  if (queue[0].query) {
2449
2459
  var flattened = this.flattenMongoQuery({ query: queue[0].query });
@@ -2462,9 +2472,6 @@ var Builder = /** @class */ (function () {
2462
2472
  if (isApiCallForCodegen) {
2463
2473
  url = "".concat(host, "/api/v1/codegen/").concat(this.apiKey, "/").concat(keyNames);
2464
2474
  }
2465
- else if (apiEndpoint === 'query') {
2466
- url = "".concat(host, "/api/v3/query/").concat(this.apiKey, "/").concat(keyNames);
2467
- }
2468
2475
  else {
2469
2476
  url = "".concat(host, "/api/v3/content/").concat(queue[0].model);
2470
2477
  }
@@ -2487,7 +2494,7 @@ var Builder = /** @class */ (function () {
2487
2494
  if (!observer) {
2488
2495
  return;
2489
2496
  }
2490
- var data = isApiCallForCodegenOrQuery ? result[keyName] : result.results;
2497
+ var data = isApiCallForCodegen ? result[keyName] : result.results;
2491
2498
  var sorted = data; // sortBy(data, item => item.priority);
2492
2499
  if (data) {
2493
2500
  var testModifiedResults = Builder.isServer
@@ -2670,6 +2677,7 @@ var Builder = /** @class */ (function () {
2670
2677
  (location.search.indexOf('builder.preview=') !== -1 ||
2671
2678
  location.search.indexOf('builder.frameEditing=') !== -1));
2672
2679
  Builder.isReact = false;
2680
+ Builder.sdkInfo = undefined;
2673
2681
  Builder.overrideUserAttributes = {};
2674
2682
  return Builder;
2675
2683
  }());