@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder.io/sdk",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
4
4
  "unpkg": "./dist/index.browser.js",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.esm.js",
@@ -228,8 +228,8 @@ var Builder = /** @class */ (function () {
228
228
  this.authToken = authToken;
229
229
  }
230
230
  if (exports.isBrowser) {
231
- this.bindMessageListeners();
232
231
  if (Builder.isEditing) {
232
+ this.bindMessageListeners();
233
233
  parent.postMessage({
234
234
  type: 'builder.animatorOptions',
235
235
  data: {
@@ -433,7 +433,9 @@ var Builder = /** @class */ (function () {
433
433
  // 1. `function name(args) => {code}`
434
434
  // 2. `name(args) => {code}`
435
435
  // 3. `(args) => {}`
436
- var appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(');
436
+ // 4. `args => {}`
437
+ var isArrowWithoutParens = /^[a-zA-Z0-9_]+\s*=>/i.test(fnStr);
438
+ var appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(') && !isArrowWithoutParens;
437
439
  return "return (".concat(appendFunction ? 'function ' : '').concat(fnStr, ").apply(this, arguments)");
438
440
  };
439
441
  return JSON.parse(JSON.stringify(info, function (key, value) {
@@ -917,182 +919,179 @@ var Builder = /** @class */ (function () {
917
919
  };
918
920
  Builder.prototype.bindMessageListeners = function () {
919
921
  var _this = this;
920
- if (exports.isBrowser) {
921
- addEventListener('message', function (event) {
922
- var _a, _b, _c, _d;
923
- var isTrusted = Builder.isTrustedHostForEvent(event);
924
- if (!isTrusted) {
925
- return;
926
- }
927
- var data = event.data;
928
- if (data) {
929
- switch (data.type) {
930
- case 'builder.ping': {
931
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
932
- type: 'builder.pong',
933
- data: {},
934
- }, '*');
922
+ addEventListener('message', function (event) {
923
+ var _a, _b, _c, _d;
924
+ var isTrusted = Builder.isTrustedHostForEvent(event);
925
+ if (!isTrusted)
926
+ return;
927
+ var data = event.data;
928
+ if (data) {
929
+ switch (data.type) {
930
+ case 'builder.ping': {
931
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
932
+ type: 'builder.pong',
933
+ data: {},
934
+ }, '*');
935
+ break;
936
+ }
937
+ case 'builder.register': {
938
+ // TODO: possibly do this for all...
939
+ if (event.source === window) {
935
940
  break;
936
941
  }
937
- case 'builder.register': {
938
- // TODO: possibly do this for all...
939
- if (event.source === window) {
940
- break;
941
- }
942
- var options = data.data;
943
- if (!options) {
944
- break;
945
- }
946
- var type = options.type, info = options.info;
947
- // TODO: all must have name and can't conflict?
948
- var typeList = Builder.registry[type];
949
- if (!typeList) {
950
- typeList = Builder.registry[type] = [];
951
- }
952
- typeList.push(info);
953
- Builder.registryChange.next(Builder.registry);
942
+ var options = data.data;
943
+ if (!options) {
954
944
  break;
955
945
  }
956
- case 'builder.settingsChange': {
957
- // TODO: possibly do this for all...
958
- if (event.source === window) {
959
- break;
960
- }
961
- var settings = data.data;
962
- if (!settings) {
963
- break;
964
- }
965
- Object.assign(Builder.settings, settings);
966
- Builder.settingsChange.next(Builder.settings);
967
- break;
946
+ var type = options.type, info = options.info;
947
+ // TODO: all must have name and can't conflict?
948
+ var typeList = Builder.registry[type];
949
+ if (!typeList) {
950
+ typeList = Builder.registry[type] = [];
968
951
  }
969
- case 'builder.registerEditor': {
970
- // TODO: possibly do this for all...
971
- if (event.source === window) {
972
- break;
973
- }
974
- var info_1 = data.data;
975
- if (!info_1) {
976
- break;
977
- }
978
- var hasComponent_1 = !!info_1.component;
979
- Builder.editors.every(function (thisInfo, index) {
980
- if (info_1.name === thisInfo.name) {
981
- if (thisInfo.component && !hasComponent_1) {
982
- return false;
983
- }
984
- else {
985
- Builder.editors[index] = thisInfo;
986
- }
987
- return false;
988
- }
989
- return true;
990
- });
952
+ typeList.push(info);
953
+ Builder.registryChange.next(Builder.registry);
954
+ break;
955
+ }
956
+ case 'builder.settingsChange': {
957
+ // TODO: possibly do this for all...
958
+ if (event.source === window) {
991
959
  break;
992
960
  }
993
- case 'builder.triggerAnimation': {
994
- Builder.animator.triggerAnimation(data.data);
961
+ var settings = data.data;
962
+ if (!settings) {
995
963
  break;
996
964
  }
997
- case 'builder.contentUpdate':
998
- var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
999
- var contentData = data.data.data; // hmmm...
1000
- var observer = _this.observersByKey[key];
1001
- if (observer && !_this.noEditorUpdates[key]) {
1002
- observer.next([contentData]);
1003
- }
1004
- break;
1005
- case 'builder.getComponents':
1006
- (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1007
- type: 'builder.components',
1008
- data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1009
- }, '*');
1010
- break;
1011
- case 'builder.editingModel':
1012
- _this.editingModel = data.data.model;
1013
- break;
1014
- case 'builder.registerComponent':
1015
- var componentData = data.data;
1016
- Builder.addComponent(componentData);
1017
- break;
1018
- case 'builder.blockContentLoading':
1019
- if (typeof data.data.model === 'string') {
1020
- _this.blockContentLoading = data.data.model;
1021
- }
1022
- break;
1023
- case 'builder.editingMode':
1024
- var editingMode = data.data;
1025
- if (editingMode) {
1026
- _this.editingMode = true;
1027
- document.body.classList.add('builder-editing');
1028
- }
1029
- else {
1030
- _this.editingMode = false;
1031
- document.body.classList.remove('builder-editing');
1032
- }
1033
- break;
1034
- case 'builder.editingPageMode':
1035
- var editingPageMode = data.data;
1036
- Builder.editingPage = editingPageMode;
1037
- break;
1038
- case 'builder.overrideUserAttributes':
1039
- var userAttributes = data.data;
1040
- (0, assign_function_1.assign)(Builder.overrideUserAttributes, userAttributes);
1041
- _this.flushGetContentQueue(true);
1042
- // TODO: refetch too
965
+ Object.assign(Builder.settings, settings);
966
+ Builder.settingsChange.next(Builder.settings);
967
+ break;
968
+ }
969
+ case 'builder.registerEditor': {
970
+ // TODO: possibly do this for all...
971
+ if (event.source === window) {
1043
972
  break;
1044
- case 'builder.overrideTestGroup':
1045
- var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
1046
- if (variationId && contentId) {
1047
- _this.setTestCookie(contentId, variationId);
1048
- _this.flushGetContentQueue(true);
1049
- }
973
+ }
974
+ var info_1 = data.data;
975
+ if (!info_1) {
1050
976
  break;
1051
- case 'builder.evaluate': {
1052
- var text = data.data.text;
1053
- var args = data.data.arguments || [];
1054
- var id_1 = data.data.id;
1055
- // tslint:disable-next-line:no-function-constructor-with-string-args
1056
- var fn = new Function(text);
1057
- var result = void 0;
1058
- var error = null;
1059
- try {
1060
- result = fn.apply(_this, args);
1061
- }
1062
- catch (err) {
1063
- error = (0, to_error_1.toError)(err);
1064
- }
1065
- if (error) {
1066
- (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
1067
- type: 'builder.evaluateError',
1068
- data: { id: id_1, error: error.message },
1069
- }, '*');
1070
- }
1071
- else {
1072
- if (result && typeof result.then === 'function') {
1073
- result
1074
- .then(function (finalResult) {
1075
- var _a;
1076
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1077
- type: 'builder.evaluateResult',
1078
- data: { id: id_1, result: finalResult },
1079
- }, '*');
1080
- })
1081
- .catch(console.error);
977
+ }
978
+ var hasComponent_1 = !!info_1.component;
979
+ Builder.editors.every(function (thisInfo, index) {
980
+ if (info_1.name === thisInfo.name) {
981
+ if (thisInfo.component && !hasComponent_1) {
982
+ return false;
1082
983
  }
1083
984
  else {
1084
- (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
985
+ Builder.editors[index] = thisInfo;
986
+ }
987
+ return false;
988
+ }
989
+ return true;
990
+ });
991
+ break;
992
+ }
993
+ case 'builder.triggerAnimation': {
994
+ Builder.animator.triggerAnimation(data.data);
995
+ break;
996
+ }
997
+ case 'builder.contentUpdate':
998
+ var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
999
+ var contentData = data.data.data; // hmmm...
1000
+ var observer = _this.observersByKey[key];
1001
+ if (observer && !_this.noEditorUpdates[key]) {
1002
+ observer.next([contentData]);
1003
+ }
1004
+ break;
1005
+ case 'builder.getComponents':
1006
+ (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1007
+ type: 'builder.components',
1008
+ data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1009
+ }, '*');
1010
+ break;
1011
+ case 'builder.editingModel':
1012
+ _this.editingModel = data.data.model;
1013
+ break;
1014
+ case 'builder.registerComponent':
1015
+ var componentData = data.data;
1016
+ Builder.addComponent(componentData);
1017
+ break;
1018
+ case 'builder.blockContentLoading':
1019
+ if (typeof data.data.model === 'string') {
1020
+ _this.blockContentLoading = data.data.model;
1021
+ }
1022
+ break;
1023
+ case 'builder.editingMode':
1024
+ var editingMode = data.data;
1025
+ if (editingMode) {
1026
+ _this.editingMode = true;
1027
+ document.body.classList.add('builder-editing');
1028
+ }
1029
+ else {
1030
+ _this.editingMode = false;
1031
+ document.body.classList.remove('builder-editing');
1032
+ }
1033
+ break;
1034
+ case 'builder.editingPageMode':
1035
+ var editingPageMode = data.data;
1036
+ Builder.editingPage = editingPageMode;
1037
+ break;
1038
+ case 'builder.overrideUserAttributes':
1039
+ var userAttributes = data.data;
1040
+ (0, assign_function_1.assign)(Builder.overrideUserAttributes, userAttributes);
1041
+ _this.flushGetContentQueue(true);
1042
+ // TODO: refetch too
1043
+ break;
1044
+ case 'builder.overrideTestGroup':
1045
+ var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
1046
+ if (variationId && contentId) {
1047
+ _this.setTestCookie(contentId, variationId);
1048
+ _this.flushGetContentQueue(true);
1049
+ }
1050
+ break;
1051
+ case 'builder.evaluate': {
1052
+ var text = data.data.text;
1053
+ var args = data.data.arguments || [];
1054
+ var id_1 = data.data.id;
1055
+ // tslint:disable-next-line:no-function-constructor-with-string-args
1056
+ var fn = new Function(text);
1057
+ var result = void 0;
1058
+ var error = null;
1059
+ try {
1060
+ result = fn.apply(_this, args);
1061
+ }
1062
+ catch (err) {
1063
+ error = (0, to_error_1.toError)(err);
1064
+ }
1065
+ if (error) {
1066
+ (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
1067
+ type: 'builder.evaluateError',
1068
+ data: { id: id_1, error: error.message },
1069
+ }, '*');
1070
+ }
1071
+ else {
1072
+ if (result && typeof result.then === 'function') {
1073
+ result
1074
+ .then(function (finalResult) {
1075
+ var _a;
1076
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1085
1077
  type: 'builder.evaluateResult',
1086
- data: { result: result, id: id_1 },
1078
+ data: { id: id_1, result: finalResult },
1087
1079
  }, '*');
1088
- }
1080
+ })
1081
+ .catch(console.error);
1082
+ }
1083
+ else {
1084
+ (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
1085
+ type: 'builder.evaluateResult',
1086
+ data: { result: result, id: id_1 },
1087
+ }, '*');
1089
1088
  }
1090
- break;
1091
1089
  }
1090
+ break;
1092
1091
  }
1093
1092
  }
1094
- });
1095
- }
1093
+ }
1094
+ });
1096
1095
  };
1097
1096
  Object.defineProperty(Builder.prototype, "defaultCanTrack", {
1098
1097
  get: function () {
@@ -1202,8 +1201,14 @@ var Builder = /** @class */ (function () {
1202
1201
  (0, assign_function_1.assign)(this.trackingUserAttributes, attributes);
1203
1202
  };
1204
1203
  Builder.prototype.get = function (modelName, options) {
1204
+ var _a;
1205
1205
  if (options === void 0) { options = {}; }
1206
1206
  var instance = this;
1207
+ var finalLocale = options.locale || ((_a = options.userAttributes) === null || _a === void 0 ? void 0 : _a.locale) || this.getUserAttributes().locale;
1208
+ var finalOptions = __assign(__assign({}, options), (finalLocale && {
1209
+ locale: String(finalLocale),
1210
+ userAttributes: __assign({ locale: String(finalLocale) }, options.userAttributes),
1211
+ }));
1207
1212
  if (!Builder.isBrowser) {
1208
1213
  instance = new Builder(options.apiKey || this.apiKey, options.req, options.res, undefined, options.authToken || this.authToken, options.apiVersion || this.apiVersion);
1209
1214
  instance.setUserAttributes(this.getUserAttributes());
@@ -1221,7 +1226,7 @@ var Builder = /** @class */ (function () {
1221
1226
  this.apiVersion = options.apiVersion;
1222
1227
  }
1223
1228
  }
1224
- return instance.queueGetContent(modelName, options).map(
1229
+ return instance.queueGetContent(modelName, finalOptions).map(
1225
1230
  /* map( */ function (matches) {
1226
1231
  var match = matches && matches[0];
1227
1232
  if (Builder.isStatic) {
@@ -1429,9 +1434,9 @@ var Builder = /** @class */ (function () {
1429
1434
  if (queue[0].sort) {
1430
1435
  queryParams.sort = queue[0].sort;
1431
1436
  }
1432
- var pageQueryParams = typeof location !== 'undefined'
1437
+ var pageQueryParams = (typeof location !== 'undefined'
1433
1438
  ? query_string_class_1.QueryString.parseDeep(location.search.substr(1))
1434
- : undefined || {}; // TODO: WHAT about SSR (this.request) ?
1439
+ : undefined) || {}; // TODO: WHAT about SSR (this.request) ?
1435
1440
  var userAttributes =
1436
1441
  // FIXME: HACK: only checks first in queue for user attributes overrides, should check all
1437
1442
  // TODO: merge user attributes provided here with defaults and current user attiributes (?)