@builder.io/sdk-solid 0.7.0 → 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.jsx CHANGED
@@ -165,6 +165,75 @@ var getUserAttributes = () => {
165
165
  };
166
166
  };
167
167
 
168
+ // src/functions/evaluate/helpers.js
169
+ var getFunctionArguments = ({
170
+ builder,
171
+ context,
172
+ event,
173
+ state
174
+ }) => {
175
+ return Object.entries({
176
+ state,
177
+ Builder: builder,
178
+ builder,
179
+ context,
180
+ event
181
+ });
182
+ };
183
+ var getBuilderGlobals = () => ({
184
+ isEditing: isEditing(),
185
+ isBrowser: isBrowser(),
186
+ isServer: !isBrowser(),
187
+ getUserAttributes: () => getUserAttributes()
188
+ });
189
+ var parseCode = (code, {
190
+ isExpression = true
191
+ }) => {
192
+ const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
193
+ const useCode = useReturn ? `return (${code});` : code;
194
+ return useCode;
195
+ };
196
+
197
+ // src/functions/evaluate/browser-runtime/browser.js
198
+ var runInBrowser = ({
199
+ code,
200
+ builder,
201
+ context,
202
+ event,
203
+ localState,
204
+ rootSetState,
205
+ rootState
206
+ }) => {
207
+ const functionArgs = getFunctionArguments({
208
+ builder,
209
+ context,
210
+ event,
211
+ state: flattenState(rootState, localState, rootSetState)
212
+ });
213
+ return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
214
+ };
215
+ function flattenState(rootState, localState, rootSetState) {
216
+ if (rootState === localState) {
217
+ throw new Error("rootState === localState");
218
+ }
219
+ return new Proxy(rootState, {
220
+ get: (_, prop) => {
221
+ if (localState && prop in localState) {
222
+ return localState[prop];
223
+ }
224
+ return rootState[prop];
225
+ },
226
+ set: (_, prop, value) => {
227
+ if (localState && prop in localState) {
228
+ throw new Error("Writing to local state is not allowed as it is read-only.");
229
+ }
230
+ rootState[prop] = value;
231
+ rootSetState == null ? void 0 : rootSetState(rootState);
232
+ return true;
233
+ }
234
+ });
235
+ }
236
+
168
237
  // src/functions/set.js
169
238
  var set = (obj, _path, value) => {
170
239
  if (Object(obj) !== obj) {
@@ -3243,22 +3312,6 @@ t.prototype.setStateStack = t.prototype.ec;
3243
3312
  t.VALUE_IN_DESCRIPTOR = Ia;
3244
3313
  var stdin_default = t;
3245
3314
 
3246
- // src/functions/evaluate/helpers.js
3247
- var getFunctionArguments = ({
3248
- builder,
3249
- context,
3250
- event,
3251
- state
3252
- }) => {
3253
- return Object.entries({
3254
- state,
3255
- Builder: builder,
3256
- builder,
3257
- context,
3258
- event
3259
- });
3260
- };
3261
-
3262
3315
  // src/functions/evaluate/edge-runtime/edge-runtime.js
3263
3316
  var __defProp = Object.defineProperty;
3264
3317
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
@@ -3356,6 +3409,9 @@ theFunction();
3356
3409
  }
3357
3410
  };
3358
3411
 
3412
+ // src/functions/evaluate/choose-eval.js
3413
+ var chooseBrowserOrServerEval = (args) => isBrowser() ? runInBrowser(args) : runInEdge(args);
3414
+
3359
3415
  // src/functions/evaluate/evaluate.js
3360
3416
  function evaluate({
3361
3417
  code,
@@ -3370,17 +3426,11 @@ function evaluate({
3370
3426
  logger.warn("Skipping evaluation of empty code block.");
3371
3427
  return;
3372
3428
  }
3373
- const builder = {
3374
- isEditing: isEditing(),
3375
- isBrowser: isBrowser(),
3376
- isServer: !isBrowser(),
3377
- getUserAttributes: () => getUserAttributes()
3378
- };
3379
- const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
3380
- const useCode = useReturn ? `return (${code});` : code;
3381
3429
  const args = {
3382
- code: useCode,
3383
- builder,
3430
+ code: parseCode(code, {
3431
+ isExpression
3432
+ }),
3433
+ builder: getBuilderGlobals(),
3384
3434
  context,
3385
3435
  event,
3386
3436
  rootSetState,
@@ -3388,7 +3438,7 @@ function evaluate({
3388
3438
  localState
3389
3439
  };
3390
3440
  try {
3391
- return runInEdge(args);
3441
+ return chooseBrowserOrServerEval(args);
3392
3442
  } catch (e) {
3393
3443
  logger.error("Failed code evaluation: " + e.message, {
3394
3444
  code
@@ -5545,45 +5595,43 @@ import { onMount, createSignal as createSignal10 } from "solid-js";
5545
5595
  function CustomCode(props) {
5546
5596
  const [scriptsInserted, setScriptsInserted] = createSignal10([]);
5547
5597
  const [scriptsRun, setScriptsRun] = createSignal10([]);
5548
- function findAndRunScripts() {
5549
- if (elem && elem.getElementsByTagName && typeof window !== "undefined") {
5550
- const scripts = elem.getElementsByTagName("script");
5551
- for (let i = 0; i < scripts.length; i++) {
5552
- const script = scripts[i];
5553
- if (script.src) {
5554
- if (scriptsInserted().includes(script.src)) {
5555
- continue;
5556
- }
5557
- scriptsInserted().push(script.src);
5558
- const newScript = document.createElement("script");
5559
- newScript.async = true;
5560
- newScript.src = script.src;
5561
- document.head.appendChild(newScript);
5562
- } else if (!script.type || [
5563
- "text/javascript",
5564
- "application/javascript",
5565
- "application/ecmascript"
5566
- ].includes(script.type)) {
5567
- if (scriptsRun().includes(script.innerText)) {
5568
- continue;
5569
- }
5570
- try {
5571
- scriptsRun().push(script.innerText);
5572
- new Function(script.innerText)();
5573
- } catch (error) {
5574
- console.warn("`CustomCode`: Error running script:", error);
5575
- }
5598
+ let elementRef;
5599
+ onMount(() => {
5600
+ if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
5601
+ return;
5602
+ }
5603
+ const scripts = elementRef.getElementsByTagName("script");
5604
+ for (let i = 0; i < scripts.length; i++) {
5605
+ const script = scripts[i];
5606
+ if (script.src) {
5607
+ if (scriptsInserted().includes(script.src)) {
5608
+ continue;
5609
+ }
5610
+ scriptsInserted().push(script.src);
5611
+ const newScript = document.createElement("script");
5612
+ newScript.async = true;
5613
+ newScript.src = script.src;
5614
+ document.head.appendChild(newScript);
5615
+ } else if (!script.type || [
5616
+ "text/javascript",
5617
+ "application/javascript",
5618
+ "application/ecmascript"
5619
+ ].includes(script.type)) {
5620
+ if (scriptsRun().includes(script.innerText)) {
5621
+ continue;
5622
+ }
5623
+ try {
5624
+ scriptsRun().push(script.innerText);
5625
+ new Function(script.innerText)();
5626
+ } catch (error) {
5627
+ console.warn("`CustomCode`: Error running script:", error);
5576
5628
  }
5577
5629
  }
5578
5630
  }
5579
- }
5580
- let elem;
5581
- onMount(() => {
5582
- findAndRunScripts();
5583
5631
  });
5584
5632
  return <div
5585
5633
  class={"builder-custom-code" + (props.replaceNodes ? " replace-nodes" : "")}
5586
- ref={elem}
5634
+ ref={elementRef}
5587
5635
  innerHTML={props.code}
5588
5636
  />;
5589
5637
  }
@@ -6241,7 +6289,7 @@ var track = (args) => _track(__spreadProps9(__spreadValues13({}, args), {
6241
6289
  }));
6242
6290
 
6243
6291
  // src/constants/sdk-version.js
6244
- var SDK_VERSION = "0.7.0";
6292
+ var SDK_VERSION = "0.7.1-0";
6245
6293
 
6246
6294
  // src/functions/register.js
6247
6295
  var registry = {};
@@ -6695,6 +6743,7 @@ var __spreadValues16 = (a, b) => {
6695
6743
  }
6696
6744
  return a;
6697
6745
  };
6746
+ var isPositiveNumber = (thing) => typeof thing === "number" && !isNaN(thing) && thing >= 0;
6698
6747
  var generateContentUrl = (options) => {
6699
6748
  let {
6700
6749
  noTraverse = false
@@ -6708,7 +6757,14 @@ var generateContentUrl = (options) => {
6708
6757
  includeRefs = true,
6709
6758
  enrich,
6710
6759
  locale,
6711
- apiVersion = DEFAULT_API_VERSION
6760
+ apiVersion = DEFAULT_API_VERSION,
6761
+ fields,
6762
+ omit,
6763
+ offset,
6764
+ cacheSeconds,
6765
+ staleCacheSeconds,
6766
+ sort,
6767
+ includeUnpublished
6712
6768
  } = options;
6713
6769
  if (!apiKey) {
6714
6770
  throw new Error("Missing API key");
@@ -6720,6 +6776,30 @@ var generateContentUrl = (options) => {
6720
6776
  noTraverse = true;
6721
6777
  }
6722
6778
  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}` : ""}`);
6779
+ url.searchParams.set("omit", omit || "meta.componentsUsed");
6780
+ if (fields) {
6781
+ url.searchParams.set("fields", fields);
6782
+ }
6783
+ if (Number.isFinite(offset) && offset > -1) {
6784
+ url.searchParams.set("offset", String(Math.floor(offset)));
6785
+ }
6786
+ if (typeof includeUnpublished === "boolean") {
6787
+ url.searchParams.set("includeUnpublished", String(includeUnpublished));
6788
+ }
6789
+ if (cacheSeconds && isPositiveNumber(cacheSeconds)) {
6790
+ url.searchParams.set("cacheSeconds", String(cacheSeconds));
6791
+ }
6792
+ if (staleCacheSeconds && isPositiveNumber(staleCacheSeconds)) {
6793
+ url.searchParams.set("staleCacheSeconds", String(staleCacheSeconds));
6794
+ }
6795
+ if (sort) {
6796
+ const flattened2 = flatten({
6797
+ sort
6798
+ });
6799
+ for (const key in flattened2) {
6800
+ url.searchParams.set(key, JSON.stringify(flattened2[key]));
6801
+ }
6802
+ }
6723
6803
  const queryOptions = __spreadValues16(__spreadValues16({}, getBuilderSearchParamsFromWindow()), normalizeSearchParams(options.options || {}));
6724
6804
  const flattened = flatten(queryOptions);
6725
6805
  for (const key in flattened) {
@@ -7006,13 +7086,8 @@ function EnableEditor(props) {
7006
7086
  }
7007
7087
  let elementRef;
7008
7088
  onMount2(() => {
7009
- if (!props.apiKey) {
7010
- logger.error(
7011
- "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
7012
- );
7013
- }
7014
7089
  if (isBrowser()) {
7015
- if (isEditing()) {
7090
+ if (isEditing() && true) {
7016
7091
  setForceReRenderCount(forceReRenderCount() + 1);
7017
7092
  window.addEventListener("message", processMessage);
7018
7093
  registerInsertMenu();
@@ -7038,18 +7113,20 @@ function EnableEditor(props) {
7038
7113
  emitStateUpdate
7039
7114
  );
7040
7115
  }
7041
- if (props.builderContextSignal.content) {
7116
+ const shouldTrackImpression = props.builderContextSignal.content && getDefaultCanTrack(props.canTrack);
7117
+ if (shouldTrackImpression) {
7042
7118
  const variationId = props.builderContextSignal.content?.testVariationId;
7043
7119
  const contentId = props.builderContextSignal.content?.id;
7120
+ const apiKeyProp = props.apiKey;
7044
7121
  _track({
7045
7122
  type: "impression",
7046
- canTrack: getDefaultCanTrack(props.canTrack),
7123
+ canTrack: true,
7047
7124
  contentId,
7048
- apiKey: props.apiKey,
7125
+ apiKey: apiKeyProp,
7049
7126
  variationId: variationId !== contentId ? variationId : void 0
7050
7127
  });
7051
7128
  }
7052
- if (isPreviewing()) {
7129
+ if (isPreviewing() && true) {
7053
7130
  const searchParams = new URL(location.href).searchParams;
7054
7131
  const searchParamPreviewModel = searchParams.get("builder.preview");
7055
7132
  const searchParamPreviewId = searchParams.get(
@@ -7068,11 +7145,18 @@ function EnableEditor(props) {
7068
7145
  });
7069
7146
  }
7070
7147
  }
7071
- evaluateJsCode();
7072
- runHttpRequests();
7073
- emitStateUpdate();
7074
7148
  }
7075
7149
  });
7150
+ onMount2(() => {
7151
+ if (!props.apiKey) {
7152
+ logger.error(
7153
+ "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
7154
+ );
7155
+ }
7156
+ evaluateJsCode();
7157
+ runHttpRequests();
7158
+ emitStateUpdate();
7159
+ });
7076
7160
  function onUpdateFn_0() {
7077
7161
  if (props.content) {
7078
7162
  mergeNewContent(props.content);
@@ -7109,6 +7193,7 @@ function EnableEditor(props) {
7109
7193
  createEffect2(on2(() => [props.builderContextSignal.rootState], onUpdateFn_4));
7110
7194
  return <stdin_default2.Provider value={props.builderContextSignal}><Show9 when={props.builderContextSignal.content}><div
7111
7195
  class={props.classNameProp}
7196
+ {...{}}
7112
7197
  key={forceReRenderCount()}
7113
7198
  ref={elementRef}
7114
7199
  onClick={(event) => onClick(event)}