@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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @builder.io/sdk
2
2
 
3
+ ## 4.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 9b11521: fix serializing single arg arrow functions that some compilers emit
8
+ - 027a07a: fix: standardize locale handling and pass through locale prop to personalization containers when filtering
9
+
10
+ ## 4.0.2
11
+
12
+ ### Patch Changes
13
+
14
+ - dda2ba4: Fix: Restrict event listening to when `isEditing === true`
15
+
3
16
  ## 4.0.1
4
17
 
5
18
  ### Patch Changes
@@ -1052,7 +1052,7 @@
1052
1052
 
1053
1053
  var DEFAULT_API_VERSION = 'v3';
1054
1054
 
1055
- var SDK_VERSION = '4.0.1';
1055
+ var SDK_VERSION = '4.0.3';
1056
1056
 
1057
1057
  function datePlusMinutes(minutes) {
1058
1058
  if (minutes === void 0) { minutes = 30; }
@@ -1247,8 +1247,8 @@
1247
1247
  this.authToken = authToken;
1248
1248
  }
1249
1249
  if (isBrowser) {
1250
- this.bindMessageListeners();
1251
1250
  if (Builder.isEditing) {
1251
+ this.bindMessageListeners();
1252
1252
  parent.postMessage({
1253
1253
  type: 'builder.animatorOptions',
1254
1254
  data: {
@@ -1452,7 +1452,9 @@
1452
1452
  // 1. `function name(args) => {code}`
1453
1453
  // 2. `name(args) => {code}`
1454
1454
  // 3. `(args) => {}`
1455
- var appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(');
1455
+ // 4. `args => {}`
1456
+ var isArrowWithoutParens = /^[a-zA-Z0-9_]+\s*=>/i.test(fnStr);
1457
+ var appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(') && !isArrowWithoutParens;
1456
1458
  return "return (".concat(appendFunction ? 'function ' : '').concat(fnStr, ").apply(this, arguments)");
1457
1459
  };
1458
1460
  return JSON.parse(JSON.stringify(info, function (key, value) {
@@ -1936,182 +1938,179 @@
1936
1938
  };
1937
1939
  Builder.prototype.bindMessageListeners = function () {
1938
1940
  var _this = this;
1939
- if (isBrowser) {
1940
- addEventListener('message', function (event) {
1941
- var _a, _b, _c, _d;
1942
- var isTrusted = Builder.isTrustedHostForEvent(event);
1943
- if (!isTrusted) {
1944
- return;
1945
- }
1946
- var data = event.data;
1947
- if (data) {
1948
- switch (data.type) {
1949
- case 'builder.ping': {
1950
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1951
- type: 'builder.pong',
1952
- data: {},
1953
- }, '*');
1941
+ addEventListener('message', function (event) {
1942
+ var _a, _b, _c, _d;
1943
+ var isTrusted = Builder.isTrustedHostForEvent(event);
1944
+ if (!isTrusted)
1945
+ return;
1946
+ var data = event.data;
1947
+ if (data) {
1948
+ switch (data.type) {
1949
+ case 'builder.ping': {
1950
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1951
+ type: 'builder.pong',
1952
+ data: {},
1953
+ }, '*');
1954
+ break;
1955
+ }
1956
+ case 'builder.register': {
1957
+ // TODO: possibly do this for all...
1958
+ if (event.source === window) {
1954
1959
  break;
1955
1960
  }
1956
- case 'builder.register': {
1957
- // TODO: possibly do this for all...
1958
- if (event.source === window) {
1959
- break;
1960
- }
1961
- var options = data.data;
1962
- if (!options) {
1963
- break;
1964
- }
1965
- var type = options.type, info = options.info;
1966
- // TODO: all must have name and can't conflict?
1967
- var typeList = Builder.registry[type];
1968
- if (!typeList) {
1969
- typeList = Builder.registry[type] = [];
1970
- }
1971
- typeList.push(info);
1972
- Builder.registryChange.next(Builder.registry);
1961
+ var options = data.data;
1962
+ if (!options) {
1973
1963
  break;
1974
1964
  }
1975
- case 'builder.settingsChange': {
1976
- // TODO: possibly do this for all...
1977
- if (event.source === window) {
1978
- break;
1979
- }
1980
- var settings = data.data;
1981
- if (!settings) {
1982
- break;
1983
- }
1984
- Object.assign(Builder.settings, settings);
1985
- Builder.settingsChange.next(Builder.settings);
1986
- break;
1965
+ var type = options.type, info = options.info;
1966
+ // TODO: all must have name and can't conflict?
1967
+ var typeList = Builder.registry[type];
1968
+ if (!typeList) {
1969
+ typeList = Builder.registry[type] = [];
1987
1970
  }
1988
- case 'builder.registerEditor': {
1989
- // TODO: possibly do this for all...
1990
- if (event.source === window) {
1991
- break;
1992
- }
1993
- var info_1 = data.data;
1994
- if (!info_1) {
1995
- break;
1996
- }
1997
- var hasComponent_1 = !!info_1.component;
1998
- Builder.editors.every(function (thisInfo, index) {
1999
- if (info_1.name === thisInfo.name) {
2000
- if (thisInfo.component && !hasComponent_1) {
2001
- return false;
2002
- }
2003
- else {
2004
- Builder.editors[index] = thisInfo;
2005
- }
2006
- return false;
2007
- }
2008
- return true;
2009
- });
1971
+ typeList.push(info);
1972
+ Builder.registryChange.next(Builder.registry);
1973
+ break;
1974
+ }
1975
+ case 'builder.settingsChange': {
1976
+ // TODO: possibly do this for all...
1977
+ if (event.source === window) {
2010
1978
  break;
2011
1979
  }
2012
- case 'builder.triggerAnimation': {
2013
- Builder.animator.triggerAnimation(data.data);
1980
+ var settings = data.data;
1981
+ if (!settings) {
2014
1982
  break;
2015
1983
  }
2016
- case 'builder.contentUpdate':
2017
- var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
2018
- var contentData = data.data.data; // hmmm...
2019
- var observer = _this.observersByKey[key];
2020
- if (observer && !_this.noEditorUpdates[key]) {
2021
- observer.next([contentData]);
2022
- }
2023
- break;
2024
- case 'builder.getComponents':
2025
- (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
2026
- type: 'builder.components',
2027
- data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
2028
- }, '*');
2029
- break;
2030
- case 'builder.editingModel':
2031
- _this.editingModel = data.data.model;
2032
- break;
2033
- case 'builder.registerComponent':
2034
- var componentData = data.data;
2035
- Builder.addComponent(componentData);
2036
- break;
2037
- case 'builder.blockContentLoading':
2038
- if (typeof data.data.model === 'string') {
2039
- _this.blockContentLoading = data.data.model;
2040
- }
2041
- break;
2042
- case 'builder.editingMode':
2043
- var editingMode = data.data;
2044
- if (editingMode) {
2045
- _this.editingMode = true;
2046
- document.body.classList.add('builder-editing');
2047
- }
2048
- else {
2049
- _this.editingMode = false;
2050
- document.body.classList.remove('builder-editing');
2051
- }
2052
- break;
2053
- case 'builder.editingPageMode':
2054
- var editingPageMode = data.data;
2055
- Builder.editingPage = editingPageMode;
2056
- break;
2057
- case 'builder.overrideUserAttributes':
2058
- var userAttributes = data.data;
2059
- assign(Builder.overrideUserAttributes, userAttributes);
2060
- _this.flushGetContentQueue(true);
2061
- // TODO: refetch too
1984
+ Object.assign(Builder.settings, settings);
1985
+ Builder.settingsChange.next(Builder.settings);
1986
+ break;
1987
+ }
1988
+ case 'builder.registerEditor': {
1989
+ // TODO: possibly do this for all...
1990
+ if (event.source === window) {
2062
1991
  break;
2063
- case 'builder.overrideTestGroup':
2064
- var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
2065
- if (variationId && contentId) {
2066
- _this.setTestCookie(contentId, variationId);
2067
- _this.flushGetContentQueue(true);
2068
- }
1992
+ }
1993
+ var info_1 = data.data;
1994
+ if (!info_1) {
2069
1995
  break;
2070
- case 'builder.evaluate': {
2071
- var text = data.data.text;
2072
- var args = data.data.arguments || [];
2073
- var id_1 = data.data.id;
2074
- // tslint:disable-next-line:no-function-constructor-with-string-args
2075
- var fn = new Function(text);
2076
- var result = void 0;
2077
- var error = null;
2078
- try {
2079
- result = fn.apply(_this, args);
2080
- }
2081
- catch (err) {
2082
- error = toError(err);
2083
- }
2084
- if (error) {
2085
- (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
2086
- type: 'builder.evaluateError',
2087
- data: { id: id_1, error: error.message },
2088
- }, '*');
2089
- }
2090
- else {
2091
- if (result && typeof result.then === 'function') {
2092
- result
2093
- .then(function (finalResult) {
2094
- var _a;
2095
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
2096
- type: 'builder.evaluateResult',
2097
- data: { id: id_1, result: finalResult },
2098
- }, '*');
2099
- })
2100
- .catch(console.error);
1996
+ }
1997
+ var hasComponent_1 = !!info_1.component;
1998
+ Builder.editors.every(function (thisInfo, index) {
1999
+ if (info_1.name === thisInfo.name) {
2000
+ if (thisInfo.component && !hasComponent_1) {
2001
+ return false;
2101
2002
  }
2102
2003
  else {
2103
- (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
2004
+ Builder.editors[index] = thisInfo;
2005
+ }
2006
+ return false;
2007
+ }
2008
+ return true;
2009
+ });
2010
+ break;
2011
+ }
2012
+ case 'builder.triggerAnimation': {
2013
+ Builder.animator.triggerAnimation(data.data);
2014
+ break;
2015
+ }
2016
+ case 'builder.contentUpdate':
2017
+ var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
2018
+ var contentData = data.data.data; // hmmm...
2019
+ var observer = _this.observersByKey[key];
2020
+ if (observer && !_this.noEditorUpdates[key]) {
2021
+ observer.next([contentData]);
2022
+ }
2023
+ break;
2024
+ case 'builder.getComponents':
2025
+ (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
2026
+ type: 'builder.components',
2027
+ data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
2028
+ }, '*');
2029
+ break;
2030
+ case 'builder.editingModel':
2031
+ _this.editingModel = data.data.model;
2032
+ break;
2033
+ case 'builder.registerComponent':
2034
+ var componentData = data.data;
2035
+ Builder.addComponent(componentData);
2036
+ break;
2037
+ case 'builder.blockContentLoading':
2038
+ if (typeof data.data.model === 'string') {
2039
+ _this.blockContentLoading = data.data.model;
2040
+ }
2041
+ break;
2042
+ case 'builder.editingMode':
2043
+ var editingMode = data.data;
2044
+ if (editingMode) {
2045
+ _this.editingMode = true;
2046
+ document.body.classList.add('builder-editing');
2047
+ }
2048
+ else {
2049
+ _this.editingMode = false;
2050
+ document.body.classList.remove('builder-editing');
2051
+ }
2052
+ break;
2053
+ case 'builder.editingPageMode':
2054
+ var editingPageMode = data.data;
2055
+ Builder.editingPage = editingPageMode;
2056
+ break;
2057
+ case 'builder.overrideUserAttributes':
2058
+ var userAttributes = data.data;
2059
+ assign(Builder.overrideUserAttributes, userAttributes);
2060
+ _this.flushGetContentQueue(true);
2061
+ // TODO: refetch too
2062
+ break;
2063
+ case 'builder.overrideTestGroup':
2064
+ var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
2065
+ if (variationId && contentId) {
2066
+ _this.setTestCookie(contentId, variationId);
2067
+ _this.flushGetContentQueue(true);
2068
+ }
2069
+ break;
2070
+ case 'builder.evaluate': {
2071
+ var text = data.data.text;
2072
+ var args = data.data.arguments || [];
2073
+ var id_1 = data.data.id;
2074
+ // tslint:disable-next-line:no-function-constructor-with-string-args
2075
+ var fn = new Function(text);
2076
+ var result = void 0;
2077
+ var error = null;
2078
+ try {
2079
+ result = fn.apply(_this, args);
2080
+ }
2081
+ catch (err) {
2082
+ error = toError(err);
2083
+ }
2084
+ if (error) {
2085
+ (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
2086
+ type: 'builder.evaluateError',
2087
+ data: { id: id_1, error: error.message },
2088
+ }, '*');
2089
+ }
2090
+ else {
2091
+ if (result && typeof result.then === 'function') {
2092
+ result
2093
+ .then(function (finalResult) {
2094
+ var _a;
2095
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
2104
2096
  type: 'builder.evaluateResult',
2105
- data: { result: result, id: id_1 },
2097
+ data: { id: id_1, result: finalResult },
2106
2098
  }, '*');
2107
- }
2099
+ })
2100
+ .catch(console.error);
2101
+ }
2102
+ else {
2103
+ (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
2104
+ type: 'builder.evaluateResult',
2105
+ data: { result: result, id: id_1 },
2106
+ }, '*');
2108
2107
  }
2109
- break;
2110
2108
  }
2109
+ break;
2111
2110
  }
2112
2111
  }
2113
- });
2114
- }
2112
+ }
2113
+ });
2115
2114
  };
2116
2115
  Object.defineProperty(Builder.prototype, "defaultCanTrack", {
2117
2116
  get: function () {
@@ -2221,8 +2220,14 @@
2221
2220
  assign(this.trackingUserAttributes, attributes);
2222
2221
  };
2223
2222
  Builder.prototype.get = function (modelName, options) {
2223
+ var _a;
2224
2224
  if (options === void 0) { options = {}; }
2225
2225
  var instance = this;
2226
+ var finalLocale = options.locale || ((_a = options.userAttributes) === null || _a === void 0 ? void 0 : _a.locale) || this.getUserAttributes().locale;
2227
+ var finalOptions = __assign(__assign({}, options), (finalLocale && {
2228
+ locale: String(finalLocale),
2229
+ userAttributes: __assign({ locale: String(finalLocale) }, options.userAttributes),
2230
+ }));
2226
2231
  if (!Builder.isBrowser) {
2227
2232
  instance = new Builder(options.apiKey || this.apiKey, options.req, options.res, undefined, options.authToken || this.authToken, options.apiVersion || this.apiVersion);
2228
2233
  instance.setUserAttributes(this.getUserAttributes());
@@ -2240,7 +2245,7 @@
2240
2245
  this.apiVersion = options.apiVersion;
2241
2246
  }
2242
2247
  }
2243
- return instance.queueGetContent(modelName, options).map(
2248
+ return instance.queueGetContent(modelName, finalOptions).map(
2244
2249
  /* map( */ function (matches) {
2245
2250
  var match = matches && matches[0];
2246
2251
  if (Builder.isStatic) {
@@ -2448,9 +2453,9 @@
2448
2453
  if (queue[0].sort) {
2449
2454
  queryParams.sort = queue[0].sort;
2450
2455
  }
2451
- var pageQueryParams = typeof location !== 'undefined'
2456
+ var pageQueryParams = (typeof location !== 'undefined'
2452
2457
  ? QueryString.parseDeep(location.search.substr(1))
2453
- : {}; // TODO: WHAT about SSR (this.request) ?
2458
+ : undefined) || {}; // TODO: WHAT about SSR (this.request) ?
2454
2459
  var userAttributes =
2455
2460
  // FIXME: HACK: only checks first in queue for user attributes overrides, should check all
2456
2461
  // TODO: merge user attributes provided here with defaults and current user attiributes (?)