@builder.io/sdk 1.1.27 → 1.1.28-2

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.umd.js CHANGED
@@ -152,7 +152,7 @@
152
152
  throw new Error("Property name \"".concat(name, "\" is not allowed"));
153
153
  }
154
154
 
155
- var version = "1.1.27-4";
155
+ var version = "1.1.28-1";
156
156
 
157
157
  var Subscription = /** @class */ (function () {
158
158
  function Subscription(listeners, listener) {
@@ -479,13 +479,17 @@
479
479
  }
480
480
  });
481
481
  }
482
- var fetch = typeof global === 'object' && typeof global.fetch === 'function'
483
- ? global.fetch
484
- : typeof window === 'undefined'
485
- ? serverOnlyRequire$1('node-fetch')
486
- : typeof window.fetch !== 'undefined'
487
- ? window.fetch
488
- : tinyFetch;
482
+ var fetch;
483
+ // If fetch is defined, in the browser, via polyfill, or in a Cloudflare worker, use it.
484
+ if (globalThis.fetch) {
485
+ fetch = globalThis.fetch;
486
+ }
487
+ // If fetch is not defined, in a Node.js environment, use node-fetch.
488
+ if (typeof window === 'undefined') {
489
+ fetch !== null && fetch !== void 0 ? fetch : (fetch = serverOnlyRequire$1('node-fetch'));
490
+ }
491
+ // Otherwise, use tiny-fetch.
492
+ fetch !== null && fetch !== void 0 ? fetch : (fetch = tinyFetch);
489
493
 
490
494
  function assign(target) {
491
495
  var args = [];
@@ -899,6 +903,52 @@
899
903
  return uuidv4().replace(/-/g, '');
900
904
  }
901
905
 
906
+ function emptyUrl() {
907
+ return {
908
+ query: null,
909
+ port: null,
910
+ auth: null,
911
+ hash: null,
912
+ host: null,
913
+ hostname: null,
914
+ href: null,
915
+ path: null,
916
+ pathname: null,
917
+ protocol: null,
918
+ search: null,
919
+ slashes: null,
920
+ };
921
+ }
922
+ // Replacement for `url.parse` using `URL` global object that works with relative paths.
923
+ // Assumptions: this function operates in a NodeJS environment.
924
+ function parse(url) {
925
+ var out = emptyUrl();
926
+ var u;
927
+ var pathOnly = url === '' || url[0] === '/';
928
+ if (pathOnly) {
929
+ u = new URL(url, 'http://0.0.0.0/');
930
+ out.href = u.href;
931
+ out.href = out.href.slice(14); // remove 'http://0.0.0.0/'
932
+ }
933
+ else {
934
+ u = new URL(url);
935
+ out.href = u.href;
936
+ out.port = u.port === '' ? null : u.port;
937
+ out.hash = u.hash === '' ? null : u.hash;
938
+ out.host = u.host;
939
+ out.hostname = u.hostname;
940
+ out.href = u.href;
941
+ out.pathname = u.pathname;
942
+ out.protocol = u.protocol;
943
+ out.slashes = url[u.protocol.length] === '/'; // check if the mimetype is proceeded by a slash
944
+ }
945
+ out.search = u.search;
946
+ out.query = u.search.slice(1); // remove '?'
947
+ out.path = "".concat(u.pathname).concat(u.search);
948
+ out.pathname = u.pathname;
949
+ return out;
950
+ }
951
+
902
952
  function pad (hash, len) {
903
953
  while (hash.length < len) {
904
954
  hash = '0' + hash;
@@ -967,6 +1017,24 @@
967
1017
 
968
1018
  var hashSum = sum;
969
1019
 
1020
+ /**
1021
+ * Safe conversion to error type. Intended to be used in catch blocks where the
1022
+ * value is not guaranteed to be an error.
1023
+ *
1024
+ * @example
1025
+ * try {
1026
+ * throw new Error('Something went wrong')
1027
+ * }
1028
+ * catch (err: unknown) {
1029
+ * const error: Error = toError(err)
1030
+ * }
1031
+ */
1032
+ function toError(err) {
1033
+ if (err instanceof Error)
1034
+ return err;
1035
+ return new Error(String(err));
1036
+ }
1037
+
970
1038
  function datePlusMinutes(minutes) {
971
1039
  if (minutes === void 0) { minutes = 30; }
972
1040
  return new Date(Date.now() + minutes * 60000);
@@ -1000,12 +1068,24 @@
1000
1068
  }
1001
1069
  var urlParser = {
1002
1070
  parse: function (url) {
1003
- var parser = document.createElement('a');
1004
- parser.href = url;
1071
+ var el = document.createElement('a');
1072
+ el.href = url;
1005
1073
  var out = {};
1006
- var props = 'username password host hostname port protocol origin pathname search hash'.split(' ');
1007
- for (var i = props.length; i--;) {
1008
- out[props[i]] = parser[props[i]];
1074
+ var props = [
1075
+ 'username',
1076
+ 'password',
1077
+ 'host',
1078
+ 'hostname',
1079
+ 'port',
1080
+ 'protocol',
1081
+ 'origin',
1082
+ 'pathname',
1083
+ 'search',
1084
+ 'hash',
1085
+ ];
1086
+ for (var _i = 0, props_1 = props; _i < props_1.length; _i++) {
1087
+ var prop = props_1[_i];
1088
+ out[prop] = el[prop];
1009
1089
  }
1010
1090
  // IE 11 pathname handling workaround
1011
1091
  // (IE omits preceeding '/', unlike other browsers)
@@ -1017,11 +1097,11 @@
1017
1097
  return out;
1018
1098
  },
1019
1099
  };
1020
- var parse = isReactNative
1021
- ? function () { return ({}); }
1100
+ var parse$1 = isReactNative
1101
+ ? function () { return emptyUrl(); }
1022
1102
  : typeof window === 'object'
1023
1103
  ? urlParser.parse
1024
- : serverOnlyRequire$1('url').parse;
1104
+ : parse;
1025
1105
  function setCookie(name$$1, value, expires) {
1026
1106
  try {
1027
1107
  var expiresString = '';
@@ -1124,6 +1204,7 @@
1124
1204
  this.observersByKey = {};
1125
1205
  this.noEditorUpdates = {};
1126
1206
  this.overrides = {};
1207
+ this.queryOptions = {};
1127
1208
  this.getContentQueue = null;
1128
1209
  this.priorContentQueue = null;
1129
1210
  this.testCookiePrefix = 'builder.tests';
@@ -1735,10 +1816,14 @@
1735
1816
  var params = QueryString.parseDeep(this.modifySearch(location.search || '').substr(1));
1736
1817
  var builder = params.builder;
1737
1818
  if (builder) {
1738
- var userAttributes = builder.userAttributes, overrides = builder.overrides, env = builder.env, host = builder.host, api = builder.api, cachebust = builder.cachebust, noCache = builder.noCache, preview = builder.preview, editing = builder.editing, frameEditing = builder.frameEditing, overrideParams = builder.params;
1819
+ var userAttributes = builder.userAttributes, overrides = builder.overrides, env = builder.env, host = builder.host, api = builder.api, cachebust = builder.cachebust, noCache = builder.noCache, preview = builder.preview, editing = builder.editing, frameEditing = builder.frameEditing, options = builder.options, overrideParams = builder.params;
1739
1820
  if (userAttributes) {
1740
1821
  this.setUserAttributes(userAttributes);
1741
1822
  }
1823
+ if (options) {
1824
+ // picking only locale and includeRefs
1825
+ this.queryOptions = __assign(__assign({}, (options.locale && { locale: options.locale })), (options.includeRefs && { includeRefs: options.includeRefs }));
1826
+ }
1742
1827
  if (overrides) {
1743
1828
  this.overrides = overrides;
1744
1829
  }
@@ -1779,7 +1864,7 @@
1779
1864
  if (isBrowser) {
1780
1865
  addEventListener('message', function (event) {
1781
1866
  var _a, _b, _c, _d, _e;
1782
- var url = parse(event.origin);
1867
+ var url = parse$1(event.origin);
1783
1868
  var isRestricted = ['builder.register', 'builder.registerComponent'].indexOf((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === -1;
1784
1869
  var isTrusted = url.hostname && Builder.isTrustedHost(url.hostname);
1785
1870
  if (isRestricted && !isTrusted) {
@@ -1921,7 +2006,7 @@
1921
2006
  result = fn.apply(_this, args);
1922
2007
  }
1923
2008
  catch (err) {
1924
- error = err;
2009
+ error = toError(err);
1925
2010
  }
1926
2011
  if (error) {
1927
2012
  (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
@@ -1991,14 +2076,15 @@
1991
2076
  });
1992
2077
  // TODO: allow adding location object as property and/or in constructor
1993
2078
  Builder.prototype.getLocation = function () {
2079
+ var _a;
1994
2080
  var parsedLocation = {};
1995
2081
  // in ssr mode
1996
2082
  if (this.request) {
1997
- parsedLocation = parse(this.request.url);
2083
+ parsedLocation = parse$1((_a = this.request.url) !== null && _a !== void 0 ? _a : '');
1998
2084
  }
1999
2085
  else if (typeof location === 'object') {
2000
2086
  // in the browser
2001
- parsedLocation = parse(location.href);
2087
+ parsedLocation = parse$1(location.href);
2002
2088
  }
2003
2089
  // IE11 bug with parsed path being empty string
2004
2090
  // causes issues with our user targeting
@@ -2170,47 +2256,6 @@
2170
2256
  }
2171
2257
  return observable;
2172
2258
  };
2173
- Builder.prototype.requestUrl = function (url, options) {
2174
- if (Builder.isBrowser) {
2175
- return fetch(url, options).then(function (res) { return res.json(); });
2176
- }
2177
- return new Promise(function (resolve, reject) {
2178
- var parsedUrl = parse(url);
2179
- var module = parsedUrl.protocol === 'http:' ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2180
- var requestOptions = {
2181
- host: parsedUrl.hostname,
2182
- port: parsedUrl.port,
2183
- path: parsedUrl.pathname + parsedUrl.search,
2184
- headers: __assign({}, options === null || options === void 0 ? void 0 : options.headers),
2185
- };
2186
- module
2187
- .get(requestOptions, function (resp) {
2188
- var data = '';
2189
- // We are collecting textual data
2190
- resp.setEncoding('utf8');
2191
- // A chunk of data has been recieved.
2192
- resp.on('data', function (chunk) {
2193
- data += chunk;
2194
- });
2195
- // The whole response has been received. Print out the result.
2196
- resp.on('end', function () {
2197
- try {
2198
- resolve(JSON.parse(data));
2199
- }
2200
- catch (err) {
2201
- if ((err === null || err === void 0 ? void 0 : err.name) === 'SyntaxError') {
2202
- var jsonParseError = new Error("[Builder.io] ERROR: invalid response.\nRequest: ".concat(JSON.stringify(requestOptions, null, 2), "\nResponse Data: ").concat(data, "\n"));
2203
- reject(jsonParseError);
2204
- }
2205
- reject(err);
2206
- }
2207
- });
2208
- })
2209
- .on('error', function (error) {
2210
- reject(error);
2211
- });
2212
- });
2213
- };
2214
2259
  Object.defineProperty(Builder.prototype, "host", {
2215
2260
  get: function () {
2216
2261
  switch (this.env) {
@@ -2250,9 +2295,9 @@
2250
2295
  var queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];
2251
2296
  // TODO: do this on every request send?
2252
2297
  this.getOverridesFromQueryString();
2253
- var queryParams = __assign({
2298
+ var queryParams = __assign(__assign({
2254
2299
  // TODO: way to force a request to be in a separate queue. or just lower queue limit to be 1 by default
2255
- omit: queue[0].omit || 'meta.componentsUsed', apiKey: this.apiKey }, queue[0].options);
2300
+ omit: queue[0].omit || 'meta.componentsUsed', apiKey: this.apiKey }, queue[0].options), this.queryOptions);
2256
2301
  if (queue[0].fields) {
2257
2302
  queryParams.fields = queue[0].fields;
2258
2303
  }
@@ -2372,7 +2417,12 @@
2372
2417
  if (this.authToken) {
2373
2418
  requestOptions.headers = __assign(__assign({}, requestOptions.headers), { Authorization: "Bearer ".concat(this.authToken) });
2374
2419
  }
2375
- var promise = this.requestUrl("".concat(host, "/api/v1/").concat(format === 'solid' || format === 'react' ? 'codegen' : 'query', "/").concat(this.apiKey, "/").concat(keyNames) + (queryParams && hasParams ? "?".concat(queryStr) : ''), requestOptions).then(function (result) {
2420
+ var fn = format === 'solid' || format === 'react' ? 'codegen' : 'query';
2421
+ var url = "".concat(host, "/api/v1/").concat(fn, "/").concat(this.apiKey, "/").concat(keyNames) +
2422
+ (queryParams && hasParams ? "?".concat(queryStr) : '');
2423
+ var promise = fetch(url, requestOptions)
2424
+ .then(function (res) { return res.json(); })
2425
+ .then(function (result) {
2376
2426
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
2377
2427
  var options = queue_3[_i];
2378
2428
  var keyName = options.key;