@builder.io/sdk 3.0.6 → 3.0.8

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 = '3.0.8';
954
954
 
955
955
  function datePlusMinutes(minutes) {
956
956
  if (minutes === void 0) { minutes = 30; }
@@ -1145,8 +1145,8 @@ var Builder = /** @class */ (function () {
1145
1145
  this.authToken = authToken;
1146
1146
  }
1147
1147
  if (isBrowser) {
1148
- this.bindMessageListeners();
1149
1148
  if (Builder.isEditing) {
1149
+ this.bindMessageListeners();
1150
1150
  parent.postMessage({
1151
1151
  type: 'builder.animatorOptions',
1152
1152
  data: {
@@ -1277,11 +1277,16 @@ var Builder = /** @class */ (function () {
1277
1277
  this.serverContext = context;
1278
1278
  };
1279
1279
  Builder.isTrustedHost = function (hostname) {
1280
- return (this.trustedHosts.findIndex(function (trustedHost) {
1280
+ var isTrusted = this.trustedHosts.findIndex(function (trustedHost) {
1281
1281
  return trustedHost.startsWith('*.')
1282
1282
  ? hostname.endsWith(trustedHost.slice(1))
1283
1283
  : trustedHost === hostname;
1284
- }) > -1);
1284
+ }) > -1;
1285
+ return isTrusted;
1286
+ };
1287
+ Builder.isTrustedHostForEvent = function (event) {
1288
+ var url = parse(event.origin);
1289
+ return url.hostname && Builder.isTrustedHost(url.hostname);
1285
1290
  };
1286
1291
  Builder.runAction = function (action) {
1287
1292
  // TODO
@@ -1440,9 +1445,7 @@ var Builder = /** @class */ (function () {
1440
1445
  getFetch()("".concat(host, "/api/v1/track"), {
1441
1446
  method: 'POST',
1442
1447
  body: JSON.stringify({ events: events }),
1443
- headers: {
1444
- 'content-type': 'application/json',
1445
- },
1448
+ headers: __assign({ 'content-type': 'application/json' }, this.getSdkHeaders()),
1446
1449
  mode: 'cors',
1447
1450
  }).catch(function () {
1448
1451
  // Not the end of the world
@@ -1831,184 +1834,179 @@ var Builder = /** @class */ (function () {
1831
1834
  };
1832
1835
  Builder.prototype.bindMessageListeners = function () {
1833
1836
  var _this = this;
1834
- if (isBrowser) {
1835
- addEventListener('message', function (event) {
1836
- var _a, _b, _c, _d, _e;
1837
- var url = parse(event.origin);
1838
- var isRestricted = ['builder.register', 'builder.registerComponent'].indexOf((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === -1;
1839
- var isTrusted = url.hostname && Builder.isTrustedHost(url.hostname);
1840
- if (isRestricted && !isTrusted) {
1841
- return;
1842
- }
1843
- var data = event.data;
1844
- if (data) {
1845
- switch (data.type) {
1846
- case 'builder.ping': {
1847
- (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1848
- type: 'builder.pong',
1849
- data: {},
1850
- }, '*');
1837
+ addEventListener('message', function (event) {
1838
+ var _a, _b, _c, _d;
1839
+ var isTrusted = Builder.isTrustedHostForEvent(event);
1840
+ if (!isTrusted)
1841
+ return;
1842
+ var data = event.data;
1843
+ if (data) {
1844
+ switch (data.type) {
1845
+ case 'builder.ping': {
1846
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1847
+ type: 'builder.pong',
1848
+ data: {},
1849
+ }, '*');
1850
+ break;
1851
+ }
1852
+ case 'builder.register': {
1853
+ // TODO: possibly do this for all...
1854
+ if (event.source === window) {
1851
1855
  break;
1852
1856
  }
1853
- case 'builder.register': {
1854
- // TODO: possibly do this for all...
1855
- if (event.source === window) {
1856
- break;
1857
- }
1858
- var options = data.data;
1859
- if (!options) {
1860
- break;
1861
- }
1862
- var type = options.type, info = options.info;
1863
- // TODO: all must have name and can't conflict?
1864
- var typeList = Builder.registry[type];
1865
- if (!typeList) {
1866
- typeList = Builder.registry[type] = [];
1867
- }
1868
- typeList.push(info);
1869
- Builder.registryChange.next(Builder.registry);
1857
+ var options = data.data;
1858
+ if (!options) {
1870
1859
  break;
1871
1860
  }
1872
- case 'builder.settingsChange': {
1873
- // TODO: possibly do this for all...
1874
- if (event.source === window) {
1875
- break;
1876
- }
1877
- var settings = data.data;
1878
- if (!settings) {
1879
- break;
1880
- }
1881
- Object.assign(Builder.settings, settings);
1882
- Builder.settingsChange.next(Builder.settings);
1861
+ var type = options.type, info = options.info;
1862
+ // TODO: all must have name and can't conflict?
1863
+ var typeList = Builder.registry[type];
1864
+ if (!typeList) {
1865
+ typeList = Builder.registry[type] = [];
1866
+ }
1867
+ typeList.push(info);
1868
+ Builder.registryChange.next(Builder.registry);
1869
+ break;
1870
+ }
1871
+ case 'builder.settingsChange': {
1872
+ // TODO: possibly do this for all...
1873
+ if (event.source === window) {
1883
1874
  break;
1884
1875
  }
1885
- case 'builder.registerEditor': {
1886
- // TODO: possibly do this for all...
1887
- if (event.source === window) {
1888
- break;
1889
- }
1890
- var info_1 = data.data;
1891
- if (!info_1) {
1892
- break;
1893
- }
1894
- var hasComponent_1 = !!info_1.component;
1895
- Builder.editors.every(function (thisInfo, index) {
1896
- if (info_1.name === thisInfo.name) {
1897
- if (thisInfo.component && !hasComponent_1) {
1898
- return false;
1899
- }
1900
- else {
1901
- Builder.editors[index] = thisInfo;
1902
- }
1903
- return false;
1904
- }
1905
- return true;
1906
- });
1876
+ var settings = data.data;
1877
+ if (!settings) {
1907
1878
  break;
1908
1879
  }
1909
- case 'builder.triggerAnimation': {
1910
- Builder.animator.triggerAnimation(data.data);
1880
+ Object.assign(Builder.settings, settings);
1881
+ Builder.settingsChange.next(Builder.settings);
1882
+ break;
1883
+ }
1884
+ case 'builder.registerEditor': {
1885
+ // TODO: possibly do this for all...
1886
+ if (event.source === window) {
1911
1887
  break;
1912
1888
  }
1913
- case 'builder.contentUpdate':
1914
- var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
1915
- var contentData = data.data.data; // hmmm...
1916
- var observer = _this.observersByKey[key];
1917
- if (observer && !_this.noEditorUpdates[key]) {
1918
- observer.next([contentData]);
1919
- }
1889
+ var info_1 = data.data;
1890
+ if (!info_1) {
1920
1891
  break;
1921
- case 'builder.getComponents':
1892
+ }
1893
+ var hasComponent_1 = !!info_1.component;
1894
+ Builder.editors.every(function (thisInfo, index) {
1895
+ if (info_1.name === thisInfo.name) {
1896
+ if (thisInfo.component && !hasComponent_1) {
1897
+ return false;
1898
+ }
1899
+ else {
1900
+ Builder.editors[index] = thisInfo;
1901
+ }
1902
+ return false;
1903
+ }
1904
+ return true;
1905
+ });
1906
+ break;
1907
+ }
1908
+ case 'builder.triggerAnimation': {
1909
+ Builder.animator.triggerAnimation(data.data);
1910
+ break;
1911
+ }
1912
+ case 'builder.contentUpdate':
1913
+ var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
1914
+ var contentData = data.data.data; // hmmm...
1915
+ var observer = _this.observersByKey[key];
1916
+ if (observer && !_this.noEditorUpdates[key]) {
1917
+ observer.next([contentData]);
1918
+ }
1919
+ break;
1920
+ case 'builder.getComponents':
1921
+ (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1922
+ type: 'builder.components',
1923
+ data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1924
+ }, '*');
1925
+ break;
1926
+ case 'builder.editingModel':
1927
+ _this.editingModel = data.data.model;
1928
+ break;
1929
+ case 'builder.registerComponent':
1930
+ var componentData = data.data;
1931
+ Builder.addComponent(componentData);
1932
+ break;
1933
+ case 'builder.blockContentLoading':
1934
+ if (typeof data.data.model === 'string') {
1935
+ _this.blockContentLoading = data.data.model;
1936
+ }
1937
+ break;
1938
+ case 'builder.editingMode':
1939
+ var editingMode = data.data;
1940
+ if (editingMode) {
1941
+ _this.editingMode = true;
1942
+ document.body.classList.add('builder-editing');
1943
+ }
1944
+ else {
1945
+ _this.editingMode = false;
1946
+ document.body.classList.remove('builder-editing');
1947
+ }
1948
+ break;
1949
+ case 'builder.editingPageMode':
1950
+ var editingPageMode = data.data;
1951
+ Builder.editingPage = editingPageMode;
1952
+ break;
1953
+ case 'builder.overrideUserAttributes':
1954
+ var userAttributes = data.data;
1955
+ assign(Builder.overrideUserAttributes, userAttributes);
1956
+ _this.flushGetContentQueue(true);
1957
+ // TODO: refetch too
1958
+ break;
1959
+ case 'builder.overrideTestGroup':
1960
+ var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
1961
+ if (variationId && contentId) {
1962
+ _this.setTestCookie(contentId, variationId);
1963
+ _this.flushGetContentQueue(true);
1964
+ }
1965
+ break;
1966
+ case 'builder.evaluate': {
1967
+ var text = data.data.text;
1968
+ var args = data.data.arguments || [];
1969
+ var id_1 = data.data.id;
1970
+ // tslint:disable-next-line:no-function-constructor-with-string-args
1971
+ var fn = new Function(text);
1972
+ var result = void 0;
1973
+ var error = null;
1974
+ try {
1975
+ result = fn.apply(_this, args);
1976
+ }
1977
+ catch (err) {
1978
+ error = toError(err);
1979
+ }
1980
+ if (error) {
1922
1981
  (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
1923
- type: 'builder.components',
1924
- data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1982
+ type: 'builder.evaluateError',
1983
+ data: { id: id_1, error: error.message },
1925
1984
  }, '*');
1926
- break;
1927
- case 'builder.editingModel':
1928
- _this.editingModel = data.data.model;
1929
- break;
1930
- case 'builder.registerComponent':
1931
- var componentData = data.data;
1932
- Builder.addComponent(componentData);
1933
- break;
1934
- case 'builder.blockContentLoading':
1935
- if (typeof data.data.model === 'string') {
1936
- _this.blockContentLoading = data.data.model;
1937
- }
1938
- break;
1939
- case 'builder.editingMode':
1940
- var editingMode = data.data;
1941
- if (editingMode) {
1942
- _this.editingMode = true;
1943
- document.body.classList.add('builder-editing');
1985
+ }
1986
+ else {
1987
+ if (result && typeof result.then === 'function') {
1988
+ result
1989
+ .then(function (finalResult) {
1990
+ var _a;
1991
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1992
+ type: 'builder.evaluateResult',
1993
+ data: { id: id_1, result: finalResult },
1994
+ }, '*');
1995
+ })
1996
+ .catch(console.error);
1944
1997
  }
1945
1998
  else {
1946
- _this.editingMode = false;
1947
- document.body.classList.remove('builder-editing');
1948
- }
1949
- break;
1950
- case 'builder.editingPageMode':
1951
- var editingPageMode = data.data;
1952
- Builder.editingPage = editingPageMode;
1953
- break;
1954
- case 'builder.overrideUserAttributes':
1955
- var userAttributes = data.data;
1956
- assign(Builder.overrideUserAttributes, userAttributes);
1957
- _this.flushGetContentQueue(true);
1958
- // TODO: refetch too
1959
- break;
1960
- case 'builder.overrideTestGroup':
1961
- var _f = data.data, variationId = _f.variationId, contentId = _f.contentId;
1962
- if (variationId && contentId) {
1963
- _this.setTestCookie(contentId, variationId);
1964
- _this.flushGetContentQueue(true);
1965
- }
1966
- break;
1967
- case 'builder.evaluate': {
1968
- var text = data.data.text;
1969
- var args = data.data.arguments || [];
1970
- var id_1 = data.data.id;
1971
- // tslint:disable-next-line:no-function-constructor-with-string-args
1972
- var fn = new Function(text);
1973
- var result = void 0;
1974
- var error = null;
1975
- try {
1976
- result = fn.apply(_this, args);
1977
- }
1978
- catch (err) {
1979
- error = toError(err);
1980
- }
1981
- if (error) {
1982
1999
  (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
1983
- type: 'builder.evaluateError',
1984
- data: { id: id_1, error: error.message },
2000
+ type: 'builder.evaluateResult',
2001
+ data: { result: result, id: id_1 },
1985
2002
  }, '*');
1986
2003
  }
1987
- else {
1988
- if (result && typeof result.then === 'function') {
1989
- result
1990
- .then(function (finalResult) {
1991
- var _a;
1992
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1993
- type: 'builder.evaluateResult',
1994
- data: { id: id_1, result: finalResult },
1995
- }, '*');
1996
- })
1997
- .catch(console.error);
1998
- }
1999
- else {
2000
- (_e = window.parent) === null || _e === void 0 ? void 0 : _e.postMessage({
2001
- type: 'builder.evaluateResult',
2002
- data: { result: result, id: id_1 },
2003
- }, '*');
2004
- }
2005
- }
2006
- break;
2007
2004
  }
2005
+ break;
2008
2006
  }
2009
2007
  }
2010
- });
2011
- }
2008
+ }
2009
+ });
2012
2010
  };
2013
2011
  Object.defineProperty(Builder.prototype, "defaultCanTrack", {
2014
2012
  get: function () {
@@ -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 = {}; }
@@ -2670,6 +2681,7 @@ var Builder = /** @class */ (function () {
2670
2681
  (location.search.indexOf('builder.preview=') !== -1 ||
2671
2682
  location.search.indexOf('builder.frameEditing=') !== -1));
2672
2683
  Builder.isReact = false;
2684
+ Builder.sdkInfo = undefined;
2673
2685
  Builder.overrideUserAttributes = {};
2674
2686
  return Builder;
2675
2687
  }());