@builder.io/sdk-solid 0.6.4 → 0.7.1-0

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/lib/edge/dev.js CHANGED
@@ -1,4 +1,4 @@
1
- import { delegateEvents, createComponent, spread, mergeProps, insert, effect, setAttribute, className, style, template, memo, Dynamic, use } from 'solid-js/web';
1
+ import { createComponent, spread, mergeProps, insert, effect, setAttribute, className, style, template, memo, Dynamic, use } from 'solid-js/web';
2
2
  import { createContext, Show, useContext, For, createSignal, onMount, createEffect, on } from 'solid-js';
3
3
  import { css } from 'solid-styled-components';
4
4
 
@@ -176,6 +176,75 @@ var getUserAttributes = () => {
176
176
  };
177
177
  };
178
178
 
179
+ // src/functions/evaluate/helpers.js
180
+ var getFunctionArguments = ({
181
+ builder,
182
+ context,
183
+ event,
184
+ state
185
+ }) => {
186
+ return Object.entries({
187
+ state,
188
+ Builder: builder,
189
+ builder,
190
+ context,
191
+ event
192
+ });
193
+ };
194
+ var getBuilderGlobals = () => ({
195
+ isEditing: isEditing(),
196
+ isBrowser: isBrowser(),
197
+ isServer: !isBrowser(),
198
+ getUserAttributes: () => getUserAttributes()
199
+ });
200
+ var parseCode = (code, {
201
+ isExpression = true
202
+ }) => {
203
+ const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
204
+ const useCode = useReturn ? `return (${code});` : code;
205
+ return useCode;
206
+ };
207
+
208
+ // src/functions/evaluate/browser-runtime/browser.js
209
+ var runInBrowser = ({
210
+ code,
211
+ builder,
212
+ context,
213
+ event,
214
+ localState,
215
+ rootSetState,
216
+ rootState
217
+ }) => {
218
+ const functionArgs = getFunctionArguments({
219
+ builder,
220
+ context,
221
+ event,
222
+ state: flattenState(rootState, localState, rootSetState)
223
+ });
224
+ return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
225
+ };
226
+ function flattenState(rootState, localState, rootSetState) {
227
+ if (rootState === localState) {
228
+ throw new Error("rootState === localState");
229
+ }
230
+ return new Proxy(rootState, {
231
+ get: (_, prop) => {
232
+ if (localState && prop in localState) {
233
+ return localState[prop];
234
+ }
235
+ return rootState[prop];
236
+ },
237
+ set: (_, prop, value) => {
238
+ if (localState && prop in localState) {
239
+ throw new Error("Writing to local state is not allowed as it is read-only.");
240
+ }
241
+ rootState[prop] = value;
242
+ rootSetState == null ? void 0 : rootSetState(rootState);
243
+ return true;
244
+ }
245
+ });
246
+ }
247
+
179
248
  // src/functions/set.js
180
249
  var set = (obj, _path, value) => {
181
250
  if (Object(obj) !== obj) {
@@ -3254,22 +3323,6 @@ t.prototype.setStateStack = t.prototype.ec;
3254
3323
  t.VALUE_IN_DESCRIPTOR = Ia;
3255
3324
  var stdin_default = t;
3256
3325
 
3257
- // src/functions/evaluate/helpers.js
3258
- var getFunctionArguments = ({
3259
- builder,
3260
- context,
3261
- event,
3262
- state
3263
- }) => {
3264
- return Object.entries({
3265
- state,
3266
- Builder: builder,
3267
- builder,
3268
- context,
3269
- event
3270
- });
3271
- };
3272
-
3273
3326
  // src/functions/evaluate/edge-runtime/edge-runtime.js
3274
3327
  var __defProp = Object.defineProperty;
3275
3328
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
@@ -3367,6 +3420,9 @@ theFunction();
3367
3420
  }
3368
3421
  };
3369
3422
 
3423
+ // src/functions/evaluate/choose-eval.js
3424
+ var chooseBrowserOrServerEval = (args) => isBrowser() ? runInBrowser(args) : runInEdge(args);
3425
+
3370
3426
  // src/functions/evaluate/evaluate.js
3371
3427
  function evaluate({
3372
3428
  code,
@@ -3381,17 +3437,11 @@ function evaluate({
3381
3437
  logger.warn("Skipping evaluation of empty code block.");
3382
3438
  return;
3383
3439
  }
3384
- const builder = {
3385
- isEditing: isEditing(),
3386
- isBrowser: isBrowser(),
3387
- isServer: !isBrowser(),
3388
- getUserAttributes: () => getUserAttributes()
3389
- };
3390
- const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
3391
- const useCode = useReturn ? `return (${code});` : code;
3392
3440
  const args = {
3393
- code: useCode,
3394
- builder,
3441
+ code: parseCode(code, {
3442
+ isExpression
3443
+ }),
3444
+ builder: getBuilderGlobals(),
3395
3445
  context,
3396
3446
  event,
3397
3447
  rootSetState,
@@ -3399,7 +3449,7 @@ function evaluate({
3399
3449
  localState
3400
3450
  };
3401
3451
  try {
3402
- return runInEdge(args);
3452
+ return chooseBrowserOrServerEval(args);
3403
3453
  } catch (e) {
3404
3454
  logger.error("Failed code evaluation: " + e.message, {
3405
3455
  code
@@ -5814,42 +5864,40 @@ var _tmpl$13 = /* @__PURE__ */ template(`<div>`);
5814
5864
  function CustomCode(props) {
5815
5865
  const [scriptsInserted, setScriptsInserted] = createSignal([]);
5816
5866
  const [scriptsRun, setScriptsRun] = createSignal([]);
5817
- function findAndRunScripts() {
5818
- if (elem && elem.getElementsByTagName && typeof window !== "undefined") {
5819
- const scripts = elem.getElementsByTagName("script");
5820
- for (let i = 0; i < scripts.length; i++) {
5821
- const script = scripts[i];
5822
- if (script.src) {
5823
- if (scriptsInserted().includes(script.src)) {
5824
- continue;
5825
- }
5826
- scriptsInserted().push(script.src);
5827
- const newScript = document.createElement("script");
5828
- newScript.async = true;
5829
- newScript.src = script.src;
5830
- document.head.appendChild(newScript);
5831
- } else if (!script.type || ["text/javascript", "application/javascript", "application/ecmascript"].includes(script.type)) {
5832
- if (scriptsRun().includes(script.innerText)) {
5833
- continue;
5834
- }
5835
- try {
5836
- scriptsRun().push(script.innerText);
5837
- new Function(script.innerText)();
5838
- } catch (error) {
5839
- console.warn("`CustomCode`: Error running script:", error);
5840
- }
5867
+ let elementRef;
5868
+ onMount(() => {
5869
+ if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
5870
+ return;
5871
+ }
5872
+ const scripts = elementRef.getElementsByTagName("script");
5873
+ for (let i = 0; i < scripts.length; i++) {
5874
+ const script = scripts[i];
5875
+ if (script.src) {
5876
+ if (scriptsInserted().includes(script.src)) {
5877
+ continue;
5878
+ }
5879
+ scriptsInserted().push(script.src);
5880
+ const newScript = document.createElement("script");
5881
+ newScript.async = true;
5882
+ newScript.src = script.src;
5883
+ document.head.appendChild(newScript);
5884
+ } else if (!script.type || ["text/javascript", "application/javascript", "application/ecmascript"].includes(script.type)) {
5885
+ if (scriptsRun().includes(script.innerText)) {
5886
+ continue;
5887
+ }
5888
+ try {
5889
+ scriptsRun().push(script.innerText);
5890
+ new Function(script.innerText)();
5891
+ } catch (error) {
5892
+ console.warn("`CustomCode`: Error running script:", error);
5841
5893
  }
5842
5894
  }
5843
5895
  }
5844
- }
5845
- let elem;
5846
- onMount(() => {
5847
- findAndRunScripts();
5848
5896
  });
5849
5897
  return (() => {
5850
5898
  const _el$ = _tmpl$13();
5851
- const _ref$ = elem;
5852
- typeof _ref$ === "function" ? use(_ref$, _el$) : elem = _el$;
5899
+ const _ref$ = elementRef;
5900
+ typeof _ref$ === "function" ? use(_ref$, _el$) : elementRef = _el$;
5853
5901
  effect((_p$) => {
5854
5902
  const _v$ = "builder-custom-code" + (props.replaceNodes ? " replace-nodes" : ""), _v$2 = props.code;
5855
5903
  _v$ !== _p$._v$ && className(_el$, _p$._v$ = _v$);
@@ -6512,7 +6560,7 @@ var track = (args) => _track(__spreadProps9(__spreadValues13({}, args), {
6512
6560
  }));
6513
6561
 
6514
6562
  // src/constants/sdk-version.js
6515
- var SDK_VERSION = "0.6.4";
6563
+ var SDK_VERSION = "0.7.1-0";
6516
6564
 
6517
6565
  // src/functions/register.js
6518
6566
  var registry = {};
@@ -6966,18 +7014,28 @@ var __spreadValues16 = (a, b) => {
6966
7014
  }
6967
7015
  return a;
6968
7016
  };
7017
+ var isPositiveNumber = (thing) => typeof thing === "number" && !isNaN(thing) && thing >= 0;
6969
7018
  var generateContentUrl = (options) => {
7019
+ let {
7020
+ noTraverse = false
7021
+ } = options;
6970
7022
  const {
6971
7023
  limit = 30,
6972
7024
  userAttributes,
6973
7025
  query,
6974
- noTraverse = false,
6975
7026
  model,
6976
7027
  apiKey,
6977
7028
  includeRefs = true,
6978
7029
  enrich,
6979
7030
  locale,
6980
- apiVersion = DEFAULT_API_VERSION
7031
+ apiVersion = DEFAULT_API_VERSION,
7032
+ fields,
7033
+ omit,
7034
+ offset,
7035
+ cacheSeconds,
7036
+ staleCacheSeconds,
7037
+ sort,
7038
+ includeUnpublished
6981
7039
  } = options;
6982
7040
  if (!apiKey) {
6983
7041
  throw new Error("Missing API key");
@@ -6985,7 +7043,34 @@ var generateContentUrl = (options) => {
6985
7043
  if (!["v2", "v3"].includes(apiVersion)) {
6986
7044
  throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
6987
7045
  }
7046
+ if ((options.limit === void 0 || options.limit > 1) && !("noTraverse" in options)) {
7047
+ noTraverse = true;
7048
+ }
6988
7049
  const url = new URL(`https://cdn.builder.io/api/${apiVersion}/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ""}${enrich ? `&enrich=${enrich}` : ""}`);
7050
+ url.searchParams.set("omit", omit || "meta.componentsUsed");
7051
+ if (fields) {
7052
+ url.searchParams.set("fields", fields);
7053
+ }
7054
+ if (Number.isFinite(offset) && offset > -1) {
7055
+ url.searchParams.set("offset", String(Math.floor(offset)));
7056
+ }
7057
+ if (typeof includeUnpublished === "boolean") {
7058
+ url.searchParams.set("includeUnpublished", String(includeUnpublished));
7059
+ }
7060
+ if (cacheSeconds && isPositiveNumber(cacheSeconds)) {
7061
+ url.searchParams.set("cacheSeconds", String(cacheSeconds));
7062
+ }
7063
+ if (staleCacheSeconds && isPositiveNumber(staleCacheSeconds)) {
7064
+ url.searchParams.set("staleCacheSeconds", String(staleCacheSeconds));
7065
+ }
7066
+ if (sort) {
7067
+ const flattened2 = flatten({
7068
+ sort
7069
+ });
7070
+ for (const key in flattened2) {
7071
+ url.searchParams.set(key, JSON.stringify(flattened2[key]));
7072
+ }
7073
+ }
6989
7074
  const queryOptions = __spreadValues16(__spreadValues16({}, getBuilderSearchParamsFromWindow()), normalizeSearchParams(options.options || {}));
6990
7075
  const flattened = flatten(queryOptions);
6991
7076
  for (const key in flattened) {
@@ -7134,7 +7219,6 @@ function isPreviewing() {
7134
7219
  // src/components/content/components/enable-editor.jsx
7135
7220
  var _tmpl$14 = /* @__PURE__ */ template(`<div>`);
7136
7221
  function EnableEditor(props) {
7137
- const [canTrackToUse, setCanTrackToUse] = createSignal(checkIsDefined(props.canTrack) ? props.canTrack : true);
7138
7222
  const [forceReRenderCount, setForceReRenderCount] = createSignal(0);
7139
7223
  createSignal(0);
7140
7224
  const [shouldSendResetCookie, setShouldSendResetCookie] = createSignal(false);
@@ -7215,7 +7299,7 @@ function EnableEditor(props) {
7215
7299
  const contentId = props.builderContextSignal.content?.id;
7216
7300
  _track({
7217
7301
  type: "click",
7218
- canTrack: canTrackToUse(),
7302
+ canTrack: getDefaultCanTrack(props.canTrack),
7219
7303
  contentId,
7220
7304
  apiKey: props.apiKey,
7221
7305
  variationId: variationId !== contentId ? variationId : void 0,
@@ -7277,11 +7361,8 @@ function EnableEditor(props) {
7277
7361
  }
7278
7362
  let elementRef;
7279
7363
  onMount(() => {
7280
- if (!props.apiKey) {
7281
- logger.error("No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
7282
- }
7283
7364
  if (isBrowser()) {
7284
- if (isEditing()) {
7365
+ if (isEditing() && true) {
7285
7366
  setForceReRenderCount(forceReRenderCount() + 1);
7286
7367
  window.addEventListener("message", processMessage);
7287
7368
  registerInsertMenu();
@@ -7302,18 +7383,20 @@ function EnableEditor(props) {
7302
7383
  });
7303
7384
  window.addEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
7304
7385
  }
7305
- if (props.builderContextSignal.content) {
7386
+ const shouldTrackImpression = props.builderContextSignal.content && getDefaultCanTrack(props.canTrack);
7387
+ if (shouldTrackImpression) {
7306
7388
  const variationId = props.builderContextSignal.content?.testVariationId;
7307
7389
  const contentId = props.builderContextSignal.content?.id;
7390
+ const apiKeyProp = props.apiKey;
7308
7391
  _track({
7309
7392
  type: "impression",
7310
- canTrack: canTrackToUse(),
7393
+ canTrack: true,
7311
7394
  contentId,
7312
- apiKey: props.apiKey,
7395
+ apiKey: apiKeyProp,
7313
7396
  variationId: variationId !== contentId ? variationId : void 0
7314
7397
  });
7315
7398
  }
7316
- if (isPreviewing()) {
7399
+ if (isPreviewing() && true) {
7317
7400
  const searchParams = new URL(location.href).searchParams;
7318
7401
  const searchParamPreviewModel = searchParams.get("builder.preview");
7319
7402
  const searchParamPreviewId = searchParams.get(`builder.preview.${searchParamPreviewModel}`);
@@ -7330,11 +7413,16 @@ function EnableEditor(props) {
7330
7413
  });
7331
7414
  }
7332
7415
  }
7333
- evaluateJsCode();
7334
- runHttpRequests();
7335
- emitStateUpdate();
7336
7416
  }
7337
7417
  });
7418
+ onMount(() => {
7419
+ if (!props.apiKey) {
7420
+ logger.error("No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
7421
+ }
7422
+ evaluateJsCode();
7423
+ runHttpRequests();
7424
+ emitStateUpdate();
7425
+ });
7338
7426
  function onUpdateFn_0() {
7339
7427
  if (props.content) {
7340
7428
  mergeNewContent(props.content);
@@ -7367,16 +7455,17 @@ function EnableEditor(props) {
7367
7455
  },
7368
7456
  get children() {
7369
7457
  const _el$ = _tmpl$14();
7370
- _el$.$$click = (event) => onClick(event);
7371
7458
  const _ref$ = elementRef;
7372
7459
  typeof _ref$ === "function" ? use(_ref$, _el$) : elementRef = _el$;
7373
7460
  spread(_el$, mergeProps({
7374
7461
  get ["class"]() {
7375
7462
  return props.classNameProp;
7376
- },
7463
+ }
7464
+ }, {}, {
7377
7465
  get key() {
7378
7466
  return forceReRenderCount();
7379
7467
  },
7468
+ "onClick": (event) => onClick(event),
7380
7469
  get ["builder-content-id"]() {
7381
7470
  return props.builderContextSignal.content?.id;
7382
7471
  },
@@ -7395,7 +7484,6 @@ function EnableEditor(props) {
7395
7484
  });
7396
7485
  }
7397
7486
  var enable_editor_default = EnableEditor;
7398
- delegateEvents(["click"]);
7399
7487
  var _tmpl$15 = /* @__PURE__ */ template(`<script>`);
7400
7488
  function InlinedScript(props) {
7401
7489
  return (() => {