@builder.io/sdk 1.1.23 → 1.1.25

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
@@ -50,6 +50,7 @@ function nextTick(fn) {
50
50
  element.data = String((called = ++called));
51
51
  }
52
52
 
53
+ var PROPERTY_NAME_DENY_LIST = Object.freeze(['__proto__', 'prototype', 'constructor']);
53
54
  // TODO: unit tests
54
55
  var QueryString = /** @class */ (function () {
55
56
  function QueryString() {
@@ -91,13 +92,16 @@ var QueryString = /** @class */ (function () {
91
92
  return str;
92
93
  };
93
94
  QueryString.deepen = function (map) {
95
+ // FIXME; Should be type Tree = Record<string, string | Tree>
96
+ // requires a typescript upgrade.
94
97
  var output = {};
95
98
  for (var k in map) {
96
99
  var t = output;
97
100
  var parts = k.split('.');
98
101
  var key = parts.pop();
99
- while (parts.length) {
100
- var part = parts.shift();
102
+ for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
103
+ var part = parts_1[_i];
104
+ assertAllowedPropertyName(part);
101
105
  t = t[part] = t[part] || {};
102
106
  }
103
107
  t[key] = map[k];
@@ -119,9 +123,13 @@ var QueryString = /** @class */ (function () {
119
123
  return _res;
120
124
  };
121
125
  return QueryString;
122
- }());
126
+ }());
127
+ function assertAllowedPropertyName(name) {
128
+ if (PROPERTY_NAME_DENY_LIST.indexOf(name) >= 0)
129
+ throw new Error("Property name \"" + name + "\" is not allowed");
130
+ }
123
131
 
124
- var version = "1.1.23-14";
132
+ var version = "1.1.25-0";
125
133
 
126
134
  var Subscription = /** @class */ (function () {
127
135
  function Subscription(listeners, listener) {
@@ -978,13 +986,13 @@ function BuilderComponent(info) {
978
986
  return Builder.Component(info);
979
987
  }
980
988
  var Builder = /** @class */ (function () {
981
- function Builder(apiKey, request, response, forceNewInstance) {
989
+ function Builder(apiKey, request, response, forceNewInstance, authToken) {
982
990
  var _this = this;
983
991
  if (apiKey === void 0) { apiKey = null; }
984
992
  if (forceNewInstance === void 0) { forceNewInstance = false; }
993
+ if (authToken === void 0) { authToken = null; }
985
994
  this.request = request;
986
995
  this.response = response;
987
- this.authToken = '';
988
996
  this.eventsQueue = [];
989
997
  this.throttledClearEventsQueue = throttle(function () {
990
998
  _this.processEventsQueue();
@@ -1005,6 +1013,7 @@ var Builder = /** @class */ (function () {
1005
1013
  this.preview = false;
1006
1014
  this.canTrack$ = new BehaviorSubject(!this.browserTrackingDisabled);
1007
1015
  this.apiKey$ = new BehaviorSubject(null);
1016
+ this.authToken$ = new BehaviorSubject(null);
1008
1017
  this.userAttributesChanged = new BehaviorSubject(null);
1009
1018
  this.editingMode$ = new BehaviorSubject(isIframe);
1010
1019
  // TODO: decorator to do this stuff with the get/set (how do with typing too? compiler?)
@@ -1040,6 +1049,9 @@ var Builder = /** @class */ (function () {
1040
1049
  if (apiKey) {
1041
1050
  this.apiKey = apiKey;
1042
1051
  }
1052
+ if (authToken) {
1053
+ this.authToken = authToken;
1054
+ }
1043
1055
  if (isBrowser) {
1044
1056
  this.bindMessageListeners();
1045
1057
  // TODO: postmessage to parent the builder info for every package
@@ -1236,7 +1248,10 @@ var Builder = /** @class */ (function () {
1236
1248
  var _a;
1237
1249
  var spec = tslib.__assign(tslib.__assign({ class: component }, component.builderOptions), options);
1238
1250
  this.addComponent(spec);
1239
- if (isBrowser) {
1251
+ var editable = options.models && this.singletonInstance.editingModel
1252
+ ? isBrowser && options.models.includes(this.singletonInstance.editingModel)
1253
+ : isBrowser;
1254
+ if (editable) {
1240
1255
  var sendSpec = this.prepareComponentSpecToSend(spec);
1241
1256
  (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1242
1257
  type: 'builder.registerComponent',
@@ -1406,9 +1421,14 @@ var Builder = /** @class */ (function () {
1406
1421
  if (isIframe || !isBrowser || Builder.isPreviewing) {
1407
1422
  return;
1408
1423
  }
1424
+ var apiKey = this.apiKey;
1425
+ if (!apiKey) {
1426
+ console.error('Builder integration error: Looks like the Builder SDK has not been initialized properly (your API key has not been set). Make sure you are calling `builder.init("«YOUR-API-KEY»");` as early as possible in your application\'s code.');
1427
+ return;
1428
+ }
1409
1429
  var eventData = JSON.parse(JSON.stringify({
1410
1430
  type: eventName,
1411
- data: tslib.__assign(tslib.__assign({}, omit(properties, 'meta')), { metadata: tslib.__assign(tslib.__assign({ sdkVersion: Builder.VERSION, url: location.href }, properties.meta), properties.metadata), ownerId: this.apiKey, userAttributes: this.getUserAttributes(), sessionId: this.sessionId, visitorId: this.visitorId }),
1431
+ data: tslib.__assign(tslib.__assign({}, omit(properties, 'meta')), { metadata: tslib.__assign(tslib.__assign({ sdkVersion: Builder.VERSION, url: location.href }, properties.meta), properties.metadata), ownerId: apiKey, userAttributes: this.getUserAttributes(), sessionId: this.sessionId, visitorId: this.visitorId }),
1412
1432
  }));
1413
1433
  for (var _i = 0, _a = this.trackingHooks; _i < _a.length; _i++) {
1414
1434
  var hook = _a[_i];
@@ -1573,6 +1593,16 @@ var Builder = /** @class */ (function () {
1573
1593
  enumerable: true,
1574
1594
  configurable: true
1575
1595
  });
1596
+ Object.defineProperty(Builder.prototype, "authToken", {
1597
+ get: function () {
1598
+ return this.authToken$.value;
1599
+ },
1600
+ set: function (token) {
1601
+ this.authToken$.next(token);
1602
+ },
1603
+ enumerable: true,
1604
+ configurable: true
1605
+ });
1576
1606
  Builder.prototype.modifySearch = function (search) {
1577
1607
  return search.replace(/(^|&|\?)(builder_.*?)=/gi, function (_match, group1, group2) { return group1 + group2.replace(/_/g, '.') + '='; });
1578
1608
  };
@@ -1837,7 +1867,7 @@ var Builder = /** @class */ (function () {
1837
1867
  enumerable: true,
1838
1868
  configurable: true
1839
1869
  });
1840
- Builder.prototype.init = function (apiKey, canTrack, req, res) {
1870
+ Builder.prototype.init = function (apiKey, canTrack, req, res, authToken) {
1841
1871
  if (canTrack === void 0) { canTrack = this.defaultCanTrack; }
1842
1872
  if (req) {
1843
1873
  this.request = req;
@@ -1847,6 +1877,9 @@ var Builder = /** @class */ (function () {
1847
1877
  }
1848
1878
  this.canTrack = canTrack;
1849
1879
  this.apiKey = apiKey;
1880
+ if (authToken) {
1881
+ this.authToken = authToken;
1882
+ }
1850
1883
  return this;
1851
1884
  };
1852
1885
  Object.defineProperty(Builder.prototype, "previewingModel", {
@@ -1928,13 +1961,16 @@ var Builder = /** @class */ (function () {
1928
1961
  if (options === void 0) { options = {}; }
1929
1962
  var instance = this;
1930
1963
  if (!Builder.isBrowser) {
1931
- instance = new Builder(options.apiKey || this.apiKey, options.req, options.res);
1964
+ instance = new Builder(options.apiKey || this.apiKey, options.req, options.res, undefined, options.authToken || this.authToken);
1932
1965
  instance.setUserAttributes(this.getUserAttributes());
1933
1966
  }
1934
1967
  else {
1935
1968
  if (options.apiKey && !this.apiKey) {
1936
1969
  this.apiKey = options.apiKey;
1937
1970
  }
1971
+ if (options.authToken && !this.authToken) {
1972
+ this.authToken = options.authToken;
1973
+ }
1938
1974
  }
1939
1975
  return instance.queueGetContent(modelName, options).map(
1940
1976
  /* map( */ function (matches) {
@@ -2036,21 +2072,21 @@ var Builder = /** @class */ (function () {
2036
2072
  }
2037
2073
  return observable;
2038
2074
  };
2039
- Builder.prototype.requestUrl = function (url) {
2075
+ Builder.prototype.requestUrl = function (url, options) {
2040
2076
  if (Builder.isBrowser) {
2041
- // TODO: send auth header if builder.authToken
2042
- return fetch(url, this.authToken
2043
- ? {
2044
- headers: {
2045
- Authorization: "Bearer " + this.authToken,
2046
- },
2047
- }
2048
- : undefined).then(function (res) { return res.json(); });
2077
+ return fetch(url, options).then(function (res) { return res.json(); });
2049
2078
  }
2050
2079
  return new Promise(function (resolve, reject) {
2051
- var module = url.indexOf('http:') === 0 ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2080
+ var parsedUrl = parse(url);
2081
+ var module = parsedUrl.protocol === 'http:' ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2082
+ var requestOptions = {
2083
+ host: parsedUrl.hostname,
2084
+ port: parsedUrl.port,
2085
+ path: parsedUrl.pathname + parsedUrl.search,
2086
+ headers: tslib.__assign({}, options === null || options === void 0 ? void 0 : options.headers),
2087
+ };
2052
2088
  module
2053
- .get(url, function (resp) {
2089
+ .get(requestOptions, function (resp) {
2054
2090
  var data = '';
2055
2091
  // A chunk of data has been recieved.
2056
2092
  resp.on('data', function (chunk) {
@@ -2228,7 +2264,11 @@ var Builder = /** @class */ (function () {
2228
2264
  }
2229
2265
  var queryStr = QueryString.stringifyDeep(queryParams);
2230
2266
  var format = queryParams.format;
2231
- var promise = this.requestUrl(host + "/api/v1/" + (format === 'solid' || format === 'react' ? 'codegen' : 'query') + "/" + this.apiKey + "/" + keyNames + (queryParams && hasParams ? "?" + queryStr : '')).then(function (result) {
2267
+ var requestOptions = { headers: {} };
2268
+ if (this.authToken) {
2269
+ requestOptions.headers = tslib.__assign(tslib.__assign({}, requestOptions.headers), { Authorization: "Bearer " + this.authToken });
2270
+ }
2271
+ var promise = this.requestUrl(host + "/api/v1/" + (format === 'solid' || format === 'react' ? 'codegen' : 'query') + "/" + this.apiKey + "/" + keyNames + (queryParams && hasParams ? "?" + queryStr : ''), requestOptions).then(function (result) {
2232
2272
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
2233
2273
  var options = queue_3[_i];
2234
2274
  var keyName = options.key;
@@ -2366,7 +2406,9 @@ var Builder = /** @class */ (function () {
2366
2406
  .getContent(modelName, tslib.__assign(tslib.__assign({ limit: 30 }, options), { key: options.key ||
2367
2407
  // Make the key include all options so we don't reuse cache for the same conent fetched
2368
2408
  // with different options
2369
- Builder.isBrowser ? modelName + ":" + hash(omit(options, 'initialContent', 'req', 'res')) : undefined }))
2409
+ Builder.isBrowser
2410
+ ? modelName + ":" + hash(omit(options, 'initialContent', 'req', 'res'))
2411
+ : undefined }))
2370
2412
  .promise();
2371
2413
  };
2372
2414
  Builder.VERSION = version;