@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/index.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
 
@@ -174,6 +174,75 @@ var getUserAttributes = () => {
174
174
  };
175
175
  };
176
176
 
177
+ // src/functions/evaluate/helpers.js
178
+ var getFunctionArguments = ({
179
+ builder,
180
+ context,
181
+ event,
182
+ state
183
+ }) => {
184
+ return Object.entries({
185
+ state,
186
+ Builder: builder,
187
+ builder,
188
+ context,
189
+ event
190
+ });
191
+ };
192
+ var getBuilderGlobals = () => ({
193
+ isEditing: isEditing(),
194
+ isBrowser: isBrowser(),
195
+ isServer: !isBrowser(),
196
+ getUserAttributes: () => getUserAttributes()
197
+ });
198
+ var parseCode = (code, {
199
+ isExpression = true
200
+ }) => {
201
+ const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
202
+ const useCode = useReturn ? `return (${code});` : code;
203
+ return useCode;
204
+ };
205
+
206
+ // src/functions/evaluate/browser-runtime/browser.js
207
+ var runInBrowser = ({
208
+ code,
209
+ builder,
210
+ context,
211
+ event,
212
+ localState,
213
+ rootSetState,
214
+ rootState
215
+ }) => {
216
+ const functionArgs = getFunctionArguments({
217
+ builder,
218
+ context,
219
+ event,
220
+ state: flattenState(rootState, localState, rootSetState)
221
+ });
222
+ return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
223
+ };
224
+ function flattenState(rootState, localState, rootSetState) {
225
+ if (rootState === localState) {
226
+ throw new Error("rootState === localState");
227
+ }
228
+ return new Proxy(rootState, {
229
+ get: (_, prop) => {
230
+ if (localState && prop in localState) {
231
+ return localState[prop];
232
+ }
233
+ return rootState[prop];
234
+ },
235
+ set: (_, prop, value) => {
236
+ if (localState && prop in localState) {
237
+ throw new Error("Writing to local state is not allowed as it is read-only.");
238
+ }
239
+ rootState[prop] = value;
240
+ rootSetState == null ? void 0 : rootSetState(rootState);
241
+ return true;
242
+ }
243
+ });
244
+ }
245
+
177
246
  // src/functions/set.js
178
247
  var set = (obj, _path, value) => {
179
248
  if (Object(obj) !== obj) {
@@ -3252,22 +3321,6 @@ t.prototype.setStateStack = t.prototype.ec;
3252
3321
  t.VALUE_IN_DESCRIPTOR = Ia;
3253
3322
  var stdin_default = t;
3254
3323
 
3255
- // src/functions/evaluate/helpers.js
3256
- var getFunctionArguments = ({
3257
- builder,
3258
- context,
3259
- event,
3260
- state
3261
- }) => {
3262
- return Object.entries({
3263
- state,
3264
- Builder: builder,
3265
- builder,
3266
- context,
3267
- event
3268
- });
3269
- };
3270
-
3271
3324
  // src/functions/evaluate/edge-runtime/edge-runtime.js
3272
3325
  var __defProp = Object.defineProperty;
3273
3326
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
@@ -3365,6 +3418,9 @@ theFunction();
3365
3418
  }
3366
3419
  };
3367
3420
 
3421
+ // src/functions/evaluate/choose-eval.js
3422
+ var chooseBrowserOrServerEval = (args) => isBrowser() ? runInBrowser(args) : runInEdge(args);
3423
+
3368
3424
  // src/functions/evaluate/evaluate.js
3369
3425
  function evaluate({
3370
3426
  code,
@@ -3379,17 +3435,11 @@ function evaluate({
3379
3435
  logger.warn("Skipping evaluation of empty code block.");
3380
3436
  return;
3381
3437
  }
3382
- const builder = {
3383
- isEditing: isEditing(),
3384
- isBrowser: isBrowser(),
3385
- isServer: !isBrowser(),
3386
- getUserAttributes: () => getUserAttributes()
3387
- };
3388
- const useReturn = isExpression && !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
3389
- const useCode = useReturn ? `return (${code});` : code;
3390
3438
  const args = {
3391
- code: useCode,
3392
- builder,
3439
+ code: parseCode(code, {
3440
+ isExpression
3441
+ }),
3442
+ builder: getBuilderGlobals(),
3393
3443
  context,
3394
3444
  event,
3395
3445
  rootSetState,
@@ -3397,7 +3447,7 @@ function evaluate({
3397
3447
  localState
3398
3448
  };
3399
3449
  try {
3400
- return runInEdge(args);
3450
+ return chooseBrowserOrServerEval(args);
3401
3451
  } catch (e) {
3402
3452
  logger.error("Failed code evaluation: " + e.message, {
3403
3453
  code
@@ -5799,41 +5849,39 @@ var _tmpl$13 = /* @__PURE__ */ template(`<div>`);
5799
5849
  function CustomCode(props) {
5800
5850
  const [scriptsInserted, setScriptsInserted] = createSignal([]);
5801
5851
  const [scriptsRun, setScriptsRun] = createSignal([]);
5802
- function findAndRunScripts() {
5803
- if (elem && elem.getElementsByTagName && typeof window !== "undefined") {
5804
- const scripts = elem.getElementsByTagName("script");
5805
- for (let i = 0; i < scripts.length; i++) {
5806
- const script = scripts[i];
5807
- if (script.src) {
5808
- if (scriptsInserted().includes(script.src)) {
5809
- continue;
5810
- }
5811
- scriptsInserted().push(script.src);
5812
- const newScript = document.createElement("script");
5813
- newScript.async = true;
5814
- newScript.src = script.src;
5815
- document.head.appendChild(newScript);
5816
- } else if (!script.type || ["text/javascript", "application/javascript", "application/ecmascript"].includes(script.type)) {
5817
- if (scriptsRun().includes(script.innerText)) {
5818
- continue;
5819
- }
5820
- try {
5821
- scriptsRun().push(script.innerText);
5822
- new Function(script.innerText)();
5823
- } catch (error) {
5824
- }
5852
+ let elementRef;
5853
+ onMount(() => {
5854
+ if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
5855
+ return;
5856
+ }
5857
+ const scripts = elementRef.getElementsByTagName("script");
5858
+ for (let i = 0; i < scripts.length; i++) {
5859
+ const script = scripts[i];
5860
+ if (script.src) {
5861
+ if (scriptsInserted().includes(script.src)) {
5862
+ continue;
5863
+ }
5864
+ scriptsInserted().push(script.src);
5865
+ const newScript = document.createElement("script");
5866
+ newScript.async = true;
5867
+ newScript.src = script.src;
5868
+ document.head.appendChild(newScript);
5869
+ } else if (!script.type || ["text/javascript", "application/javascript", "application/ecmascript"].includes(script.type)) {
5870
+ if (scriptsRun().includes(script.innerText)) {
5871
+ continue;
5872
+ }
5873
+ try {
5874
+ scriptsRun().push(script.innerText);
5875
+ new Function(script.innerText)();
5876
+ } catch (error) {
5825
5877
  }
5826
5878
  }
5827
5879
  }
5828
- }
5829
- let elem;
5830
- onMount(() => {
5831
- findAndRunScripts();
5832
5880
  });
5833
5881
  return (() => {
5834
5882
  const _el$ = _tmpl$13();
5835
- const _ref$ = elem;
5836
- typeof _ref$ === "function" ? use(_ref$, _el$) : elem = _el$;
5883
+ const _ref$ = elementRef;
5884
+ typeof _ref$ === "function" ? use(_ref$, _el$) : elementRef = _el$;
5837
5885
  effect((_p$) => {
5838
5886
  const _v$ = "builder-custom-code" + (props.replaceNodes ? " replace-nodes" : ""), _v$2 = props.code;
5839
5887
  _v$ !== _p$._v$ && className(_el$, _p$._v$ = _v$);
@@ -6491,7 +6539,7 @@ var track = (args) => _track(__spreadProps9(__spreadValues13({}, args), {
6491
6539
  }));
6492
6540
 
6493
6541
  // src/constants/sdk-version.js
6494
- var SDK_VERSION = "0.7.0";
6542
+ var SDK_VERSION = "0.7.1-0";
6495
6543
 
6496
6544
  // src/functions/register.js
6497
6545
  var registry = {};
@@ -6944,6 +6992,7 @@ var __spreadValues16 = (a, b) => {
6944
6992
  }
6945
6993
  return a;
6946
6994
  };
6995
+ var isPositiveNumber = (thing) => typeof thing === "number" && !isNaN(thing) && thing >= 0;
6947
6996
  var generateContentUrl = (options) => {
6948
6997
  let {
6949
6998
  noTraverse = false
@@ -6957,7 +7006,14 @@ var generateContentUrl = (options) => {
6957
7006
  includeRefs = true,
6958
7007
  enrich,
6959
7008
  locale,
6960
- apiVersion = DEFAULT_API_VERSION
7009
+ apiVersion = DEFAULT_API_VERSION,
7010
+ fields,
7011
+ omit,
7012
+ offset,
7013
+ cacheSeconds,
7014
+ staleCacheSeconds,
7015
+ sort,
7016
+ includeUnpublished
6961
7017
  } = options;
6962
7018
  if (!apiKey) {
6963
7019
  throw new Error("Missing API key");
@@ -6969,6 +7025,30 @@ var generateContentUrl = (options) => {
6969
7025
  noTraverse = true;
6970
7026
  }
6971
7027
  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}` : ""}`);
7028
+ url.searchParams.set("omit", omit || "meta.componentsUsed");
7029
+ if (fields) {
7030
+ url.searchParams.set("fields", fields);
7031
+ }
7032
+ if (Number.isFinite(offset) && offset > -1) {
7033
+ url.searchParams.set("offset", String(Math.floor(offset)));
7034
+ }
7035
+ if (typeof includeUnpublished === "boolean") {
7036
+ url.searchParams.set("includeUnpublished", String(includeUnpublished));
7037
+ }
7038
+ if (cacheSeconds && isPositiveNumber(cacheSeconds)) {
7039
+ url.searchParams.set("cacheSeconds", String(cacheSeconds));
7040
+ }
7041
+ if (staleCacheSeconds && isPositiveNumber(staleCacheSeconds)) {
7042
+ url.searchParams.set("staleCacheSeconds", String(staleCacheSeconds));
7043
+ }
7044
+ if (sort) {
7045
+ const flattened2 = flatten({
7046
+ sort
7047
+ });
7048
+ for (const key in flattened2) {
7049
+ url.searchParams.set(key, JSON.stringify(flattened2[key]));
7050
+ }
7051
+ }
6972
7052
  const queryOptions = __spreadValues16(__spreadValues16({}, getBuilderSearchParamsFromWindow()), normalizeSearchParams(options.options || {}));
6973
7053
  const flattened = flatten(queryOptions);
6974
7054
  for (const key in flattened) {
@@ -7258,11 +7338,8 @@ function EnableEditor(props) {
7258
7338
  }
7259
7339
  let elementRef;
7260
7340
  onMount(() => {
7261
- if (!props.apiKey) {
7262
- logger.error("No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
7263
- }
7264
7341
  if (isBrowser()) {
7265
- if (isEditing()) {
7342
+ if (isEditing() && true) {
7266
7343
  setForceReRenderCount(forceReRenderCount() + 1);
7267
7344
  window.addEventListener("message", processMessage);
7268
7345
  registerInsertMenu();
@@ -7283,18 +7360,20 @@ function EnableEditor(props) {
7283
7360
  });
7284
7361
  window.addEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
7285
7362
  }
7286
- if (props.builderContextSignal.content) {
7363
+ const shouldTrackImpression = props.builderContextSignal.content && getDefaultCanTrack(props.canTrack);
7364
+ if (shouldTrackImpression) {
7287
7365
  const variationId = props.builderContextSignal.content?.testVariationId;
7288
7366
  const contentId = props.builderContextSignal.content?.id;
7367
+ const apiKeyProp = props.apiKey;
7289
7368
  _track({
7290
7369
  type: "impression",
7291
- canTrack: getDefaultCanTrack(props.canTrack),
7370
+ canTrack: true,
7292
7371
  contentId,
7293
- apiKey: props.apiKey,
7372
+ apiKey: apiKeyProp,
7294
7373
  variationId: variationId !== contentId ? variationId : void 0
7295
7374
  });
7296
7375
  }
7297
- if (isPreviewing()) {
7376
+ if (isPreviewing() && true) {
7298
7377
  const searchParams = new URL(location.href).searchParams;
7299
7378
  const searchParamPreviewModel = searchParams.get("builder.preview");
7300
7379
  const searchParamPreviewId = searchParams.get(`builder.preview.${searchParamPreviewModel}`);
@@ -7311,11 +7390,16 @@ function EnableEditor(props) {
7311
7390
  });
7312
7391
  }
7313
7392
  }
7314
- evaluateJsCode();
7315
- runHttpRequests();
7316
- emitStateUpdate();
7317
7393
  }
7318
7394
  });
7395
+ onMount(() => {
7396
+ if (!props.apiKey) {
7397
+ logger.error("No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
7398
+ }
7399
+ evaluateJsCode();
7400
+ runHttpRequests();
7401
+ emitStateUpdate();
7402
+ });
7319
7403
  function onUpdateFn_0() {
7320
7404
  if (props.content) {
7321
7405
  mergeNewContent(props.content);
@@ -7348,16 +7432,17 @@ function EnableEditor(props) {
7348
7432
  },
7349
7433
  get children() {
7350
7434
  const _el$ = _tmpl$14();
7351
- _el$.$$click = (event) => onClick(event);
7352
7435
  const _ref$ = elementRef;
7353
7436
  typeof _ref$ === "function" ? use(_ref$, _el$) : elementRef = _el$;
7354
7437
  spread(_el$, mergeProps({
7355
7438
  get ["class"]() {
7356
7439
  return props.classNameProp;
7357
- },
7440
+ }
7441
+ }, {}, {
7358
7442
  get key() {
7359
7443
  return forceReRenderCount();
7360
7444
  },
7445
+ "onClick": (event) => onClick(event),
7361
7446
  get ["builder-content-id"]() {
7362
7447
  return props.builderContextSignal.content?.id;
7363
7448
  },
@@ -7376,7 +7461,6 @@ function EnableEditor(props) {
7376
7461
  });
7377
7462
  }
7378
7463
  var enable_editor_default = EnableEditor;
7379
- delegateEvents(["click"]);
7380
7464
  var _tmpl$15 = /* @__PURE__ */ template(`<script>`);
7381
7465
  function InlinedScript(props) {
7382
7466
  return (() => {