@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.umd.js CHANGED
@@ -73,6 +73,7 @@
73
73
  element.data = String((called = ++called));
74
74
  }
75
75
 
76
+ var PROPERTY_NAME_DENY_LIST = Object.freeze(['__proto__', 'prototype']);
76
77
  // TODO: unit tests
77
78
  var QueryString = /** @class */ (function () {
78
79
  function QueryString() {
@@ -114,14 +115,17 @@
114
115
  return str;
115
116
  };
116
117
  QueryString.deepen = function (map) {
117
- var output = {};
118
+ // FIXME; Should be type Tree = Record<string, string | Tree>
119
+ // requires a typescript upgrade.
120
+ var output = Object.create(null);
118
121
  for (var k in map) {
119
122
  var t = output;
120
123
  var parts = k.split('.');
121
124
  var key = parts.pop();
122
- while (parts.length) {
123
- var part = parts.shift();
124
- t = t[part] = t[part] || {};
125
+ for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
126
+ var part = parts_1[_i];
127
+ assertAllowedPropertyName(part);
128
+ t = t[part] = t[part] || Object.create(null);
125
129
  }
126
130
  t[key] = map[k];
127
131
  }
@@ -142,9 +146,13 @@
142
146
  return _res;
143
147
  };
144
148
  return QueryString;
145
- }());
149
+ }());
150
+ function assertAllowedPropertyName(name) {
151
+ if (PROPERTY_NAME_DENY_LIST.indexOf(name) >= 0)
152
+ throw new Error("Property name \"" + name + "\" is not allowed");
153
+ }
146
154
 
147
- var version = "1.1.23-14";
155
+ var version = "1.1.23";
148
156
 
149
157
  var Subscription = /** @class */ (function () {
150
158
  function Subscription(listeners, listener) {
@@ -1069,13 +1077,13 @@
1069
1077
  return Builder.Component(info);
1070
1078
  }
1071
1079
  var Builder = /** @class */ (function () {
1072
- function Builder(apiKey, request, response, forceNewInstance) {
1080
+ function Builder(apiKey, request, response, forceNewInstance, authToken) {
1073
1081
  var _this = this;
1074
1082
  if (apiKey === void 0) { apiKey = null; }
1075
1083
  if (forceNewInstance === void 0) { forceNewInstance = false; }
1084
+ if (authToken === void 0) { authToken = null; }
1076
1085
  this.request = request;
1077
1086
  this.response = response;
1078
- this.authToken = '';
1079
1087
  this.eventsQueue = [];
1080
1088
  this.throttledClearEventsQueue = throttle(function () {
1081
1089
  _this.processEventsQueue();
@@ -1096,6 +1104,7 @@
1096
1104
  this.preview = false;
1097
1105
  this.canTrack$ = new BehaviorSubject(!this.browserTrackingDisabled);
1098
1106
  this.apiKey$ = new BehaviorSubject(null);
1107
+ this.authToken$ = new BehaviorSubject(null);
1099
1108
  this.userAttributesChanged = new BehaviorSubject(null);
1100
1109
  this.editingMode$ = new BehaviorSubject(isIframe);
1101
1110
  // TODO: decorator to do this stuff with the get/set (how do with typing too? compiler?)
@@ -1131,6 +1140,9 @@
1131
1140
  if (apiKey) {
1132
1141
  this.apiKey = apiKey;
1133
1142
  }
1143
+ if (authToken) {
1144
+ this.authToken = authToken;
1145
+ }
1134
1146
  if (isBrowser) {
1135
1147
  this.bindMessageListeners();
1136
1148
  // TODO: postmessage to parent the builder info for every package
@@ -1327,7 +1339,10 @@
1327
1339
  var _a;
1328
1340
  var spec = __assign(__assign({ class: component }, component.builderOptions), options);
1329
1341
  this.addComponent(spec);
1330
- if (isBrowser) {
1342
+ var editable = options.models && this.singletonInstance.editingModel
1343
+ ? isBrowser && options.models.includes(this.singletonInstance.editingModel)
1344
+ : isBrowser;
1345
+ if (editable) {
1331
1346
  var sendSpec = this.prepareComponentSpecToSend(spec);
1332
1347
  (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1333
1348
  type: 'builder.registerComponent',
@@ -1497,9 +1512,14 @@
1497
1512
  if (isIframe || !isBrowser || Builder.isPreviewing) {
1498
1513
  return;
1499
1514
  }
1515
+ var apiKey = this.apiKey;
1516
+ if (!apiKey) {
1517
+ 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.');
1518
+ return;
1519
+ }
1500
1520
  var eventData = JSON.parse(JSON.stringify({
1501
1521
  type: eventName,
1502
- 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 }),
1522
+ 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 }),
1503
1523
  }));
1504
1524
  for (var _i = 0, _a = this.trackingHooks; _i < _a.length; _i++) {
1505
1525
  var hook = _a[_i];
@@ -1664,6 +1684,16 @@
1664
1684
  enumerable: true,
1665
1685
  configurable: true
1666
1686
  });
1687
+ Object.defineProperty(Builder.prototype, "authToken", {
1688
+ get: function () {
1689
+ return this.authToken$.value;
1690
+ },
1691
+ set: function (token) {
1692
+ this.authToken$.next(token);
1693
+ },
1694
+ enumerable: true,
1695
+ configurable: true
1696
+ });
1667
1697
  Builder.prototype.modifySearch = function (search) {
1668
1698
  return search.replace(/(^|&|\?)(builder_.*?)=/gi, function (_match, group1, group2) { return group1 + group2.replace(/_/g, '.') + '='; });
1669
1699
  };
@@ -1928,7 +1958,7 @@
1928
1958
  enumerable: true,
1929
1959
  configurable: true
1930
1960
  });
1931
- Builder.prototype.init = function (apiKey, canTrack, req, res) {
1961
+ Builder.prototype.init = function (apiKey, canTrack, req, res, authToken) {
1932
1962
  if (canTrack === void 0) { canTrack = this.defaultCanTrack; }
1933
1963
  if (req) {
1934
1964
  this.request = req;
@@ -1938,6 +1968,9 @@
1938
1968
  }
1939
1969
  this.canTrack = canTrack;
1940
1970
  this.apiKey = apiKey;
1971
+ if (authToken) {
1972
+ this.authToken = authToken;
1973
+ }
1941
1974
  return this;
1942
1975
  };
1943
1976
  Object.defineProperty(Builder.prototype, "previewingModel", {
@@ -2019,13 +2052,16 @@
2019
2052
  if (options === void 0) { options = {}; }
2020
2053
  var instance = this;
2021
2054
  if (!Builder.isBrowser) {
2022
- instance = new Builder(options.apiKey || this.apiKey, options.req, options.res);
2055
+ instance = new Builder(options.apiKey || this.apiKey, options.req, options.res, undefined, options.authToken || this.authToken);
2023
2056
  instance.setUserAttributes(this.getUserAttributes());
2024
2057
  }
2025
2058
  else {
2026
2059
  if (options.apiKey && !this.apiKey) {
2027
2060
  this.apiKey = options.apiKey;
2028
2061
  }
2062
+ if (options.authToken && !this.authToken) {
2063
+ this.authToken = options.authToken;
2064
+ }
2029
2065
  }
2030
2066
  return instance.queueGetContent(modelName, options).map(
2031
2067
  /* map( */ function (matches) {
@@ -2127,21 +2163,21 @@
2127
2163
  }
2128
2164
  return observable;
2129
2165
  };
2130
- Builder.prototype.requestUrl = function (url) {
2166
+ Builder.prototype.requestUrl = function (url, options) {
2131
2167
  if (Builder.isBrowser) {
2132
- // TODO: send auth header if builder.authToken
2133
- return fetch(url, this.authToken
2134
- ? {
2135
- headers: {
2136
- Authorization: "Bearer " + this.authToken,
2137
- },
2138
- }
2139
- : undefined).then(function (res) { return res.json(); });
2168
+ return fetch(url, options).then(function (res) { return res.json(); });
2140
2169
  }
2141
2170
  return new Promise(function (resolve, reject) {
2142
- var module = url.indexOf('http:') === 0 ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2171
+ var parsedUrl = parse(url);
2172
+ var module = parsedUrl.protocol === 'http:' ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2173
+ var requestOptions = {
2174
+ host: parsedUrl.hostname,
2175
+ port: parsedUrl.port,
2176
+ path: parsedUrl.pathname + parsedUrl.search,
2177
+ headers: __assign({}, options === null || options === void 0 ? void 0 : options.headers),
2178
+ };
2143
2179
  module
2144
- .get(url, function (resp) {
2180
+ .get(requestOptions, function (resp) {
2145
2181
  var data = '';
2146
2182
  // A chunk of data has been recieved.
2147
2183
  resp.on('data', function (chunk) {
@@ -2319,7 +2355,11 @@
2319
2355
  }
2320
2356
  var queryStr = QueryString.stringifyDeep(queryParams);
2321
2357
  var format = queryParams.format;
2322
- var promise = this.requestUrl(host + "/api/v1/" + (format === 'solid' || format === 'react' ? 'codegen' : 'query') + "/" + this.apiKey + "/" + keyNames + (queryParams && hasParams ? "?" + queryStr : '')).then(function (result) {
2358
+ var requestOptions = { headers: {} };
2359
+ if (this.authToken) {
2360
+ requestOptions.headers = __assign(__assign({}, requestOptions.headers), { Authorization: "Bearer " + this.authToken });
2361
+ }
2362
+ var promise = this.requestUrl(host + "/api/v1/" + (format === 'solid' || format === 'react' ? 'codegen' : 'query') + "/" + this.apiKey + "/" + keyNames + (queryParams && hasParams ? "?" + queryStr : ''), requestOptions).then(function (result) {
2323
2363
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
2324
2364
  var options = queue_3[_i];
2325
2365
  var keyName = options.key;
@@ -2457,7 +2497,9 @@
2457
2497
  .getContent(modelName, __assign(__assign({ limit: 30 }, options), { key: options.key ||
2458
2498
  // Make the key include all options so we don't reuse cache for the same conent fetched
2459
2499
  // with different options
2460
- Builder.isBrowser ? modelName + ":" + hashSum(omit(options, 'initialContent', 'req', 'res')) : undefined }))
2500
+ Builder.isBrowser
2501
+ ? modelName + ":" + hashSum(omit(options, 'initialContent', 'req', 'res'))
2502
+ : undefined }))
2461
2503
  .promise();
2462
2504
  };
2463
2505
  Builder.VERSION = version;