@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder.io/sdk",
3
- "version": "3.0.6",
3
+ "version": "3.0.8",
4
4
  "unpkg": "./dist/index.browser.js",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.esm.js",
@@ -743,6 +743,7 @@ export declare class Builder {
743
743
  */
744
744
  static setServerContext(context: any): void;
745
745
  static isTrustedHost(hostname: string): boolean;
746
+ static isTrustedHostForEvent(event: MessageEvent): boolean | "" | null;
746
747
  static runAction(action: Action | string): void;
747
748
  static fields(name: string, fields: readonly Input[]): void;
748
749
  private static _editingPage;
@@ -772,6 +773,10 @@ export declare class Builder {
772
773
  private static addComponent;
773
774
  static component(info?: Partial<Component>): (component: Class) => Class;
774
775
  static isReact: boolean;
776
+ static sdkInfo: {
777
+ name: string;
778
+ version: string;
779
+ } | undefined;
775
780
  static get Component(): typeof Builder.component;
776
781
  private eventsQueue;
777
782
  private throttledClearEventsQueue;
@@ -890,6 +895,8 @@ export declare class Builder {
890
895
  next?: any;
891
896
  }): Promise<any>;
892
897
  get host(): string;
898
+ private getSdkHeaders;
899
+ private addSdkHeaders;
893
900
  private makeFetchApiCall;
894
901
  private flattenMongoQuery;
895
902
  private flushGetContentQueue;
@@ -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: {
@@ -360,11 +360,16 @@ var Builder = /** @class */ (function () {
360
360
  this.serverContext = context;
361
361
  };
362
362
  Builder.isTrustedHost = function (hostname) {
363
- return (this.trustedHosts.findIndex(function (trustedHost) {
363
+ var isTrusted = this.trustedHosts.findIndex(function (trustedHost) {
364
364
  return trustedHost.startsWith('*.')
365
365
  ? hostname.endsWith(trustedHost.slice(1))
366
366
  : trustedHost === hostname;
367
- }) > -1);
367
+ }) > -1;
368
+ return isTrusted;
369
+ };
370
+ Builder.isTrustedHostForEvent = function (event) {
371
+ var url = parse(event.origin);
372
+ return url.hostname && Builder.isTrustedHost(url.hostname);
368
373
  };
369
374
  Builder.runAction = function (action) {
370
375
  // TODO
@@ -523,9 +528,7 @@ var Builder = /** @class */ (function () {
523
528
  (0, fetch_function_1.getFetch)()("".concat(host, "/api/v1/track"), {
524
529
  method: 'POST',
525
530
  body: JSON.stringify({ events: events }),
526
- headers: {
527
- 'content-type': 'application/json',
528
- },
531
+ headers: __assign({ 'content-type': 'application/json' }, this.getSdkHeaders()),
529
532
  mode: 'cors',
530
533
  }).catch(function () {
531
534
  // Not the end of the world
@@ -914,184 +917,179 @@ var Builder = /** @class */ (function () {
914
917
  };
915
918
  Builder.prototype.bindMessageListeners = function () {
916
919
  var _this = this;
917
- if (exports.isBrowser) {
918
- addEventListener('message', function (event) {
919
- var _a, _b, _c, _d, _e;
920
- var url = parse(event.origin);
921
- var isRestricted = ['builder.register', 'builder.registerComponent'].indexOf((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === -1;
922
- var isTrusted = url.hostname && Builder.isTrustedHost(url.hostname);
923
- if (isRestricted && !isTrusted) {
924
- return;
925
- }
926
- var data = event.data;
927
- if (data) {
928
- switch (data.type) {
929
- case 'builder.ping': {
930
- (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
931
- type: 'builder.pong',
932
- data: {},
933
- }, '*');
920
+ addEventListener('message', function (event) {
921
+ var _a, _b, _c, _d;
922
+ var isTrusted = Builder.isTrustedHostForEvent(event);
923
+ if (!isTrusted)
924
+ return;
925
+ var data = event.data;
926
+ if (data) {
927
+ switch (data.type) {
928
+ case 'builder.ping': {
929
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
930
+ type: 'builder.pong',
931
+ data: {},
932
+ }, '*');
933
+ break;
934
+ }
935
+ case 'builder.register': {
936
+ // TODO: possibly do this for all...
937
+ if (event.source === window) {
934
938
  break;
935
939
  }
936
- case 'builder.register': {
937
- // TODO: possibly do this for all...
938
- if (event.source === window) {
939
- break;
940
- }
941
- var options = data.data;
942
- if (!options) {
943
- break;
944
- }
945
- var type = options.type, info = options.info;
946
- // TODO: all must have name and can't conflict?
947
- var typeList = Builder.registry[type];
948
- if (!typeList) {
949
- typeList = Builder.registry[type] = [];
950
- }
951
- typeList.push(info);
952
- Builder.registryChange.next(Builder.registry);
940
+ var options = data.data;
941
+ if (!options) {
953
942
  break;
954
943
  }
955
- case 'builder.settingsChange': {
956
- // TODO: possibly do this for all...
957
- if (event.source === window) {
958
- break;
959
- }
960
- var settings = data.data;
961
- if (!settings) {
962
- break;
963
- }
964
- Object.assign(Builder.settings, settings);
965
- Builder.settingsChange.next(Builder.settings);
944
+ var type = options.type, info = options.info;
945
+ // TODO: all must have name and can't conflict?
946
+ var typeList = Builder.registry[type];
947
+ if (!typeList) {
948
+ typeList = Builder.registry[type] = [];
949
+ }
950
+ typeList.push(info);
951
+ Builder.registryChange.next(Builder.registry);
952
+ break;
953
+ }
954
+ case 'builder.settingsChange': {
955
+ // TODO: possibly do this for all...
956
+ if (event.source === window) {
966
957
  break;
967
958
  }
968
- case 'builder.registerEditor': {
969
- // TODO: possibly do this for all...
970
- if (event.source === window) {
971
- break;
972
- }
973
- var info_1 = data.data;
974
- if (!info_1) {
975
- break;
976
- }
977
- var hasComponent_1 = !!info_1.component;
978
- Builder.editors.every(function (thisInfo, index) {
979
- if (info_1.name === thisInfo.name) {
980
- if (thisInfo.component && !hasComponent_1) {
981
- return false;
982
- }
983
- else {
984
- Builder.editors[index] = thisInfo;
985
- }
986
- return false;
987
- }
988
- return true;
989
- });
959
+ var settings = data.data;
960
+ if (!settings) {
990
961
  break;
991
962
  }
992
- case 'builder.triggerAnimation': {
993
- Builder.animator.triggerAnimation(data.data);
963
+ Object.assign(Builder.settings, settings);
964
+ Builder.settingsChange.next(Builder.settings);
965
+ break;
966
+ }
967
+ case 'builder.registerEditor': {
968
+ // TODO: possibly do this for all...
969
+ if (event.source === window) {
994
970
  break;
995
971
  }
996
- case 'builder.contentUpdate':
997
- var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
998
- var contentData = data.data.data; // hmmm...
999
- var observer = _this.observersByKey[key];
1000
- if (observer && !_this.noEditorUpdates[key]) {
1001
- observer.next([contentData]);
1002
- }
972
+ var info_1 = data.data;
973
+ if (!info_1) {
1003
974
  break;
1004
- case 'builder.getComponents':
975
+ }
976
+ var hasComponent_1 = !!info_1.component;
977
+ Builder.editors.every(function (thisInfo, index) {
978
+ if (info_1.name === thisInfo.name) {
979
+ if (thisInfo.component && !hasComponent_1) {
980
+ return false;
981
+ }
982
+ else {
983
+ Builder.editors[index] = thisInfo;
984
+ }
985
+ return false;
986
+ }
987
+ return true;
988
+ });
989
+ break;
990
+ }
991
+ case 'builder.triggerAnimation': {
992
+ Builder.animator.triggerAnimation(data.data);
993
+ break;
994
+ }
995
+ case 'builder.contentUpdate':
996
+ var key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
997
+ var contentData = data.data.data; // hmmm...
998
+ var observer = _this.observersByKey[key];
999
+ if (observer && !_this.noEditorUpdates[key]) {
1000
+ observer.next([contentData]);
1001
+ }
1002
+ break;
1003
+ case 'builder.getComponents':
1004
+ (_b = window.parent) === null || _b === void 0 ? void 0 : _b.postMessage({
1005
+ type: 'builder.components',
1006
+ data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1007
+ }, '*');
1008
+ break;
1009
+ case 'builder.editingModel':
1010
+ _this.editingModel = data.data.model;
1011
+ break;
1012
+ case 'builder.registerComponent':
1013
+ var componentData = data.data;
1014
+ Builder.addComponent(componentData);
1015
+ break;
1016
+ case 'builder.blockContentLoading':
1017
+ if (typeof data.data.model === 'string') {
1018
+ _this.blockContentLoading = data.data.model;
1019
+ }
1020
+ break;
1021
+ case 'builder.editingMode':
1022
+ var editingMode = data.data;
1023
+ if (editingMode) {
1024
+ _this.editingMode = true;
1025
+ document.body.classList.add('builder-editing');
1026
+ }
1027
+ else {
1028
+ _this.editingMode = false;
1029
+ document.body.classList.remove('builder-editing');
1030
+ }
1031
+ break;
1032
+ case 'builder.editingPageMode':
1033
+ var editingPageMode = data.data;
1034
+ Builder.editingPage = editingPageMode;
1035
+ break;
1036
+ case 'builder.overrideUserAttributes':
1037
+ var userAttributes = data.data;
1038
+ (0, assign_function_1.assign)(Builder.overrideUserAttributes, userAttributes);
1039
+ _this.flushGetContentQueue(true);
1040
+ // TODO: refetch too
1041
+ break;
1042
+ case 'builder.overrideTestGroup':
1043
+ var _e = data.data, variationId = _e.variationId, contentId = _e.contentId;
1044
+ if (variationId && contentId) {
1045
+ _this.setTestCookie(contentId, variationId);
1046
+ _this.flushGetContentQueue(true);
1047
+ }
1048
+ break;
1049
+ case 'builder.evaluate': {
1050
+ var text = data.data.text;
1051
+ var args = data.data.arguments || [];
1052
+ var id_1 = data.data.id;
1053
+ // tslint:disable-next-line:no-function-constructor-with-string-args
1054
+ var fn = new Function(text);
1055
+ var result = void 0;
1056
+ var error = null;
1057
+ try {
1058
+ result = fn.apply(_this, args);
1059
+ }
1060
+ catch (err) {
1061
+ error = (0, to_error_1.toError)(err);
1062
+ }
1063
+ if (error) {
1005
1064
  (_c = window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({
1006
- type: 'builder.components',
1007
- data: Builder.components.map(function (item) { return Builder.prepareComponentSpecToSend(item); }),
1065
+ type: 'builder.evaluateError',
1066
+ data: { id: id_1, error: error.message },
1008
1067
  }, '*');
1009
- break;
1010
- case 'builder.editingModel':
1011
- _this.editingModel = data.data.model;
1012
- break;
1013
- case 'builder.registerComponent':
1014
- var componentData = data.data;
1015
- Builder.addComponent(componentData);
1016
- break;
1017
- case 'builder.blockContentLoading':
1018
- if (typeof data.data.model === 'string') {
1019
- _this.blockContentLoading = data.data.model;
1020
- }
1021
- break;
1022
- case 'builder.editingMode':
1023
- var editingMode = data.data;
1024
- if (editingMode) {
1025
- _this.editingMode = true;
1026
- document.body.classList.add('builder-editing');
1068
+ }
1069
+ else {
1070
+ if (result && typeof result.then === 'function') {
1071
+ result
1072
+ .then(function (finalResult) {
1073
+ var _a;
1074
+ (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1075
+ type: 'builder.evaluateResult',
1076
+ data: { id: id_1, result: finalResult },
1077
+ }, '*');
1078
+ })
1079
+ .catch(console.error);
1027
1080
  }
1028
1081
  else {
1029
- _this.editingMode = false;
1030
- document.body.classList.remove('builder-editing');
1031
- }
1032
- break;
1033
- case 'builder.editingPageMode':
1034
- var editingPageMode = data.data;
1035
- Builder.editingPage = editingPageMode;
1036
- break;
1037
- case 'builder.overrideUserAttributes':
1038
- var userAttributes = data.data;
1039
- (0, assign_function_1.assign)(Builder.overrideUserAttributes, userAttributes);
1040
- _this.flushGetContentQueue(true);
1041
- // TODO: refetch too
1042
- break;
1043
- case 'builder.overrideTestGroup':
1044
- var _f = data.data, variationId = _f.variationId, contentId = _f.contentId;
1045
- if (variationId && contentId) {
1046
- _this.setTestCookie(contentId, variationId);
1047
- _this.flushGetContentQueue(true);
1048
- }
1049
- break;
1050
- case 'builder.evaluate': {
1051
- var text = data.data.text;
1052
- var args = data.data.arguments || [];
1053
- var id_1 = data.data.id;
1054
- // tslint:disable-next-line:no-function-constructor-with-string-args
1055
- var fn = new Function(text);
1056
- var result = void 0;
1057
- var error = null;
1058
- try {
1059
- result = fn.apply(_this, args);
1060
- }
1061
- catch (err) {
1062
- error = (0, to_error_1.toError)(err);
1063
- }
1064
- if (error) {
1065
1082
  (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
1066
- type: 'builder.evaluateError',
1067
- data: { id: id_1, error: error.message },
1083
+ type: 'builder.evaluateResult',
1084
+ data: { result: result, id: id_1 },
1068
1085
  }, '*');
1069
1086
  }
1070
- else {
1071
- if (result && typeof result.then === 'function') {
1072
- result
1073
- .then(function (finalResult) {
1074
- var _a;
1075
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({
1076
- type: 'builder.evaluateResult',
1077
- data: { id: id_1, result: finalResult },
1078
- }, '*');
1079
- })
1080
- .catch(console.error);
1081
- }
1082
- else {
1083
- (_e = window.parent) === null || _e === void 0 ? void 0 : _e.postMessage({
1084
- type: 'builder.evaluateResult',
1085
- data: { result: result, id: id_1 },
1086
- }, '*');
1087
- }
1088
- }
1089
- break;
1090
1087
  }
1088
+ break;
1091
1089
  }
1092
1090
  }
1093
- });
1094
- }
1091
+ }
1092
+ });
1095
1093
  };
1096
1094
  Object.defineProperty(Builder.prototype, "defaultCanTrack", {
1097
1095
  get: function () {
@@ -1324,7 +1322,7 @@ var Builder = /** @class */ (function () {
1324
1322
  // even though we only use `fetch()` now, we prefer to keep the old behavior and use the `fetch` that comes from
1325
1323
  // the core SDK for consistency
1326
1324
  Builder.prototype.requestUrl = function (url, options) {
1327
- return (0, fetch_function_1.getFetch)()(url, options).then(function (res) { return res.json(); });
1325
+ return (0, fetch_function_1.getFetch)()(url, this.addSdkHeaders(options)).then(function (res) { return res.json(); });
1328
1326
  };
1329
1327
  Object.defineProperty(Builder.prototype, "host", {
1330
1328
  get: function () {
@@ -1353,8 +1351,21 @@ var Builder = /** @class */ (function () {
1353
1351
  enumerable: false,
1354
1352
  configurable: true
1355
1353
  });
1354
+ Builder.prototype.getSdkHeaders = function () {
1355
+ if (!Builder.sdkInfo) {
1356
+ return {};
1357
+ }
1358
+ return {
1359
+ 'X-Builder-SDK': Builder.sdkInfo.name,
1360
+ 'X-Builder-SDK-GEN': '1',
1361
+ 'X-Builder-SDK-Version': Builder.sdkInfo.version,
1362
+ };
1363
+ };
1364
+ Builder.prototype.addSdkHeaders = function (fetchOptions) {
1365
+ return __assign(__assign({}, fetchOptions), { headers: __assign(__assign({}, fetchOptions.headers), this.getSdkHeaders()) });
1366
+ };
1356
1367
  Builder.prototype.makeFetchApiCall = function (url, requestOptions) {
1357
- return (0, fetch_function_1.getFetch)()(url, requestOptions);
1368
+ return (0, fetch_function_1.getFetch)()(url, this.addSdkHeaders(requestOptions));
1358
1369
  };
1359
1370
  Builder.prototype.flattenMongoQuery = function (obj, _current, _res) {
1360
1371
  if (_res === void 0) { _res = {}; }
@@ -1753,6 +1764,7 @@ var Builder = /** @class */ (function () {
1753
1764
  (location.search.indexOf('builder.preview=') !== -1 ||
1754
1765
  location.search.indexOf('builder.frameEditing=') !== -1));
1755
1766
  Builder.isReact = false;
1767
+ Builder.sdkInfo = undefined;
1756
1768
  Builder.overrideUserAttributes = {};
1757
1769
  return Builder;
1758
1770
  }());