@builder.io/sdk-react-nextjs 0.5.1 → 0.5.3-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.
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.5.1";
1
+ export declare const SDK_VERSION = "0.5.3-0";
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.5.1";
1
+ export const SDK_VERSION = "0.5.3-0";
@@ -2,7 +2,13 @@ import { logger } from '../../helpers/logger.js';
2
2
  import { isBrowser } from '../is-browser.js';
3
3
  import { isEditing } from '../is-editing.js';
4
4
  import { isNonNodeServer } from '../is-non-node-server.js';
5
- import { runInNonNode } from './non-node-runtime.js';
5
+ /**
6
+ * We need to lazy-load this module so it doesn't leak into the browser, as it is massive and not needed there.
7
+ */
8
+ let runInNonNode;
9
+ if (isNonNodeServer()) {
10
+ import('./non-node-runtime.js').then(theModule => runInNonNode = theModule.runInNonNode);
11
+ }
6
12
  export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true }) {
7
13
  if (code === '') {
8
14
  logger.warn('Skipping evaluation of empty code block.');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-react-nextjs",
3
3
  "description": "Builder.io SDK for React",
4
- "version": "0.5.1",
4
+ "version": "0.5.3-0",
5
5
  "files": [
6
6
  "dist"
7
7
  ],
@@ -10,7 +10,8 @@
10
10
  ".": "./dist/index.js"
11
11
  },
12
12
  "scripts": {
13
- "build": "tsc"
13
+ "clean": "rimraf dist",
14
+ "build": "yarn clean && tsc"
14
15
  },
15
16
  "peerDependencies": {
16
17
  "next": "^13.4.4",
@@ -19,6 +20,7 @@
19
20
  "devDependencies": {
20
21
  "next": "^13.4.4",
21
22
  "react": "^18.2.0",
23
+ "rimraf": "^3.0.2",
22
24
  "typescript": "^5.1.6"
23
25
  },
24
26
  "dependencies": {
@@ -1,10 +0,0 @@
1
- export type Patch = {
2
- path: string;
3
- op: 'add' | 'remove' | 'replace';
4
- value: any;
5
- };
6
- export declare const applyPatchWithMinimalMutationChain: <T extends object>(obj: T, patch: {
7
- path: string;
8
- op: 'add' | 'remove' | 'replace';
9
- value: any;
10
- }, preserveRoot?: boolean) => T;
@@ -1,54 +0,0 @@
1
- export const applyPatchWithMinimalMutationChain = (obj, patch, preserveRoot = false) => {
2
- if (Object(obj) !== obj) {
3
- return obj;
4
- }
5
- const { path, op, value } = patch;
6
- const pathArr = path.split(/\//);
7
- if (pathArr[0] === '') {
8
- pathArr.shift();
9
- }
10
- const newObj = preserveRoot ? obj : {
11
- ...obj
12
- };
13
- let objPart = newObj;
14
- for (let i = 0; i < pathArr.length; i++) {
15
- const isLast = i === pathArr.length - 1;
16
- const property = pathArr[i];
17
- if (isLast) {
18
- if (op === 'replace') {
19
- objPart[property] = value;
20
- }
21
- else if (op === 'add') {
22
- const index = Number(property);
23
- if (Array.isArray(objPart)) {
24
- if (property === '-') {
25
- objPart.push(value);
26
- }
27
- else {
28
- objPart.splice(index, 0, value);
29
- }
30
- }
31
- else {
32
- objPart[property] = value;
33
- }
34
- }
35
- else if (op === 'remove') {
36
- const index = Number(property);
37
- if (Array.isArray(objPart)) {
38
- objPart.splice(index, 1);
39
- }
40
- else {
41
- delete objPart[property];
42
- }
43
- }
44
- }
45
- else {
46
- const nextProperty = pathArr[i + 1];
47
- const newPart = Object(objPart[property]) === objPart[property] ? objPart[property] : String(Number(nextProperty)) === nextProperty ? [] : {};
48
- objPart = objPart[property] = Array.isArray(newPart) ? [...newPart] : {
49
- ...newPart
50
- };
51
- }
52
- }
53
- return newObj;
54
- };
@@ -1,7 +0,0 @@
1
- import type { BuilderContextInterface, BuilderRenderState } from '../context/types.js';
2
- export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression }: {
3
- code: string;
4
- event?: Event;
5
- isExpression?: boolean;
6
- } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): any;
7
- export declare function flattenState(rootState: Record<string | symbol, any>, localState: Record<string | symbol, any> | undefined, rootSetState: ((rootState: BuilderRenderState) => void) | undefined): BuilderRenderState;
@@ -1,46 +0,0 @@
1
- import { isBrowser } from './is-browser.js';
2
- import { isEditing } from './is-editing.js';
3
- export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true }) {
4
- if (code === '') {
5
- console.warn('Skipping evaluation of empty code block.');
6
- return;
7
- }
8
- const builder = {
9
- isEditing: isEditing(),
10
- isBrowser: isBrowser(),
11
- isServer: !isBrowser()
12
- }; // Be able to handle simple expressions like "state.foo" or "1 + 1"
13
- // as well as full blocks like "var foo = "bar"; return foo"
14
- const useReturn = // we disable this for cases where we definitely don't want a return
15
- isExpression && !(code.includes(';') || code.includes(' return ') || code.trim().startsWith('return '));
16
- const useCode = useReturn ? `return (${code});` : code;
17
- try {
18
- return new Function('builder', 'Builder'
19
- /* <- legacy */
20
- , 'state', 'context', 'event', useCode)(builder, builder, flattenState(rootState, localState, rootSetState), context, event);
21
- }
22
- catch (e) {
23
- console.warn('Builder custom code error: \n While Evaluating: \n ', useCode, '\n', e);
24
- }
25
- }
26
- export function flattenState(rootState, localState, rootSetState) {
27
- if (rootState === localState) {
28
- throw new Error('rootState === localState');
29
- }
30
- return new Proxy(rootState, {
31
- get: (_, prop) => {
32
- if (localState && prop in localState) {
33
- return localState[prop];
34
- }
35
- return rootState[prop];
36
- },
37
- set: (_, prop, value) => {
38
- if (localState && prop in localState) {
39
- throw new Error('Writing to local state is not allowed as it is read-only.');
40
- }
41
- rootState[prop] = value;
42
- rootSetState?.(rootState);
43
- return true;
44
- }
45
- });
46
- }
@@ -1,2 +0,0 @@
1
- import type { BuilderContent } from '../../types/builder-content';
2
- export declare const processCookies: (content: BuilderContent) => BuilderContent;
@@ -1,35 +0,0 @@
1
- import { cookies } from 'next/headers';
2
- import { applyPatchWithMinimalMutationChain } from '../apply-patch-with-mutation';
3
- export const processCookies = (content) => {
4
- const cookieStore = cookies();
5
- const builderPatches = cookieStore
6
- .getAll()
7
- .filter((x) => x.name.startsWith('builder.patch.' + content.id + '.'))
8
- .map((x) => {
9
- // split into: `builder.patch.${contentId}.${blockId}.${index}`
10
- const [, , , blockId, index] = x.name.split('.');
11
- return {
12
- blockId,
13
- index: parseInt(index),
14
- value: x.value,
15
- };
16
- })
17
- .sort((a, b) => a.index - b.index);
18
- if (!builderPatches.length)
19
- return content;
20
- let newContent = content;
21
- for (const patchCookie of builderPatches) {
22
- const { value } = patchCookie;
23
- try {
24
- const blockPatches = JSON.parse(value);
25
- for (const patch of blockPatches) {
26
- newContent = applyPatchWithMinimalMutationChain(newContent, patch, true);
27
- }
28
- }
29
- catch (e) {
30
- console.log('error parsing patch cookie', { value });
31
- console.log('ignoring cookie');
32
- }
33
- }
34
- return newContent;
35
- };