@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.cjs.js CHANGED
@@ -958,7 +958,7 @@ function toError(err) {
958
958
 
959
959
  var DEFAULT_API_VERSION = 'v3';
960
960
 
961
- var SDK_VERSION = '3.0.6';
961
+ var SDK_VERSION = '3.0.8';
962
962
 
963
963
  function datePlusMinutes(minutes) {
964
964
  if (minutes === void 0) { minutes = 30; }
@@ -1153,8 +1153,8 @@ var Builder = /** @class */ (function () {
1153
1153
  this.authToken = authToken;
1154
1154
  }
1155
1155
  if (isBrowser) {
1156
- this.bindMessageListeners();
1157
1156
  if (Builder.isEditing) {
1157
+ this.bindMessageListeners();
1158
1158
  parent.postMessage({
1159
1159
  type: 'builder.animatorOptions',
1160
1160
  data: {
@@ -1285,11 +1285,16 @@ var Builder = /** @class */ (function () {
1285
1285
  this.serverContext = context;
1286
1286
  };
1287
1287
  Builder.isTrustedHost = function (hostname) {
1288
- return (this.trustedHosts.findIndex(function (trustedHost) {
1288
+ var isTrusted = this.trustedHosts.findIndex(function (trustedHost) {
1289
1289
  return trustedHost.startsWith('*.')
1290
1290
  ? hostname.endsWith(trustedHost.slice(1))
1291
1291
  : trustedHost === hostname;
1292
- }) > -1);
1292
+ }) > -1;
1293
+ return isTrusted;
1294
+ };
1295
+ Builder.isTrustedHostForEvent = function (event) {
1296
+ var url = parse(event.origin);
1297
+ return url.hostname && Builder.isTrustedHost(url.hostname);
1293
1298
  };
1294
1299
  Builder.runAction = function (action) {
1295
1300
  // TODO
@@ -1448,9 +1453,7 @@ var Builder = /** @class */ (function () {
1448
1453
  getFetch()("".concat(host, "/api/v1/track"), {
1449
1454
  method: 'POST',
1450
1455
  body: JSON.stringify({ events: events }),
1451
- headers: {
1452
- 'content-type': 'application/json',
1453
- },
1456
+ headers: tslib.__assign({ 'content-type': 'application/json' }, this.getSdkHeaders()),
1454
1457
  mode: 'cors',
1455
1458
  }).catch(function () {
1456
1459
  // Not the end of the world
@@ -1839,184 +1842,179 @@ var Builder = /** @class */ (function () {
1839
1842
  };
1840
1843
  Builder.prototype.bindMessageListeners = function () {
1841
1844
  var _this = this;
1842
- if (isBrowser) {
1843
- addEventListener('message', function (event) {
1844
- var _a, _b, _c, _d, _e;
1845
- var url = parse(event.origin);
1846
- var isRestricted = ['builder.register', 'builder.registerComponent'].indexOf((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === -1;
1847
- var isTrusted = url.hostname && Builder.isTrustedHost(url.hostname);
1848
- if (isRestricted && !isTrusted) {
1849
- return;
1850
- }
1851
- var data = event.data;
1852
- if (data) {
1853
- switch (data.type) {
1854
- case 'builder.ping': {
1855
- (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1856
- type: 'builder.pong',
1857
- data: {},
1858
- }, '*');
1845
+ addEventListener('message', function (event) {
1846
+ var _a, _b, _c, _d;
1847
+ var isTrusted = Builder.isTrustedHostForEvent(event);
1848
+ if (!isTrusted)
1849
+ return;
1850
+ var data = event.data;
1851
+ if (data) {
1852
+ switch (data.type) {
1853
+ case 'builder.ping': {
1854
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1855
+ type: 'builder.pong',
1856
+ data: {},
1857
+ }, '*');
1858
+ break;
1859
+ }
1860
+ case 'builder.register': {
1861
+ // TODO: possibly do this for all...
1862
+ if (event.source === window) {
1859
1863
  break;
1860
1864
  }
1861
- case 'builder.register': {
1862
- // TODO: possibly do this for all...
1863
- if (event.source === window) {
1864
- break;
1865
- }
1866
- var options = data.data;
1867
- if (!options) {
1868
- break;
1869
- }
1870
- var type = options.type, info = options.info;
1871
- // TODO: all must have name and can't conflict?
1872
- var typeList = Builder.registry[type];
1873
- if (!typeList) {
1874
- typeList = Builder.registry[type] = [];
1875
- }
1876
- typeList.push(info);
1877
- Builder.registryChange.next(Builder.registry);
1865
+ var options = data.data;
1866
+ if (!options) {
1878
1867
  break;
1879
1868
  }
1880
- case 'builder.settingsChange': {
1881
- // TODO: possibly do this for all...
1882
- if (event.source === window) {
1883
- break;
1884
- }
1885
- var settings = data.data;
1886
- if (!settings) {
1887
- break;
1888
- }
1889
- Object.assign(Builder.settings, settings);
1890
- Builder.settingsChange.next(Builder.settings);
1869
+ var type = options.type, info = options.info;
1870
+ // TODO: all must have name and can't conflict?
1871
+ var typeList = Builder.registry[type];
1872
+ if (!typeList) {
1873
+ typeList = Builder.registry[type] = [];
1874
+ }
1875
+ typeList.push(info);
1876
+ Builder.registryChange.next(Builder.registry);
1877
+ break;
1878
+ }
1879
+ case 'builder.settingsChange': {
1880
+ // TODO: possibly do this for all...
1881
+ if (event.source === window) {
1891
1882
  break;
1892
1883
  }
1893
- case 'builder.registerEditor': {
1894
- // TODO: possibly do this for all...
1895
- if (event.source === window) {
1896
- break;
1897
- }
1898
- var info_1 = data.data;
1899
- if (!info_1) {
1900
- break;
1901
- }
1902
- var hasComponent_1 = !!info_1.component;
1903
- Builder.editors.every(function (thisInfo, index) {
1904
- if (info_1.name === thisInfo.name) {
1905
- if (thisInfo.component && !hasComponent_1) {
1906
- return false;
1907
- }
1908
- else {
1909
- Builder.editors[index] = thisInfo;
1910
- }
1911
- return false;
1912
- }
1913
- return true;
1914
- });
1884
+ var settings = data.data;
1885
+ if (!settings) {
1915
1886
  break;
1916
1887
  }
1917
- case 'builder.triggerAnimation': {
1918
- Builder.animator.triggerAnimation(data.data);
1888
+ Object.assign(Builder.settings, settings);
1889
+ Builder.settingsChange.next(Builder.settings);
1890
+ break;
1891
+ }
1892
+ case 'builder.registerEditor': {
1893
+ // TODO: possibly do this for all...
1894
+ if (event.source === window) {
1919
1895
  break;
1920
1896
  }
1921
- case 'builder.contentUpdate':
1922
- var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
1923
- var contentData = data.data.data; // hmmm...
1924
- var observer = _this.observersByKey[key];
1925
- if (observer && !_this.noEditorUpdates[key]) {
1926
- observer.next([contentData]);
1927
- }
1897
+ var info_1 = data.data;
1898
+ if (!info_1) {
1928
1899
  break;
1929
- case 'builder.getComponents':
1900
+ }
1901
+ var hasComponent_1 = !!info_1.component;
1902
+ Builder.editors.every(function (thisInfo, index) {
1903
+ if (info_1.name === thisInfo.name) {
1904
+ if (thisInfo.component && !hasComponent_1) {
1905
+ return false;
1906
+ }
1907
+ else {
1908
+ Builder.editors[index] = thisInfo;
1909
+ }
1910
+ return false;
1911
+ }
1912
+ return true;
1913
+ });
1914
+ break;
1915
+ }
1916
+ case 'builder.triggerAnimation': {
1917
+ Builder.animator.triggerAnimation(data.data);
1918
+ break;
1919
+ }
1920
+ case 'builder.contentUpdate':
1921
+ var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
1922
+ var contentData = data.data.data; // hmmm...
1923
+ var observer = _this.observersByKey[key];
1924
+ if (observer && !_this.noEditorUpdates[key]) {
1925
+ observer.next([contentData]);
1926
+ }
1927
+ break;
1928
+ case 'builder.getComponents':
1929
+ (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1930
+ type: 'builder.components',
1931
+ data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1932
+ }, '*');
1933
+ break;
1934
+ case 'builder.editingModel':
1935
+ _this.editingModel = data.data.model;
1936
+ break;
1937
+ case 'builder.registerComponent':
1938
+ var componentData = data.data;
1939
+ Builder.addComponent(componentData);
1940
+ break;
1941
+ case 'builder.blockContentLoading':
1942
+ if (typeof data.data.model === 'string') {
1943
+ _this.blockContentLoading = data.data.model;
1944
+ }
1945
+ break;
1946
+ case 'builder.editingMode':
1947
+ var editingMode = data.data;
1948
+ if (editingMode) {
1949
+ _this.editingMode = true;
1950
+ document.body.classList.add('builder-editing');
1951
+ }
1952
+ else {
1953
+ _this.editingMode = false;
1954
+ document.body.classList.remove('builder-editing');
1955
+ }
1956
+ break;
1957
+ case 'builder.editingPageMode':
1958
+ var editingPageMode = data.data;
1959
+ Builder.editingPage = editingPageMode;
1960
+ break;
1961
+ case 'builder.overrideUserAttributes':
1962
+ var userAttributes = data.data;
1963
+ assign(Builder.overrideUserAttributes, userAttributes);
1964
+ _this.flushGetContentQueue(true);
1965
+ // TODO: refetch too
1966
+ break;
1967
+ case 'builder.overrideTestGroup':
1968
+ var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
1969
+ if (variationId && contentId) {
1970
+ _this.setTestCookie(contentId, variationId);
1971
+ _this.flushGetContentQueue(true);
1972
+ }
1973
+ break;
1974
+ case 'builder.evaluate': {
1975
+ var text = data.data.text;
1976
+ var args = data.data.arguments || [];
1977
+ var id_1 = data.data.id;
1978
+ // tslint:disable-next-line:no-function-constructor-with-string-args
1979
+ var fn = new Function(text);
1980
+ var result = void 0;
1981
+ var error = null;
1982
+ try {
1983
+ result = fn.apply(_this, args);
1984
+ }
1985
+ catch (err) {
1986
+ error = toError(err);
1987
+ }
1988
+ if (error) {
1930
1989
  (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
1931
- type: 'builder.components',
1932
- data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1990
+ type: 'builder.evaluateError',
1991
+ data: { id: id_1, error: error.message },
1933
1992
  }, '*');
1934
- break;
1935
- case 'builder.editingModel':
1936
- _this.editingModel = data.data.model;
1937
- break;
1938
- case 'builder.registerComponent':
1939
- var componentData = data.data;
1940
- Builder.addComponent(componentData);
1941
- break;
1942
- case 'builder.blockContentLoading':
1943
- if (typeof data.data.model === 'string') {
1944
- _this.blockContentLoading = data.data.model;
1945
- }
1946
- break;
1947
- case 'builder.editingMode':
1948
- var editingMode = data.data;
1949
- if (editingMode) {
1950
- _this.editingMode = true;
1951
- document.body.classList.add('builder-editing');
1993
+ }
1994
+ else {
1995
+ if (result && typeof result.then === 'function') {
1996
+ result
1997
+ .then(function (finalResult) {
1998
+ var _a;
1999
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
2000
+ type: 'builder.evaluateResult',
2001
+ data: { id: id_1, result: finalResult },
2002
+ }, '*');
2003
+ })
2004
+ .catch(console.error);
1952
2005
  }
1953
2006
  else {
1954
- _this.editingMode = false;
1955
- document.body.classList.remove('builder-editing');
1956
- }
1957
- break;
1958
- case 'builder.editingPageMode':
1959
- var editingPageMode = data.data;
1960
- Builder.editingPage = editingPageMode;
1961
- break;
1962
- case 'builder.overrideUserAttributes':
1963
- var userAttributes = data.data;
1964
- assign(Builder.overrideUserAttributes, userAttributes);
1965
- _this.flushGetContentQueue(true);
1966
- // TODO: refetch too
1967
- break;
1968
- case 'builder.overrideTestGroup':
1969
- var _f = data.data, variationId = _f.variationId, contentId = _f.contentId;
1970
- if (variationId && contentId) {
1971
- _this.setTestCookie(contentId, variationId);
1972
- _this.flushGetContentQueue(true);
1973
- }
1974
- break;
1975
- case 'builder.evaluate': {
1976
- var text = data.data.text;
1977
- var args = data.data.arguments || [];
1978
- var id_1 = data.data.id;
1979
- // tslint:disable-next-line:no-function-constructor-with-string-args
1980
- var fn = new Function(text);
1981
- var result = void 0;
1982
- var error = null;
1983
- try {
1984
- result = fn.apply(_this, args);
1985
- }
1986
- catch (err) {
1987
- error = toError(err);
1988
- }
1989
- if (error) {
1990
2007
  (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
1991
- type: 'builder.evaluateError',
1992
- data: { id: id_1, error: error.message },
2008
+ type: 'builder.evaluateResult',
2009
+ data: { result: result, id: id_1 },
1993
2010
  }, '*');
1994
2011
  }
1995
- else {
1996
- if (result && typeof result.then === 'function') {
1997
- result
1998
- .then(function (finalResult) {
1999
- var _a;
2000
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
2001
- type: 'builder.evaluateResult',
2002
- data: { id: id_1, result: finalResult },
2003
- }, '*');
2004
- })
2005
- .catch(console.error);
2006
- }
2007
- else {
2008
- (_e = window.parent) === null || _e === void 0 ? void 0 : _e.postMessage({
2009
- type: 'builder.evaluateResult',
2010
- data: { result: result, id: id_1 },
2011
- }, '*');
2012
- }
2013
- }
2014
- break;
2015
2012
  }
2013
+ break;
2016
2014
  }
2017
2015
  }
2018
- });
2019
- }
2016
+ }
2017
+ });
2020
2018
  };
2021
2019
  Object.defineProperty(Builder.prototype, "defaultCanTrack", {
2022
2020
  get: function () {
@@ -2249,7 +2247,7 @@ var Builder = /** @class */ (function () {
2249
2247
  // even though we only use `fetch()` now, we prefer to keep the old behavior and use the `fetch` that comes from
2250
2248
  // the core SDK for consistency
2251
2249
  Builder.prototype.requestUrl = function (url, options) {
2252
- return getFetch()(url, options).then(function (res) { return res.json(); });
2250
+ return getFetch()(url, this.addSdkHeaders(options)).then(function (res) { return res.json(); });
2253
2251
  };
2254
2252
  Object.defineProperty(Builder.prototype, "host", {
2255
2253
  get: function () {
@@ -2278,8 +2276,21 @@ var Builder = /** @class */ (function () {
2278
2276
  enumerable: false,
2279
2277
  configurable: true
2280
2278
  });
2279
+ Builder.prototype.getSdkHeaders = function () {
2280
+ if (!Builder.sdkInfo) {
2281
+ return {};
2282
+ }
2283
+ return {
2284
+ 'X-Builder-SDK': Builder.sdkInfo.name,
2285
+ 'X-Builder-SDK-GEN': '1',
2286
+ 'X-Builder-SDK-Version': Builder.sdkInfo.version,
2287
+ };
2288
+ };
2289
+ Builder.prototype.addSdkHeaders = function (fetchOptions) {
2290
+ return tslib.__assign(tslib.__assign({}, fetchOptions), { headers: tslib.__assign(tslib.__assign({}, fetchOptions.headers), this.getSdkHeaders()) });
2291
+ };
2281
2292
  Builder.prototype.makeFetchApiCall = function (url, requestOptions) {
2282
- return getFetch()(url, requestOptions);
2293
+ return getFetch()(url, this.addSdkHeaders(requestOptions));
2283
2294
  };
2284
2295
  Builder.prototype.flattenMongoQuery = function (obj, _current, _res) {
2285
2296
  if (_res === void 0) { _res = {}; }
@@ -2678,6 +2689,7 @@ var Builder = /** @class */ (function () {
2678
2689
  (location.search.indexOf('builder.preview=') !== -1 ||
2679
2690
  location.search.indexOf('builder.frameEditing=') !== -1));
2680
2691
  Builder.isReact = false;
2692
+ Builder.sdkInfo = undefined;
2681
2693
  Builder.overrideUserAttributes = {};
2682
2694
  return Builder;
2683
2695
  }());