@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.
@@ -163,6 +163,75 @@ var getUserAttributes = () => {
163
163
  };
164
164
  };
165
165
 
166
+ // src/functions/evaluate/helpers.js
167
+ var getFunctionArguments = ({
168
+ builder,
169
+ context,
170
+ event,
171
+ state
172
+ }) => {
173
+ return Object.entries({
174
+ state,
175
+ Builder: builder,
176
+ builder,
177
+ context,
178
+ event
179
+ });
180
+ };
181
+ var getBuilderGlobals = () => ({
182
+ isEditing: isEditing(),
183
+ isBrowser: isBrowser(),
184
+ isServer: !isBrowser(),
185
+ getUserAttributes: () => getUserAttributes()
186
+ });
187
+ var parseCode = (code, {
188
+ isExpression = true
189
+ }) => {
190
+ const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
191
+ const useCode = useReturn ? `return (${code});` : code;
192
+ return useCode;
193
+ };
194
+
195
+ // src/functions/evaluate/browser-runtime/browser.js
196
+ var runInBrowser = ({
197
+ code,
198
+ builder,
199
+ context,
200
+ event,
201
+ localState,
202
+ rootSetState,
203
+ rootState
204
+ }) => {
205
+ const functionArgs = getFunctionArguments({
206
+ builder,
207
+ context,
208
+ event,
209
+ state: flattenState(rootState, localState, rootSetState)
210
+ });
211
+ return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
212
+ };
213
+ function flattenState(rootState, localState, rootSetState) {
214
+ if (rootState === localState) {
215
+ throw new Error("rootState === localState");
216
+ }
217
+ return new Proxy(rootState, {
218
+ get: (_, prop) => {
219
+ if (localState && prop in localState) {
220
+ return localState[prop];
221
+ }
222
+ return rootState[prop];
223
+ },
224
+ set: (_, prop, value) => {
225
+ if (localState && prop in localState) {
226
+ throw new Error("Writing to local state is not allowed as it is read-only.");
227
+ }
228
+ rootState[prop] = value;
229
+ rootSetState == null ? void 0 : rootSetState(rootState);
230
+ return true;
231
+ }
232
+ });
233
+ }
234
+
166
235
  // src/functions/set.js
167
236
  var set = (obj, _path, value) => {
168
237
  if (Object(obj) !== obj) {
@@ -3241,22 +3310,6 @@ t.prototype.setStateStack = t.prototype.ec;
3241
3310
  t.VALUE_IN_DESCRIPTOR = Ia;
3242
3311
  var stdin_default = t;
3243
3312
 
3244
- // src/functions/evaluate/helpers.js
3245
- var getFunctionArguments = ({
3246
- builder,
3247
- context,
3248
- event,
3249
- state
3250
- }) => {
3251
- return Object.entries({
3252
- state,
3253
- Builder: builder,
3254
- builder,
3255
- context,
3256
- event
3257
- });
3258
- };
3259
-
3260
3313
  // src/functions/evaluate/edge-runtime/edge-runtime.js
3261
3314
  var __defProp = Object.defineProperty;
3262
3315
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
@@ -3354,6 +3407,9 @@ theFunction();
3354
3407
  }
3355
3408
  };
3356
3409
 
3410
+ // src/functions/evaluate/choose-eval.js
3411
+ var chooseBrowserOrServerEval = (args) => isBrowser() ? runInBrowser(args) : runInEdge(args);
3412
+
3357
3413
  // src/functions/evaluate/evaluate.js
3358
3414
  function evaluate({
3359
3415
  code,
@@ -3368,17 +3424,11 @@ function evaluate({
3368
3424
  logger.warn("Skipping evaluation of empty code block.");
3369
3425
  return;
3370
3426
  }
3371
- const builder = {
3372
- isEditing: isEditing(),
3373
- isBrowser: isBrowser(),
3374
- isServer: !isBrowser(),
3375
- getUserAttributes: () => getUserAttributes()
3376
- };
3377
- const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
3378
- const useCode = useReturn ? `return (${code});` : code;
3379
3427
  const args = {
3380
- code: useCode,
3381
- builder,
3428
+ code: parseCode(code, {
3429
+ isExpression
3430
+ }),
3431
+ builder: getBuilderGlobals(),
3382
3432
  context,
3383
3433
  event,
3384
3434
  rootSetState,
@@ -3386,7 +3436,7 @@ function evaluate({
3386
3436
  localState
3387
3437
  };
3388
3438
  try {
3389
- return runInEdge(args);
3439
+ return chooseBrowserOrServerEval(args);
3390
3440
  } catch (e) {
3391
3441
  logger.error("Failed code evaluation: " + e.message, {
3392
3442
  code
@@ -5535,44 +5585,42 @@ import { onMount, createSignal as createSignal10 } from "solid-js";
5535
5585
  function CustomCode(props) {
5536
5586
  const [scriptsInserted, setScriptsInserted] = createSignal10([]);
5537
5587
  const [scriptsRun, setScriptsRun] = createSignal10([]);
5538
- function findAndRunScripts() {
5539
- if (elem && elem.getElementsByTagName && typeof window !== "undefined") {
5540
- const scripts = elem.getElementsByTagName("script");
5541
- for (let i = 0; i < scripts.length; i++) {
5542
- const script = scripts[i];
5543
- if (script.src) {
5544
- if (scriptsInserted().includes(script.src)) {
5545
- continue;
5546
- }
5547
- scriptsInserted().push(script.src);
5548
- const newScript = document.createElement("script");
5549
- newScript.async = true;
5550
- newScript.src = script.src;
5551
- document.head.appendChild(newScript);
5552
- } else if (!script.type || [
5553
- "text/javascript",
5554
- "application/javascript",
5555
- "application/ecmascript"
5556
- ].includes(script.type)) {
5557
- if (scriptsRun().includes(script.innerText)) {
5558
- continue;
5559
- }
5560
- try {
5561
- scriptsRun().push(script.innerText);
5562
- new Function(script.innerText)();
5563
- } catch (error) {
5564
- }
5588
+ let elementRef;
5589
+ onMount(() => {
5590
+ if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
5591
+ return;
5592
+ }
5593
+ const scripts = elementRef.getElementsByTagName("script");
5594
+ for (let i = 0; i < scripts.length; i++) {
5595
+ const script = scripts[i];
5596
+ if (script.src) {
5597
+ if (scriptsInserted().includes(script.src)) {
5598
+ continue;
5599
+ }
5600
+ scriptsInserted().push(script.src);
5601
+ const newScript = document.createElement("script");
5602
+ newScript.async = true;
5603
+ newScript.src = script.src;
5604
+ document.head.appendChild(newScript);
5605
+ } else if (!script.type || [
5606
+ "text/javascript",
5607
+ "application/javascript",
5608
+ "application/ecmascript"
5609
+ ].includes(script.type)) {
5610
+ if (scriptsRun().includes(script.innerText)) {
5611
+ continue;
5612
+ }
5613
+ try {
5614
+ scriptsRun().push(script.innerText);
5615
+ new Function(script.innerText)();
5616
+ } catch (error) {
5565
5617
  }
5566
5618
  }
5567
5619
  }
5568
- }
5569
- let elem;
5570
- onMount(() => {
5571
- findAndRunScripts();
5572
5620
  });
5573
5621
  return <div
5574
5622
  class={"builder-custom-code" + (props.replaceNodes ? " replace-nodes" : "")}
5575
- ref={elem}
5623
+ ref={elementRef}
5576
5624
  innerHTML={props.code}
5577
5625
  />;
5578
5626
  }
@@ -6225,7 +6273,7 @@ var track = (args) => _track(__spreadProps9(__spreadValues13({}, args), {
6225
6273
  }));
6226
6274
 
6227
6275
  // src/constants/sdk-version.js
6228
- var SDK_VERSION = "0.7.0";
6276
+ var SDK_VERSION = "0.7.1-0";
6229
6277
 
6230
6278
  // src/functions/register.js
6231
6279
  var registry = {};
@@ -6678,6 +6726,7 @@ var __spreadValues16 = (a, b) => {
6678
6726
  }
6679
6727
  return a;
6680
6728
  };
6729
+ var isPositiveNumber = (thing) => typeof thing === "number" && !isNaN(thing) && thing >= 0;
6681
6730
  var generateContentUrl = (options) => {
6682
6731
  let {
6683
6732
  noTraverse = false
@@ -6691,7 +6740,14 @@ var generateContentUrl = (options) => {
6691
6740
  includeRefs = true,
6692
6741
  enrich,
6693
6742
  locale,
6694
- apiVersion = DEFAULT_API_VERSION
6743
+ apiVersion = DEFAULT_API_VERSION,
6744
+ fields,
6745
+ omit,
6746
+ offset,
6747
+ cacheSeconds,
6748
+ staleCacheSeconds,
6749
+ sort,
6750
+ includeUnpublished
6695
6751
  } = options;
6696
6752
  if (!apiKey) {
6697
6753
  throw new Error("Missing API key");
@@ -6703,6 +6759,30 @@ var generateContentUrl = (options) => {
6703
6759
  noTraverse = true;
6704
6760
  }
6705
6761
  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}` : ""}`);
6762
+ url.searchParams.set("omit", omit || "meta.componentsUsed");
6763
+ if (fields) {
6764
+ url.searchParams.set("fields", fields);
6765
+ }
6766
+ if (Number.isFinite(offset) && offset > -1) {
6767
+ url.searchParams.set("offset", String(Math.floor(offset)));
6768
+ }
6769
+ if (typeof includeUnpublished === "boolean") {
6770
+ url.searchParams.set("includeUnpublished", String(includeUnpublished));
6771
+ }
6772
+ if (cacheSeconds && isPositiveNumber(cacheSeconds)) {
6773
+ url.searchParams.set("cacheSeconds", String(cacheSeconds));
6774
+ }
6775
+ if (staleCacheSeconds && isPositiveNumber(staleCacheSeconds)) {
6776
+ url.searchParams.set("staleCacheSeconds", String(staleCacheSeconds));
6777
+ }
6778
+ if (sort) {
6779
+ const flattened2 = flatten({
6780
+ sort
6781
+ });
6782
+ for (const key in flattened2) {
6783
+ url.searchParams.set(key, JSON.stringify(flattened2[key]));
6784
+ }
6785
+ }
6706
6786
  const queryOptions = __spreadValues16(__spreadValues16({}, getBuilderSearchParamsFromWindow()), normalizeSearchParams(options.options || {}));
6707
6787
  const flattened = flatten(queryOptions);
6708
6788
  for (const key in flattened) {
@@ -6988,13 +7068,8 @@ function EnableEditor(props) {
6988
7068
  }
6989
7069
  let elementRef;
6990
7070
  onMount2(() => {
6991
- if (!props.apiKey) {
6992
- logger.error(
6993
- "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
6994
- );
6995
- }
6996
7071
  if (isBrowser()) {
6997
- if (isEditing()) {
7072
+ if (isEditing() && true) {
6998
7073
  setForceReRenderCount(forceReRenderCount() + 1);
6999
7074
  window.addEventListener("message", processMessage);
7000
7075
  registerInsertMenu();
@@ -7020,18 +7095,20 @@ function EnableEditor(props) {
7020
7095
  emitStateUpdate
7021
7096
  );
7022
7097
  }
7023
- if (props.builderContextSignal.content) {
7098
+ const shouldTrackImpression = props.builderContextSignal.content && getDefaultCanTrack(props.canTrack);
7099
+ if (shouldTrackImpression) {
7024
7100
  const variationId = props.builderContextSignal.content?.testVariationId;
7025
7101
  const contentId = props.builderContextSignal.content?.id;
7102
+ const apiKeyProp = props.apiKey;
7026
7103
  _track({
7027
7104
  type: "impression",
7028
- canTrack: getDefaultCanTrack(props.canTrack),
7105
+ canTrack: true,
7029
7106
  contentId,
7030
- apiKey: props.apiKey,
7107
+ apiKey: apiKeyProp,
7031
7108
  variationId: variationId !== contentId ? variationId : void 0
7032
7109
  });
7033
7110
  }
7034
- if (isPreviewing()) {
7111
+ if (isPreviewing() && true) {
7035
7112
  const searchParams = new URL(location.href).searchParams;
7036
7113
  const searchParamPreviewModel = searchParams.get("builder.preview");
7037
7114
  const searchParamPreviewId = searchParams.get(
@@ -7050,11 +7127,18 @@ function EnableEditor(props) {
7050
7127
  });
7051
7128
  }
7052
7129
  }
7053
- evaluateJsCode();
7054
- runHttpRequests();
7055
- emitStateUpdate();
7056
7130
  }
7057
7131
  });
7132
+ onMount2(() => {
7133
+ if (!props.apiKey) {
7134
+ logger.error(
7135
+ "No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
7136
+ );
7137
+ }
7138
+ evaluateJsCode();
7139
+ runHttpRequests();
7140
+ emitStateUpdate();
7141
+ });
7058
7142
  function onUpdateFn_0() {
7059
7143
  if (props.content) {
7060
7144
  mergeNewContent(props.content);
@@ -7091,6 +7175,7 @@ function EnableEditor(props) {
7091
7175
  createEffect2(on2(() => [props.builderContextSignal.rootState], onUpdateFn_4));
7092
7176
  return <stdin_default2.Provider value={props.builderContextSignal}><Show9 when={props.builderContextSignal.content}><div
7093
7177
  class={props.classNameProp}
7178
+ {...{}}
7094
7179
  key={forceReRenderCount()}
7095
7180
  ref={elementRef}
7096
7181
  onClick={(event) => onClick(event)}