@builder.io/sdk 1.1.27 → 1.1.28-6

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.
Files changed (44) hide show
  1. package/.envrc +1 -0
  2. package/.task/checksum/build +1 -0
  3. package/CHANGELOG.md +1 -1
  4. package/Taskfile.yaml +15 -0
  5. package/coverage/clover.xml +6 -0
  6. package/coverage/coverage-final.json +1 -0
  7. package/coverage/lcov-report/base.css +224 -0
  8. package/coverage/lcov-report/block-navigation.js +87 -0
  9. package/coverage/lcov-report/favicon.png +0 -0
  10. package/coverage/lcov-report/index.html +101 -0
  11. package/coverage/lcov-report/prettify.css +1 -0
  12. package/coverage/lcov-report/prettify.js +2 -0
  13. package/coverage/lcov-report/query-string.class.ts.html +328 -0
  14. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/coverage/lcov-report/sorter.js +196 -0
  16. package/coverage/lcov-report/url.ts.html +253 -0
  17. package/coverage/lcov.info +0 -0
  18. package/dist/index.browser.js +1 -1
  19. package/dist/index.browser.js.map +1 -1
  20. package/dist/index.cjs.js +107 -62
  21. package/dist/index.cjs.js.map +1 -1
  22. package/dist/index.esm.js +107 -62
  23. package/dist/index.esm.js.map +1 -1
  24. package/dist/index.umd.js +107 -62
  25. package/dist/index.umd.js.map +1 -1
  26. package/dist/package.json +1 -1
  27. package/dist/src/builder.class.d.ts +0 -5
  28. package/dist/src/builder.class.js +31 -52
  29. package/dist/src/builder.class.js.map +1 -1
  30. package/dist/src/functions/fetch.function.d.ts +2 -2
  31. package/dist/src/functions/fetch.function.js +10 -7
  32. package/dist/src/functions/fetch.function.js.map +1 -1
  33. package/dist/src/functions/to-error.d.ts +13 -0
  34. package/dist/src/functions/to-error.js +22 -0
  35. package/dist/src/functions/to-error.js.map +1 -0
  36. package/dist/src/url.d.ts +9 -0
  37. package/dist/src/url.js +51 -0
  38. package/dist/src/url.js.map +1 -0
  39. package/dist/src/url.test.d.ts +1 -0
  40. package/dist/src/url.test.js +129 -0
  41. package/dist/src/url.test.js.map +1 -0
  42. package/dist/tsconfig.tsbuildinfo +1 -0
  43. package/package.json +1 -1
  44. package/tsconfig.json +15 -3
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-5";
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 = '';
@@ -1688,7 +1768,7 @@ var Builder = /** @class */ (function () {
1688
1768
  if (isBrowser) {
1689
1769
  addEventListener('message', function (event) {
1690
1770
  var _a, _b, _c, _d, _e;
1691
- var url = parse(event.origin);
1771
+ var url = parse$1(event.origin);
1692
1772
  var isRestricted = ['builder.register', 'builder.registerComponent'].indexOf((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) === -1;
1693
1773
  var isTrusted = url.hostname && Builder.isTrustedHost(url.hostname);
1694
1774
  if (isRestricted && !isTrusted) {
@@ -1830,7 +1910,7 @@ var Builder = /** @class */ (function () {
1830
1910
  result = fn.apply(_this, args);
1831
1911
  }
1832
1912
  catch (err) {
1833
- error = err;
1913
+ error = toError(err);
1834
1914
  }
1835
1915
  if (error) {
1836
1916
  (_d = window.parent) === null || _d === void 0 ? void 0 : _d.postMessage({
@@ -1900,14 +1980,15 @@ var Builder = /** @class */ (function () {
1900
1980
  });
1901
1981
  // TODO: allow adding location object as property and/or in constructor
1902
1982
  Builder.prototype.getLocation = function () {
1983
+ var _a;
1903
1984
  var parsedLocation = {};
1904
1985
  // in ssr mode
1905
1986
  if (this.request) {
1906
- parsedLocation = parse(this.request.url);
1987
+ parsedLocation = parse$1((_a = this.request.url) !== null && _a !== void 0 ? _a : '');
1907
1988
  }
1908
1989
  else if (typeof location === 'object') {
1909
1990
  // in the browser
1910
- parsedLocation = parse(location.href);
1991
+ parsedLocation = parse$1(location.href);
1911
1992
  }
1912
1993
  // IE11 bug with parsed path being empty string
1913
1994
  // causes issues with our user targeting
@@ -2079,47 +2160,6 @@ var Builder = /** @class */ (function () {
2079
2160
  }
2080
2161
  return observable;
2081
2162
  };
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
2163
  Object.defineProperty(Builder.prototype, "host", {
2124
2164
  get: function () {
2125
2165
  switch (this.env) {
@@ -2281,7 +2321,12 @@ var Builder = /** @class */ (function () {
2281
2321
  if (this.authToken) {
2282
2322
  requestOptions.headers = tslib.__assign(tslib.__assign({}, requestOptions.headers), { Authorization: "Bearer ".concat(this.authToken) });
2283
2323
  }
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) {
2324
+ var fn = format === 'solid' || format === 'react' ? 'codegen' : 'query';
2325
+ var url = "".concat(host, "/api/v1/").concat(fn, "/").concat(this.apiKey, "/").concat(keyNames) +
2326
+ (queryParams && hasParams ? "?".concat(queryStr) : '');
2327
+ var promise = fetch(url, requestOptions)
2328
+ .then(function (res) { return res.json(); })
2329
+ .then(function (result) {
2285
2330
  for (var _i = 0, queue_3 = queue; _i < queue_3.length; _i++) {
2286
2331
  var options = queue_3[_i];
2287
2332
  var keyName = options.key;