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