@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.esm.js CHANGED
@@ -123,7 +123,7 @@ function assertAllowedPropertyName(name) {
123
123
  throw new Error("Property name \"".concat(name, "\" is not allowed"));
124
124
  }
125
125
 
126
- var version = "1.1.27-4";
126
+ var version = "1.1.28-1";
127
127
 
128
128
  var Subscription = /** @class */ (function () {
129
129
  function Subscription(listeners, listener) {
@@ -450,13 +450,17 @@ function tinyFetch(url, options) {
450
450
  }
451
451
  });
452
452
  }
453
- var fetch = typeof global === 'object' && typeof global.fetch === 'function'
454
- ? global.fetch
455
- : typeof window === 'undefined'
456
- ? serverOnlyRequire$1('node-fetch')
457
- : typeof window.fetch !== 'undefined'
458
- ? window.fetch
459
- : tinyFetch;
453
+ var fetch;
454
+ // If fetch is defined, in the browser, via polyfill, or in a Cloudflare worker, use it.
455
+ if (globalThis.fetch) {
456
+ fetch = globalThis.fetch;
457
+ }
458
+ // If fetch is not defined, in a Node.js environment, use node-fetch.
459
+ if (typeof window === 'undefined') {
460
+ fetch !== null && fetch !== void 0 ? fetch : (fetch = serverOnlyRequire$1('node-fetch'));
461
+ }
462
+ // Otherwise, use tiny-fetch.
463
+ fetch !== null && fetch !== void 0 ? fetch : (fetch = tinyFetch);
460
464
 
461
465
  function assign(target) {
462
466
  var args = [];
@@ -870,6 +874,70 @@ function uuid() {
870
874
  return uuidv4().replace(/-/g, '');
871
875
  }
872
876
 
877
+ function emptyUrl() {
878
+ return {
879
+ query: null,
880
+ port: null,
881
+ auth: null,
882
+ hash: null,
883
+ host: null,
884
+ hostname: null,
885
+ href: null,
886
+ path: null,
887
+ pathname: null,
888
+ protocol: null,
889
+ search: null,
890
+ slashes: null,
891
+ };
892
+ }
893
+ // Replacement for `url.parse` using `URL` global object that works with relative paths.
894
+ // Assumptions: this function operates in a NodeJS environment.
895
+ function parse(url) {
896
+ var out = emptyUrl();
897
+ var u;
898
+ var pathOnly = url === '' || url[0] === '/';
899
+ if (pathOnly) {
900
+ u = new URL(url, 'http://0.0.0.0/');
901
+ out.href = u.href;
902
+ out.href = out.href.slice(14); // remove 'http://0.0.0.0/'
903
+ }
904
+ else {
905
+ u = new URL(url);
906
+ out.href = u.href;
907
+ out.port = u.port === '' ? null : u.port;
908
+ out.hash = u.hash === '' ? null : u.hash;
909
+ out.host = u.host;
910
+ out.hostname = u.hostname;
911
+ out.href = u.href;
912
+ out.pathname = u.pathname;
913
+ out.protocol = u.protocol;
914
+ out.slashes = url[u.protocol.length] === '/'; // check if the mimetype is proceeded by a slash
915
+ }
916
+ out.search = u.search;
917
+ out.query = u.search.slice(1); // remove '?'
918
+ out.path = "".concat(u.pathname).concat(u.search);
919
+ out.pathname = u.pathname;
920
+ return out;
921
+ }
922
+
923
+ /**
924
+ * Safe conversion to error type. Intended to be used in catch blocks where the
925
+ * value is not guaranteed to be an error.
926
+ *
927
+ * @example
928
+ * try {
929
+ * throw new Error('Something went wrong')
930
+ * }
931
+ * catch (err: unknown) {
932
+ * const error: Error = toError(err)
933
+ * }
934
+ */
935
+ function toError(err) {
936
+ if (err instanceof Error)
937
+ return err;
938
+ return new Error(String(err));
939
+ }
940
+
873
941
  function datePlusMinutes(minutes) {
874
942
  if (minutes === void 0) { minutes = 30; }
875
943
  return new Date(Date.now() + minutes * 60000);
@@ -903,12 +971,24 @@ function getQueryParam(url, variable) {
903
971
  }
904
972
  var urlParser = {
905
973
  parse: function (url) {
906
- var parser = document.createElement('a');
907
- parser.href = url;
974
+ var el = document.createElement('a');
975
+ el.href = url;
908
976
  var out = {};
909
- var props = 'username password host hostname port protocol origin pathname search hash'.split(' ');
910
- for (var i = props.length; i--;) {
911
- out[props[i]] = parser[props[i]];
977
+ var props = [
978
+ 'username',
979
+ 'password',
980
+ 'host',
981
+ 'hostname',
982
+ 'port',
983
+ 'protocol',
984
+ 'origin',
985
+ 'pathname',
986
+ 'search',
987
+ 'hash',
988
+ ];
989
+ for (var _i = 0, props_1 = props; _i < props_1.length; _i++) {
990
+ var prop = props_1[_i];
991
+ out[prop] = el[prop];
912
992
  }
913
993
  // IE 11 pathname handling workaround
914
994
  // (IE omits preceeding '/', unlike other browsers)
@@ -920,11 +1000,11 @@ var urlParser = {
920
1000
  return out;
921
1001
  },
922
1002
  };
923
- var parse = isReactNative
924
- ? function () { return ({}); }
1003
+ var parse$1 = isReactNative
1004
+ ? function () { return emptyUrl(); }
925
1005
  : typeof window === 'object'
926
1006
  ? urlParser.parse
927
- : serverOnlyRequire$1('url').parse;
1007
+ : parse;
928
1008
  function setCookie(name$$1, value, expires) {
929
1009
  try {
930
1010
  var expiresString = '';
@@ -1027,6 +1107,7 @@ var Builder = /** @class */ (function () {
1027
1107
  this.observersByKey = {};
1028
1108
  this.noEditorUpdates = {};
1029
1109
  this.overrides = {};
1110
+ this.queryOptions = {};
1030
1111
  this.getContentQueue = null;
1031
1112
  this.priorContentQueue = null;
1032
1113
  this.testCookiePrefix = 'builder.tests';
@@ -1638,10 +1719,14 @@ var Builder = /** @class */ (function () {
1638
1719
  var params = QueryString.parseDeep(this.modifySearch(location.search || '').substr(1));
1639
1720
  var builder = params.builder;
1640
1721
  if (builder) {
1641
- 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;
1722
+ 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;
1642
1723
  if (userAttributes) {
1643
1724
  this.setUserAttributes(userAttributes);
1644
1725
  }
1726
+ if (options) {
1727
+ // picking only locale and includeRefs
1728
+ this.queryOptions = __assign(__assign({}, (options.locale && { locale: options.locale })), (options.includeRefs && { includeRefs: options.includeRefs }));
1729
+ }
1645
1730
  if (overrides) {
1646
1731
  this.overrides = overrides;
1647
1732
  }
@@ -1682,7 +1767,7 @@ var Builder = /** @class */ (function () {
1682
1767
  if (isBrowser) {
1683
1768
  addEventListener('message', function (event) {
1684
1769
  var _a, _b, _c, _d, _e;
1685
- var url = parse(event.origin);
1770
+ var url = parse$1(event.origin);
1686
1771
  var isRestricted = ['builder.register', 'builder.registerComponent'].indexOf((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === -1;
1687
1772
  var isTrusted = url.hostname && Builder.isTrustedHost(url.hostname);
1688
1773
  if (isRestricted && !isTrusted) {
@@ -1824,7 +1909,7 @@ var Builder = /** @class */ (function () {
1824
1909
  result = fn.apply(_this, args);
1825
1910
  }
1826
1911
  catch (err) {
1827
- error = err;
1912
+ error = toError(err);
1828
1913
  }
1829
1914
  if (error) {
1830
1915
  (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
@@ -1894,14 +1979,15 @@ var Builder = /** @class */ (function () {
1894
1979
  });
1895
1980
  // TODO: allow adding location object as property and/or in constructor
1896
1981
  Builder.prototype.getLocation = function () {
1982
+ var _a;
1897
1983
  var parsedLocation = {};
1898
1984
  // in ssr mode
1899
1985
  if (this.request) {
1900
- parsedLocation = parse(this.request.url);
1986
+ parsedLocation = parse$1((_a = this.request.url) !== null && _a !== void 0 ? _a : '');
1901
1987
  }
1902
1988
  else if (typeof location === 'object') {
1903
1989
  // in the browser
1904
- parsedLocation = parse(location.href);
1990
+ parsedLocation = parse$1(location.href);
1905
1991
  }
1906
1992
  // IE11 bug with parsed path being empty string
1907
1993
  // causes issues with our user targeting
@@ -2073,47 +2159,6 @@ var Builder = /** @class */ (function () {
2073
2159
  }
2074
2160
  return observable;
2075
2161
  };
2076
- Builder.prototype.requestUrl = function (url, options) {
2077
- if (Builder.isBrowser) {
2078
- return fetch(url, options).then(function (res) { return res.json(); });
2079
- }
2080
- return new Promise(function (resolve, reject) {
2081
- var parsedUrl = parse(url);
2082
- var module = parsedUrl.protocol === 'http:' ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2083
- var requestOptions = {
2084
- host: parsedUrl.hostname,
2085
- port: parsedUrl.port,
2086
- path: parsedUrl.pathname + parsedUrl.search,
2087
- headers: __assign({}, options === null || options === void 0 ? void 0 : options.headers),
2088
- };
2089
- module
2090
- .get(requestOptions, function (resp) {
2091
- var data = '';
2092
- // We are collecting textual data
2093
- resp.setEncoding('utf8');
2094
- // A chunk of data has been recieved.
2095
- resp.on('data', function (chunk) {
2096
- data += chunk;
2097
- });
2098
- // The whole response has been received. Print out the result.
2099
- resp.on('end', function () {
2100
- try {
2101
- resolve(JSON.parse(data));
2102
- }
2103
- catch (err) {
2104
- if ((err === null || err === void 0 ? void 0 : err.name) === 'SyntaxError') {
2105
- var jsonParseError = new Error("[Builder.io] ERROR: invalid response.\nRequest: ".concat(JSON.stringify(requestOptions, null, 2), "\nResponse Data: ").concat(data, "\n"));
2106
- reject(jsonParseError);
2107
- }
2108
- reject(err);
2109
- }
2110
- });
2111
- })
2112
- .on('error', function (error) {
2113
- reject(error);
2114
- });
2115
- });
2116
- };
2117
2162
  Object.defineProperty(Builder.prototype, "host", {
2118
2163
  get: function () {
2119
2164
  switch (this.env) {
@@ -2153,9 +2198,9 @@ var Builder = /** @class */ (function () {
2153
2198
  var queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];
2154
2199
  // TODO: do this on every request send?
2155
2200
  this.getOverridesFromQueryString();
2156
- var queryParams = __assign({
2201
+ var queryParams = __assign(__assign({
2157
2202
  // TODO: way to force a request to be in a separate queue. or just lower queue limit to be 1 by default
2158
- omit: queue[0].omit || 'meta.componentsUsed', apiKey: this.apiKey }, queue[0].options);
2203
+ omit: queue[0].omit || 'meta.componentsUsed', apiKey: this.apiKey }, queue[0].options), this.queryOptions);
2159
2204
  if (queue[0].fields) {
2160
2205
  queryParams.fields = queue[0].fields;
2161
2206
  }
@@ -2275,7 +2320,12 @@ var Builder = /** @class */ (function () {
2275
2320
  if (this.authToken) {
2276
2321
  requestOptions.headers = __assign(__assign({}, requestOptions.headers), { Authorization: "Bearer ".concat(this.authToken) });
2277
2322
  }
2278
- 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) {
2323
+ var fn = format === 'solid' || format === 'react' ? 'codegen' : 'query';
2324
+ var url = "".concat(host, "/api/v1/").concat(fn, "/").concat(this.apiKey, "/").concat(keyNames) +
2325
+ (queryParams && hasParams ? "?".concat(queryStr) : '');
2326
+ var promise = fetch(url, requestOptions)
2327
+ .then(function (res) { return res.json(); })
2328
+ .then(function (result) {
2279
2329
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
2280
2330
  var options = queue_3[_i];
2281
2331
  var keyName = options.key;