@builder.io/sdk 4.0.1 → 4.0.3

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 = '4.0.1';
961
+ var SDK_VERSION = '4.0.3';
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: {
@@ -1358,7 +1358,9 @@ var Builder = /** @class */ (function () {
1358
1358
  // 1. `function name(args) => {code}`
1359
1359
  // 2. `name(args) => {code}`
1360
1360
  // 3. `(args) => {}`
1361
- var appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(');
1361
+ // 4. `args => {}`
1362
+ var isArrowWithoutParens = /^[a-zA-Z0-9_]+\s*=>/i.test(fnStr);
1363
+ var appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(') && !isArrowWithoutParens;
1362
1364
  return "return (".concat(appendFunction ? 'function ' : '').concat(fnStr, ").apply(this, arguments)");
1363
1365
  };
1364
1366
  return JSON.parse(JSON.stringify(info, function (key, value) {
@@ -1842,182 +1844,179 @@ var Builder = /** @class */ (function () {
1842
1844
  };
1843
1845
  Builder.prototype.bindMessageListeners = function () {
1844
1846
  var _this = this;
1845
- if (isBrowser) {
1846
- addEventListener('message', function (event) {
1847
- var _a, _b, _c, _d;
1848
- var isTrusted = Builder.isTrustedHostForEvent(event);
1849
- if (!isTrusted) {
1850
- return;
1851
- }
1852
- var data = event.data;
1853
- if (data) {
1854
- switch (data.type) {
1855
- case 'builder.ping': {
1856
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1857
- type: 'builder.pong',
1858
- data: {},
1859
- }, '*');
1847
+ addEventListener('message', function (event) {
1848
+ var _a, _b, _c, _d;
1849
+ var isTrusted = Builder.isTrustedHostForEvent(event);
1850
+ if (!isTrusted)
1851
+ return;
1852
+ var data = event.data;
1853
+ if (data) {
1854
+ switch (data.type) {
1855
+ case 'builder.ping': {
1856
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1857
+ type: 'builder.pong',
1858
+ data: {},
1859
+ }, '*');
1860
+ break;
1861
+ }
1862
+ case 'builder.register': {
1863
+ // TODO: possibly do this for all...
1864
+ if (event.source === window) {
1860
1865
  break;
1861
1866
  }
1862
- case 'builder.register': {
1863
- // TODO: possibly do this for all...
1864
- if (event.source === window) {
1865
- break;
1866
- }
1867
- var options = data.data;
1868
- if (!options) {
1869
- break;
1870
- }
1871
- var type = options.type, info = options.info;
1872
- // TODO: all must have name and can't conflict?
1873
- var typeList = Builder.registry[type];
1874
- if (!typeList) {
1875
- typeList = Builder.registry[type] = [];
1876
- }
1877
- typeList.push(info);
1878
- Builder.registryChange.next(Builder.registry);
1867
+ var options = data.data;
1868
+ if (!options) {
1879
1869
  break;
1880
1870
  }
1881
- case 'builder.settingsChange': {
1882
- // TODO: possibly do this for all...
1883
- if (event.source === window) {
1884
- break;
1885
- }
1886
- var settings = data.data;
1887
- if (!settings) {
1888
- break;
1889
- }
1890
- Object.assign(Builder.settings, settings);
1891
- Builder.settingsChange.next(Builder.settings);
1892
- break;
1871
+ var type = options.type, info = options.info;
1872
+ // TODO: all must have name and can't conflict?
1873
+ var typeList = Builder.registry[type];
1874
+ if (!typeList) {
1875
+ typeList = Builder.registry[type] = [];
1893
1876
  }
1894
- case 'builder.registerEditor': {
1895
- // TODO: possibly do this for all...
1896
- if (event.source === window) {
1897
- break;
1898
- }
1899
- var info_1 = data.data;
1900
- if (!info_1) {
1901
- break;
1902
- }
1903
- var hasComponent_1 = !!info_1.component;
1904
- Builder.editors.every(function (thisInfo, index) {
1905
- if (info_1.name === thisInfo.name) {
1906
- if (thisInfo.component && !hasComponent_1) {
1907
- return false;
1908
- }
1909
- else {
1910
- Builder.editors[index] = thisInfo;
1911
- }
1912
- return false;
1913
- }
1914
- return true;
1915
- });
1877
+ typeList.push(info);
1878
+ Builder.registryChange.next(Builder.registry);
1879
+ break;
1880
+ }
1881
+ case 'builder.settingsChange': {
1882
+ // TODO: possibly do this for all...
1883
+ if (event.source === window) {
1916
1884
  break;
1917
1885
  }
1918
- case 'builder.triggerAnimation': {
1919
- Builder.animator.triggerAnimation(data.data);
1886
+ var settings = data.data;
1887
+ if (!settings) {
1920
1888
  break;
1921
1889
  }
1922
- case 'builder.contentUpdate':
1923
- var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
1924
- var contentData = data.data.data; // hmmm...
1925
- var observer = _this.observersByKey[key];
1926
- if (observer && !_this.noEditorUpdates[key]) {
1927
- observer.next([contentData]);
1928
- }
1929
- break;
1930
- case 'builder.getComponents':
1931
- (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1932
- type: 'builder.components',
1933
- data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1934
- }, '*');
1935
- break;
1936
- case 'builder.editingModel':
1937
- _this.editingModel = data.data.model;
1938
- break;
1939
- case 'builder.registerComponent':
1940
- var componentData = data.data;
1941
- Builder.addComponent(componentData);
1942
- break;
1943
- case 'builder.blockContentLoading':
1944
- if (typeof data.data.model === 'string') {
1945
- _this.blockContentLoading = data.data.model;
1946
- }
1947
- break;
1948
- case 'builder.editingMode':
1949
- var editingMode = data.data;
1950
- if (editingMode) {
1951
- _this.editingMode = true;
1952
- document.body.classList.add('builder-editing');
1953
- }
1954
- else {
1955
- _this.editingMode = false;
1956
- document.body.classList.remove('builder-editing');
1957
- }
1958
- break;
1959
- case 'builder.editingPageMode':
1960
- var editingPageMode = data.data;
1961
- Builder.editingPage = editingPageMode;
1962
- break;
1963
- case 'builder.overrideUserAttributes':
1964
- var userAttributes = data.data;
1965
- assign(Builder.overrideUserAttributes, userAttributes);
1966
- _this.flushGetContentQueue(true);
1967
- // TODO: refetch too
1890
+ Object.assign(Builder.settings, settings);
1891
+ Builder.settingsChange.next(Builder.settings);
1892
+ break;
1893
+ }
1894
+ case 'builder.registerEditor': {
1895
+ // TODO: possibly do this for all...
1896
+ if (event.source === window) {
1968
1897
  break;
1969
- case 'builder.overrideTestGroup':
1970
- var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
1971
- if (variationId && contentId) {
1972
- _this.setTestCookie(contentId, variationId);
1973
- _this.flushGetContentQueue(true);
1974
- }
1898
+ }
1899
+ var info_1 = data.data;
1900
+ if (!info_1) {
1975
1901
  break;
1976
- case 'builder.evaluate': {
1977
- var text = data.data.text;
1978
- var args = data.data.arguments || [];
1979
- var id_1 = data.data.id;
1980
- // tslint:disable-next-line:no-function-constructor-with-string-args
1981
- var fn = new Function(text);
1982
- var result = void 0;
1983
- var error = null;
1984
- try {
1985
- result = fn.apply(_this, args);
1986
- }
1987
- catch (err) {
1988
- error = toError(err);
1989
- }
1990
- if (error) {
1991
- (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
1992
- type: 'builder.evaluateError',
1993
- data: { id: id_1, error: error.message },
1994
- }, '*');
1995
- }
1996
- else {
1997
- if (result && typeof result.then === 'function') {
1998
- result
1999
- .then(function (finalResult) {
2000
- var _a;
2001
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
2002
- type: 'builder.evaluateResult',
2003
- data: { id: id_1, result: finalResult },
2004
- }, '*');
2005
- })
2006
- .catch(console.error);
1902
+ }
1903
+ var hasComponent_1 = !!info_1.component;
1904
+ Builder.editors.every(function (thisInfo, index) {
1905
+ if (info_1.name === thisInfo.name) {
1906
+ if (thisInfo.component && !hasComponent_1) {
1907
+ return false;
2007
1908
  }
2008
1909
  else {
2009
- (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
1910
+ Builder.editors[index] = thisInfo;
1911
+ }
1912
+ return false;
1913
+ }
1914
+ return true;
1915
+ });
1916
+ break;
1917
+ }
1918
+ case 'builder.triggerAnimation': {
1919
+ Builder.animator.triggerAnimation(data.data);
1920
+ break;
1921
+ }
1922
+ case 'builder.contentUpdate':
1923
+ var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
1924
+ var contentData = data.data.data; // hmmm...
1925
+ var observer = _this.observersByKey[key];
1926
+ if (observer && !_this.noEditorUpdates[key]) {
1927
+ observer.next([contentData]);
1928
+ }
1929
+ break;
1930
+ case 'builder.getComponents':
1931
+ (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1932
+ type: 'builder.components',
1933
+ data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1934
+ }, '*');
1935
+ break;
1936
+ case 'builder.editingModel':
1937
+ _this.editingModel = data.data.model;
1938
+ break;
1939
+ case 'builder.registerComponent':
1940
+ var componentData = data.data;
1941
+ Builder.addComponent(componentData);
1942
+ break;
1943
+ case 'builder.blockContentLoading':
1944
+ if (typeof data.data.model === 'string') {
1945
+ _this.blockContentLoading = data.data.model;
1946
+ }
1947
+ break;
1948
+ case 'builder.editingMode':
1949
+ var editingMode = data.data;
1950
+ if (editingMode) {
1951
+ _this.editingMode = true;
1952
+ document.body.classList.add('builder-editing');
1953
+ }
1954
+ else {
1955
+ _this.editingMode = false;
1956
+ document.body.classList.remove('builder-editing');
1957
+ }
1958
+ break;
1959
+ case 'builder.editingPageMode':
1960
+ var editingPageMode = data.data;
1961
+ Builder.editingPage = editingPageMode;
1962
+ break;
1963
+ case 'builder.overrideUserAttributes':
1964
+ var userAttributes = data.data;
1965
+ assign(Builder.overrideUserAttributes, userAttributes);
1966
+ _this.flushGetContentQueue(true);
1967
+ // TODO: refetch too
1968
+ break;
1969
+ case 'builder.overrideTestGroup':
1970
+ var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
1971
+ if (variationId && contentId) {
1972
+ _this.setTestCookie(contentId, variationId);
1973
+ _this.flushGetContentQueue(true);
1974
+ }
1975
+ break;
1976
+ case 'builder.evaluate': {
1977
+ var text = data.data.text;
1978
+ var args = data.data.arguments || [];
1979
+ var id_1 = data.data.id;
1980
+ // tslint:disable-next-line:no-function-constructor-with-string-args
1981
+ var fn = new Function(text);
1982
+ var result = void 0;
1983
+ var error = null;
1984
+ try {
1985
+ result = fn.apply(_this, args);
1986
+ }
1987
+ catch (err) {
1988
+ error = toError(err);
1989
+ }
1990
+ if (error) {
1991
+ (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
1992
+ type: 'builder.evaluateError',
1993
+ data: { id: id_1, error: error.message },
1994
+ }, '*');
1995
+ }
1996
+ else {
1997
+ if (result && typeof result.then === 'function') {
1998
+ result
1999
+ .then(function (finalResult) {
2000
+ var _a;
2001
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
2010
2002
  type: 'builder.evaluateResult',
2011
- data: { result: result, id: id_1 },
2003
+ data: { id: id_1, result: finalResult },
2012
2004
  }, '*');
2013
- }
2005
+ })
2006
+ .catch(console.error);
2007
+ }
2008
+ else {
2009
+ (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
2010
+ type: 'builder.evaluateResult',
2011
+ data: { result: result, id: id_1 },
2012
+ }, '*');
2014
2013
  }
2015
- break;
2016
2014
  }
2015
+ break;
2017
2016
  }
2018
2017
  }
2019
- });
2020
- }
2018
+ }
2019
+ });
2021
2020
  };
2022
2021
  Object.defineProperty(Builder.prototype, "defaultCanTrack", {
2023
2022
  get: function () {
@@ -2127,8 +2126,14 @@ var Builder = /** @class */ (function () {
2127
2126
  assign(this.trackingUserAttributes, attributes);
2128
2127
  };
2129
2128
  Builder.prototype.get = function (modelName, options) {
2129
+ var _a;
2130
2130
  if (options === void 0) { options = {}; }
2131
2131
  var instance = this;
2132
+ var finalLocale = options.locale || ((_a = options.userAttributes) === null || _a === void 0 ? void 0 : _a.locale) || this.getUserAttributes().locale;
2133
+ var finalOptions = tslib.__assign(tslib.__assign({}, options), (finalLocale && {
2134
+ locale: String(finalLocale),
2135
+ userAttributes: tslib.__assign({ locale: String(finalLocale) }, options.userAttributes),
2136
+ }));
2132
2137
  if (!Builder.isBrowser) {
2133
2138
  instance = new Builder(options.apiKey || this.apiKey, options.req, options.res, undefined, options.authToken || this.authToken, options.apiVersion || this.apiVersion);
2134
2139
  instance.setUserAttributes(this.getUserAttributes());
@@ -2146,7 +2151,7 @@ var Builder = /** @class */ (function () {
2146
2151
  this.apiVersion = options.apiVersion;
2147
2152
  }
2148
2153
  }
2149
- return instance.queueGetContent(modelName, options).map(
2154
+ return instance.queueGetContent(modelName, finalOptions).map(
2150
2155
  /* map( */ function (matches) {
2151
2156
  var match = matches && matches[0];
2152
2157
  if (Builder.isStatic) {
@@ -2354,9 +2359,9 @@ var Builder = /** @class */ (function () {
2354
2359
  if (queue[0].sort) {
2355
2360
  queryParams.sort = queue[0].sort;
2356
2361
  }
2357
- var pageQueryParams = typeof location !== 'undefined'
2362
+ var pageQueryParams = (typeof location !== 'undefined'
2358
2363
  ? QueryString.parseDeep(location.search.substr(1))
2359
- : {}; // TODO: WHAT about SSR (this.request) ?
2364
+ : undefined) || {}; // TODO: WHAT about SSR (this.request) ?
2360
2365
  var userAttributes =
2361
2366
  // FIXME: HACK: only checks first in queue for user attributes overrides, should check all
2362
2367
  // TODO: merge user attributes provided here with defaults and current user attiributes (?)