@builder.io/sdk 1.1.23 → 1.1.24

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
@@ -44,6 +44,7 @@ function nextTick(fn) {
44
44
  element.data = String((called = ++called));
45
45
  }
46
46
 
47
+ var PROPERTY_NAME_DENY_LIST = Object.freeze(['__proto__', 'prototype']);
47
48
  // TODO: unit tests
48
49
  var QueryString = /** @class */ (function () {
49
50
  function QueryString() {
@@ -85,14 +86,17 @@ var QueryString = /** @class */ (function () {
85
86
  return str;
86
87
  };
87
88
  QueryString.deepen = function (map) {
88
- var output = {};
89
+ // FIXME; Should be type Tree = Record<string, string | Tree>
90
+ // requires a typescript upgrade.
91
+ var output = Object.create(null);
89
92
  for (var k in map) {
90
93
  var t = output;
91
94
  var parts = k.split('.');
92
95
  var key = parts.pop();
93
- while (parts.length) {
94
- var part = parts.shift();
95
- t = t[part] = t[part] || {};
96
+ for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
97
+ var part = parts_1[_i];
98
+ assertAllowedPropertyName(part);
99
+ t = t[part] = t[part] || Object.create(null);
96
100
  }
97
101
  t[key] = map[k];
98
102
  }
@@ -113,9 +117,13 @@ var QueryString = /** @class */ (function () {
113
117
  return _res;
114
118
  };
115
119
  return QueryString;
116
- }());
120
+ }());
121
+ function assertAllowedPropertyName(name) {
122
+ if (PROPERTY_NAME_DENY_LIST.indexOf(name) >= 0)
123
+ throw new Error("Property name \"" + name + "\" is not allowed");
124
+ }
117
125
 
118
- var version = "1.1.23-14";
126
+ var version = "1.1.23";
119
127
 
120
128
  var Subscription = /** @class */ (function () {
121
129
  function Subscription(listeners, listener) {
@@ -972,13 +980,13 @@ function BuilderComponent(info) {
972
980
  return Builder.Component(info);
973
981
  }
974
982
  var Builder = /** @class */ (function () {
975
- function Builder(apiKey, request, response, forceNewInstance) {
983
+ function Builder(apiKey, request, response, forceNewInstance, authToken) {
976
984
  var _this = this;
977
985
  if (apiKey === void 0) { apiKey = null; }
978
986
  if (forceNewInstance === void 0) { forceNewInstance = false; }
987
+ if (authToken === void 0) { authToken = null; }
979
988
  this.request = request;
980
989
  this.response = response;
981
- this.authToken = '';
982
990
  this.eventsQueue = [];
983
991
  this.throttledClearEventsQueue = throttle(function () {
984
992
  _this.processEventsQueue();
@@ -999,6 +1007,7 @@ var Builder = /** @class */ (function () {
999
1007
  this.preview = false;
1000
1008
  this.canTrack$ = new BehaviorSubject(!this.browserTrackingDisabled);
1001
1009
  this.apiKey$ = new BehaviorSubject(null);
1010
+ this.authToken$ = new BehaviorSubject(null);
1002
1011
  this.userAttributesChanged = new BehaviorSubject(null);
1003
1012
  this.editingMode$ = new BehaviorSubject(isIframe);
1004
1013
  // TODO: decorator to do this stuff with the get/set (how do with typing too? compiler?)
@@ -1034,6 +1043,9 @@ var Builder = /** @class */ (function () {
1034
1043
  if (apiKey) {
1035
1044
  this.apiKey = apiKey;
1036
1045
  }
1046
+ if (authToken) {
1047
+ this.authToken = authToken;
1048
+ }
1037
1049
  if (isBrowser) {
1038
1050
  this.bindMessageListeners();
1039
1051
  // TODO: postmessage to parent the builder info for every package
@@ -1230,7 +1242,10 @@ var Builder = /** @class */ (function () {
1230
1242
  var _a;
1231
1243
  var spec = __assign(__assign({ class: component }, component.builderOptions), options);
1232
1244
  this.addComponent(spec);
1233
- if (isBrowser) {
1245
+ var editable = options.models && this.singletonInstance.editingModel
1246
+ ? isBrowser && options.models.includes(this.singletonInstance.editingModel)
1247
+ : isBrowser;
1248
+ if (editable) {
1234
1249
  var sendSpec = this.prepareComponentSpecToSend(spec);
1235
1250
  (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1236
1251
  type: 'builder.registerComponent',
@@ -1400,9 +1415,14 @@ var Builder = /** @class */ (function () {
1400
1415
  if (isIframe || !isBrowser || Builder.isPreviewing) {
1401
1416
  return;
1402
1417
  }
1418
+ var apiKey = this.apiKey;
1419
+ if (!apiKey) {
1420
+ 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.');
1421
+ return;
1422
+ }
1403
1423
  var eventData = JSON.parse(JSON.stringify({
1404
1424
  type: eventName,
1405
- data: __assign(__assign({}, omit(properties, 'meta')), { metadata: __assign(__assign({ sdkVersion: Builder.VERSION, url: location.href }, properties.meta), properties.metadata), ownerId: this.apiKey, userAttributes: this.getUserAttributes(), sessionId: this.sessionId, visitorId: this.visitorId }),
1425
+ data: __assign(__assign({}, omit(properties, 'meta')), { metadata: __assign(__assign({ sdkVersion: Builder.VERSION, url: location.href }, properties.meta), properties.metadata), ownerId: apiKey, userAttributes: this.getUserAttributes(), sessionId: this.sessionId, visitorId: this.visitorId }),
1406
1426
  }));
1407
1427
  for (var _i = 0, _a = this.trackingHooks; _i < _a.length; _i++) {
1408
1428
  var hook = _a[_i];
@@ -1567,6 +1587,16 @@ var Builder = /** @class */ (function () {
1567
1587
  enumerable: true,
1568
1588
  configurable: true
1569
1589
  });
1590
+ Object.defineProperty(Builder.prototype, "authToken", {
1591
+ get: function () {
1592
+ return this.authToken$.value;
1593
+ },
1594
+ set: function (token) {
1595
+ this.authToken$.next(token);
1596
+ },
1597
+ enumerable: true,
1598
+ configurable: true
1599
+ });
1570
1600
  Builder.prototype.modifySearch = function (search) {
1571
1601
  return search.replace(/(^|&|\?)(builder_.*?)=/gi, function (_match, group1, group2) { return group1 + group2.replace(/_/g, '.') + '='; });
1572
1602
  };
@@ -1831,7 +1861,7 @@ var Builder = /** @class */ (function () {
1831
1861
  enumerable: true,
1832
1862
  configurable: true
1833
1863
  });
1834
- Builder.prototype.init = function (apiKey, canTrack, req, res) {
1864
+ Builder.prototype.init = function (apiKey, canTrack, req, res, authToken) {
1835
1865
  if (canTrack === void 0) { canTrack = this.defaultCanTrack; }
1836
1866
  if (req) {
1837
1867
  this.request = req;
@@ -1841,6 +1871,9 @@ var Builder = /** @class */ (function () {
1841
1871
  }
1842
1872
  this.canTrack = canTrack;
1843
1873
  this.apiKey = apiKey;
1874
+ if (authToken) {
1875
+ this.authToken = authToken;
1876
+ }
1844
1877
  return this;
1845
1878
  };
1846
1879
  Object.defineProperty(Builder.prototype, "previewingModel", {
@@ -1922,13 +1955,16 @@ var Builder = /** @class */ (function () {
1922
1955
  if (options === void 0) { options = {}; }
1923
1956
  var instance = this;
1924
1957
  if (!Builder.isBrowser) {
1925
- instance = new Builder(options.apiKey || this.apiKey, options.req, options.res);
1958
+ instance = new Builder(options.apiKey || this.apiKey, options.req, options.res, undefined, options.authToken || this.authToken);
1926
1959
  instance.setUserAttributes(this.getUserAttributes());
1927
1960
  }
1928
1961
  else {
1929
1962
  if (options.apiKey && !this.apiKey) {
1930
1963
  this.apiKey = options.apiKey;
1931
1964
  }
1965
+ if (options.authToken && !this.authToken) {
1966
+ this.authToken = options.authToken;
1967
+ }
1932
1968
  }
1933
1969
  return instance.queueGetContent(modelName, options).map(
1934
1970
  /* map( */ function (matches) {
@@ -2030,21 +2066,21 @@ var Builder = /** @class */ (function () {
2030
2066
  }
2031
2067
  return observable;
2032
2068
  };
2033
- Builder.prototype.requestUrl = function (url) {
2069
+ Builder.prototype.requestUrl = function (url, options) {
2034
2070
  if (Builder.isBrowser) {
2035
- // TODO: send auth header if builder.authToken
2036
- return fetch(url, this.authToken
2037
- ? {
2038
- headers: {
2039
- Authorization: "Bearer " + this.authToken,
2040
- },
2041
- }
2042
- : undefined).then(function (res) { return res.json(); });
2071
+ return fetch(url, options).then(function (res) { return res.json(); });
2043
2072
  }
2044
2073
  return new Promise(function (resolve, reject) {
2045
- var module = url.indexOf('http:') === 0 ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2074
+ var parsedUrl = parse(url);
2075
+ var module = parsedUrl.protocol === 'http:' ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2076
+ var requestOptions = {
2077
+ host: parsedUrl.hostname,
2078
+ port: parsedUrl.port,
2079
+ path: parsedUrl.pathname + parsedUrl.search,
2080
+ headers: __assign({}, options === null || options === void 0 ? void 0 : options.headers),
2081
+ };
2046
2082
  module
2047
- .get(url, function (resp) {
2083
+ .get(requestOptions, function (resp) {
2048
2084
  var data = '';
2049
2085
  // A chunk of data has been recieved.
2050
2086
  resp.on('data', function (chunk) {
@@ -2222,7 +2258,11 @@ var Builder = /** @class */ (function () {
2222
2258
  }
2223
2259
  var queryStr = QueryString.stringifyDeep(queryParams);
2224
2260
  var format = queryParams.format;
2225
- var promise = this.requestUrl(host + "/api/v1/" + (format === 'solid' || format === 'react' ? 'codegen' : 'query') + "/" + this.apiKey + "/" + keyNames + (queryParams && hasParams ? "?" + queryStr : '')).then(function (result) {
2261
+ var requestOptions = { headers: {} };
2262
+ if (this.authToken) {
2263
+ requestOptions.headers = __assign(__assign({}, requestOptions.headers), { Authorization: "Bearer " + this.authToken });
2264
+ }
2265
+ var promise = this.requestUrl(host + "/api/v1/" + (format === 'solid' || format === 'react' ? 'codegen' : 'query') + "/" + this.apiKey + "/" + keyNames + (queryParams && hasParams ? "?" + queryStr : ''), requestOptions).then(function (result) {
2226
2266
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
2227
2267
  var options = queue_3[_i];
2228
2268
  var keyName = options.key;
@@ -2360,7 +2400,9 @@ var Builder = /** @class */ (function () {
2360
2400
  .getContent(modelName, __assign(__assign({ limit: 30 }, options), { key: options.key ||
2361
2401
  // Make the key include all options so we don't reuse cache for the same conent fetched
2362
2402
  // with different options
2363
- Builder.isBrowser ? modelName + ":" + hash(omit(options, 'initialContent', 'req', 'res')) : undefined }))
2403
+ Builder.isBrowser
2404
+ ? modelName + ":" + hash(omit(options, 'initialContent', 'req', 'res'))
2405
+ : undefined }))
2364
2406
  .promise();
2365
2407
  };
2366
2408
  Builder.VERSION = version;