@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.cjs.js CHANGED
@@ -129,7 +129,7 @@ function assertAllowedPropertyName(name) {
129
129
  throw new Error("Property name \"".concat(name, "\" is not allowed"));
130
130
  }
131
131
 
132
- var version = "1.1.27-4";
132
+ var version = "1.1.28-1";
133
133
 
134
134
  var Subscription = /** @class */ (function () {
135
135
  function Subscription(listeners, listener) {
@@ -456,13 +456,17 @@ function tinyFetch(url, options) {
456
456
  }
457
457
  });
458
458
  }
459
- var fetch = typeof global === 'object' && typeof global.fetch === 'function'
460
- ? global.fetch
461
- : typeof window === 'undefined'
462
- ? serverOnlyRequire$1('node-fetch')
463
- : typeof window.fetch !== 'undefined'
464
- ? window.fetch
465
- : tinyFetch;
459
+ var fetch;
460
+ // If fetch is defined, in the browser, via polyfill, or in a Cloudflare worker, use it.
461
+ if (globalThis.fetch) {
462
+ fetch = globalThis.fetch;
463
+ }
464
+ // If fetch is not defined, in a Node.js environment, use node-fetch.
465
+ if (typeof window === 'undefined') {
466
+ fetch !== null && fetch !== void 0 ? fetch : (fetch = serverOnlyRequire$1('node-fetch'));
467
+ }
468
+ // Otherwise, use tiny-fetch.
469
+ fetch !== null && fetch !== void 0 ? fetch : (fetch = tinyFetch);
466
470
 
467
471
  function assign(target) {
468
472
  var args = [];
@@ -876,6 +880,70 @@ function uuid() {
876
880
  return uuidv4().replace(/-/g, '');
877
881
  }
878
882
 
883
+ function emptyUrl() {
884
+ return {
885
+ query: null,
886
+ port: null,
887
+ auth: null,
888
+ hash: null,
889
+ host: null,
890
+ hostname: null,
891
+ href: null,
892
+ path: null,
893
+ pathname: null,
894
+ protocol: null,
895
+ search: null,
896
+ slashes: null,
897
+ };
898
+ }
899
+ // Replacement for `url.parse` using `URL` global object that works with relative paths.
900
+ // Assumptions: this function operates in a NodeJS environment.
901
+ function parse(url) {
902
+ var out = emptyUrl();
903
+ var u;
904
+ var pathOnly = url === '' || url[0] === '/';
905
+ if (pathOnly) {
906
+ u = new URL(url, 'http://0.0.0.0/');
907
+ out.href = u.href;
908
+ out.href = out.href.slice(14); // remove 'http://0.0.0.0/'
909
+ }
910
+ else {
911
+ u = new URL(url);
912
+ out.href = u.href;
913
+ out.port = u.port === '' ? null : u.port;
914
+ out.hash = u.hash === '' ? null : u.hash;
915
+ out.host = u.host;
916
+ out.hostname = u.hostname;
917
+ out.href = u.href;
918
+ out.pathname = u.pathname;
919
+ out.protocol = u.protocol;
920
+ out.slashes = url[u.protocol.length] === '/'; // check if the mimetype is proceeded by a slash
921
+ }
922
+ out.search = u.search;
923
+ out.query = u.search.slice(1); // remove '?'
924
+ out.path = "".concat(u.pathname).concat(u.search);
925
+ out.pathname = u.pathname;
926
+ return out;
927
+ }
928
+
929
+ /**
930
+ * Safe conversion to error type. Intended to be used in catch blocks where the
931
+ * value is not guaranteed to be an error.
932
+ *
933
+ * @example
934
+ * try {
935
+ * throw new Error('Something went wrong')
936
+ * }
937
+ * catch (err: unknown) {
938
+ * const error: Error = toError(err)
939
+ * }
940
+ */
941
+ function toError(err) {
942
+ if (err instanceof Error)
943
+ return err;
944
+ return new Error(String(err));
945
+ }
946
+
879
947
  function datePlusMinutes(minutes) {
880
948
  if (minutes === void 0) { minutes = 30; }
881
949
  return new Date(Date.now() + minutes * 60000);
@@ -909,12 +977,24 @@ function getQueryParam(url, variable) {
909
977
  }
910
978
  var urlParser = {
911
979
  parse: function (url) {
912
- var parser = document.createElement('a');
913
- parser.href = url;
980
+ var el = document.createElement('a');
981
+ el.href = url;
914
982
  var out = {};
915
- var props = 'username password host hostname port protocol origin pathname search hash'.split(' ');
916
- for (var i = props.length; i--;) {
917
- out[props[i]] = parser[props[i]];
983
+ var props = [
984
+ 'username',
985
+ 'password',
986
+ 'host',
987
+ 'hostname',
988
+ 'port',
989
+ 'protocol',
990
+ 'origin',
991
+ 'pathname',
992
+ 'search',
993
+ 'hash',
994
+ ];
995
+ for (var _i = 0, props_1 = props; _i < props_1.length; _i++) {
996
+ var prop = props_1[_i];
997
+ out[prop] = el[prop];
918
998
  }
919
999
  // IE 11 pathname handling workaround
920
1000
  // (IE omits preceeding '/', unlike other browsers)
@@ -926,11 +1006,11 @@ var urlParser = {
926
1006
  return out;
927
1007
  },
928
1008
  };
929
- var parse = isReactNative
930
- ? function () { return ({}); }
1009
+ var parse$1 = isReactNative
1010
+ ? function () { return emptyUrl(); }
931
1011
  : typeof window === 'object'
932
1012
  ? urlParser.parse
933
- : serverOnlyRequire$1('url').parse;
1013
+ : parse;
934
1014
  function setCookie(name$$1, value, expires) {
935
1015
  try {
936
1016
  var expiresString = '';
@@ -1033,6 +1113,7 @@ var Builder = /** @class */ (function () {
1033
1113
  this.observersByKey = {};
1034
1114
  this.noEditorUpdates = {};
1035
1115
  this.overrides = {};
1116
+ this.queryOptions = {};
1036
1117
  this.getContentQueue = null;
1037
1118
  this.priorContentQueue = null;
1038
1119
  this.testCookiePrefix = 'builder.tests';
@@ -1644,10 +1725,14 @@ var Builder = /** @class */ (function () {
1644
1725
  var params = QueryString.parseDeep(this.modifySearch(location.search || '').substr(1));
1645
1726
  var builder = params.builder;
1646
1727
  if (builder) {
1647
- 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;
1728
+ 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;
1648
1729
  if (userAttributes) {
1649
1730
  this.setUserAttributes(userAttributes);
1650
1731
  }
1732
+ if (options) {
1733
+ // picking only locale and includeRefs
1734
+ this.queryOptions = tslib.__assign(tslib.__assign({}, (options.locale && { locale: options.locale })), (options.includeRefs && { includeRefs: options.includeRefs }));
1735
+ }
1651
1736
  if (overrides) {
1652
1737
  this.overrides = overrides;
1653
1738
  }
@@ -1688,7 +1773,7 @@ var Builder = /** @class */ (function () {
1688
1773
  if (isBrowser) {
1689
1774
  addEventListener('message', function (event) {
1690
1775
  var _a, _b, _c, _d, _e;
1691
- var url = parse(event.origin);
1776
+ var url = parse$1(event.origin);
1692
1777
  var isRestricted = ['builder.register', 'builder.registerComponent'].indexOf((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === -1;
1693
1778
  var isTrusted = url.hostname && Builder.isTrustedHost(url.hostname);
1694
1779
  if (isRestricted && !isTrusted) {
@@ -1830,7 +1915,7 @@ var Builder = /** @class */ (function () {
1830
1915
  result = fn.apply(_this, args);
1831
1916
  }
1832
1917
  catch (err) {
1833
- error = err;
1918
+ error = toError(err);
1834
1919
  }
1835
1920
  if (error) {
1836
1921
  (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
@@ -1900,14 +1985,15 @@ var Builder = /** @class */ (function () {
1900
1985
  });
1901
1986
  // TODO: allow adding location object as property and/or in constructor
1902
1987
  Builder.prototype.getLocation = function () {
1988
+ var _a;
1903
1989
  var parsedLocation = {};
1904
1990
  // in ssr mode
1905
1991
  if (this.request) {
1906
- parsedLocation = parse(this.request.url);
1992
+ parsedLocation = parse$1((_a = this.request.url) !== null && _a !== void 0 ? _a : '');
1907
1993
  }
1908
1994
  else if (typeof location === 'object') {
1909
1995
  // in the browser
1910
- parsedLocation = parse(location.href);
1996
+ parsedLocation = parse$1(location.href);
1911
1997
  }
1912
1998
  // IE11 bug with parsed path being empty string
1913
1999
  // causes issues with our user targeting
@@ -2079,47 +2165,6 @@ var Builder = /** @class */ (function () {
2079
2165
  }
2080
2166
  return observable;
2081
2167
  };
2082
- Builder.prototype.requestUrl = function (url, options) {
2083
- if (Builder.isBrowser) {
2084
- return fetch(url, options).then(function (res) { return res.json(); });
2085
- }
2086
- return new Promise(function (resolve, reject) {
2087
- var parsedUrl = parse(url);
2088
- var module = parsedUrl.protocol === 'http:' ? serverOnlyRequire$1('http') : serverOnlyRequire$1('https');
2089
- var requestOptions = {
2090
- host: parsedUrl.hostname,
2091
- port: parsedUrl.port,
2092
- path: parsedUrl.pathname + parsedUrl.search,
2093
- headers: tslib.__assign({}, options === null || options === void 0 ? void 0 : options.headers),
2094
- };
2095
- module
2096
- .get(requestOptions, function (resp) {
2097
- var data = '';
2098
- // We are collecting textual data
2099
- resp.setEncoding('utf8');
2100
- // A chunk of data has been recieved.
2101
- resp.on('data', function (chunk) {
2102
- data += chunk;
2103
- });
2104
- // The whole response has been received. Print out the result.
2105
- resp.on('end', function () {
2106
- try {
2107
- resolve(JSON.parse(data));
2108
- }
2109
- catch (err) {
2110
- if ((err === null || err === void 0 ? void 0 : err.name) === 'SyntaxError') {
2111
- var jsonParseError = new Error("[Builder.io] ERROR: invalid response.\nRequest: ".concat(JSON.stringify(requestOptions, null, 2), "\nResponse Data: ").concat(data, "\n"));
2112
- reject(jsonParseError);
2113
- }
2114
- reject(err);
2115
- }
2116
- });
2117
- })
2118
- .on('error', function (error) {
2119
- reject(error);
2120
- });
2121
- });
2122
- };
2123
2168
  Object.defineProperty(Builder.prototype, "host", {
2124
2169
  get: function () {
2125
2170
  switch (this.env) {
@@ -2159,9 +2204,9 @@ var Builder = /** @class */ (function () {
2159
2204
  var queue = useQueue || (usePastQueue ? this.priorContentQueue : this.getContentQueue) || [];
2160
2205
  // TODO: do this on every request send?
2161
2206
  this.getOverridesFromQueryString();
2162
- var queryParams = tslib.__assign({
2207
+ var queryParams = tslib.__assign(tslib.__assign({
2163
2208
  // TODO: way to force a request to be in a separate queue. or just lower queue limit to be 1 by default
2164
- omit: queue[0].omit || 'meta.componentsUsed', apiKey: this.apiKey }, queue[0].options);
2209
+ omit: queue[0].omit || 'meta.componentsUsed', apiKey: this.apiKey }, queue[0].options), this.queryOptions);
2165
2210
  if (queue[0].fields) {
2166
2211
  queryParams.fields = queue[0].fields;
2167
2212
  }
@@ -2281,7 +2326,12 @@ var Builder = /** @class */ (function () {
2281
2326
  if (this.authToken) {
2282
2327
  requestOptions.headers = tslib.__assign(tslib.__assign({}, requestOptions.headers), { Authorization: "Bearer ".concat(this.authToken) });
2283
2328
  }
2284
- 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) {
2329
+ var fn = format === 'solid' || format === 'react' ? 'codegen' : 'query';
2330
+ var url = "".concat(host, "/api/v1/").concat(fn, "/").concat(this.apiKey, "/").concat(keyNames) +
2331
+ (queryParams && hasParams ? "?".concat(queryStr) : '');
2332
+ var promise = fetch(url, requestOptions)
2333
+ .then(function (res) { return res.json(); })
2334
+ .then(function (result) {
2285
2335
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
2286
2336
  var options = queue_3[_i];
2287
2337
  var keyName = options.key;