@builder.io/sdk-svelte 0.1.16 → 0.2.1

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-svelte",
3
3
  "description": "Builder.io SDK for Svelte",
4
- "version": "0.1.15",
4
+ "version": "0.2.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "package",
@@ -93,7 +93,6 @@
93
93
  "./functions/get-block-actions": "./functions/get-block-actions.js",
94
94
  "./functions/get-block-component-options": "./functions/get-block-component-options.js",
95
95
  "./functions/get-block-properties": "./functions/get-block-properties.js",
96
- "./functions/get-block-tag": "./functions/get-block-tag.js",
97
96
  "./functions/get-builder-search-params/fn.test": "./functions/get-builder-search-params/fn.test.js",
98
97
  "./functions/get-builder-search-params": "./functions/get-builder-search-params/index.js",
99
98
  "./functions/get-content/ab-testing": "./functions/get-content/ab-testing.js",
@@ -1,3 +1,27 @@
1
+ ### 0.2.1
2
+
3
+ - No Changes.
4
+
5
+ ### 0.2.0
6
+
7
+ - Sets the default `apiVersion` to `v3`.
8
+
9
+ In case you feel the need to use our older API Version `v2`, reach out to us at support@builder.io first. But you can override the default by setting `apiVersion` explicitly to `v2` as follows:
10
+
11
+ ```jsx
12
+ <RenderContent apiVersion="v2" />
13
+ ```
14
+
15
+ ```js
16
+ getContent({ apiVersion: 'v2' });
17
+ ```
18
+
19
+ More details on the Builder API Versions visit [this link](https://www.builder.io/c/docs/content-api-versions).
20
+
21
+ ### 0.1.16
22
+
23
+ - No changes.
24
+
1
25
  ### 0.1.15
2
26
 
3
27
  - No changes.
@@ -36,6 +36,9 @@ $: css = () => {
36
36
  const mediumStyles = styles?.medium;
37
37
  const smallStyles = styles?.small;
38
38
  const className = useBlock().id;
39
+ if (!className) {
40
+ return "";
41
+ }
39
42
  const largeStylesClass = largeStyles
40
43
  ? createCssClass({
41
44
  className,
@@ -10,3 +10,4 @@ export declare const getRepeatItemData: ({ block, context, }: {
10
10
  block: BuilderBlock;
11
11
  context: BuilderContextInterface;
12
12
  }) => RepeatData[] | undefined;
13
+ export declare const getProxyState: (context: BuilderContextInterface) => BuilderContextInterface['state'];
@@ -80,3 +80,19 @@ export const getRepeatItemData = ({ block, context, }) => {
80
80
  }));
81
81
  return repeatArray;
82
82
  };
83
+ export const getProxyState = (context) => {
84
+ if (typeof Proxy === 'undefined') {
85
+ console.error('no Proxy available in this environment, cannot proxy state.');
86
+ return context.state;
87
+ }
88
+ const useState = new Proxy(context.state, {
89
+ set: (obj, prop, value) => {
90
+ // set the value on the state object, so that the event handler instantly gets the update.
91
+ obj[prop] = value;
92
+ // set the value in the context, so that the rest of the app gets the update.
93
+ context.setState?.(obj);
94
+ return true;
95
+ },
96
+ });
97
+ return useState;
98
+ };
@@ -4,11 +4,10 @@
4
4
  import { getBlockActions } from "../../functions/get-block-actions.js";
5
5
  import { getBlockComponentOptions } from "../../functions/get-block-component-options.js";
6
6
  import { getBlockProperties } from "../../functions/get-block-properties.js";
7
- import { getBlockTag } from "../../functions/get-block-tag.js";
8
7
  import { getProcessedBlock } from "../../functions/get-processed-block.js";
9
8
  import "../../types/builder-block.js";
10
9
  import BlockStyles from "./block-styles.svelte";
11
- import { getComponent, getRepeatItemData, isEmptyHtmlElement, } from "./render-block.helpers.js";
10
+ import { getComponent, getProxyState, getRepeatItemData, isEmptyHtmlElement, } from "./render-block.helpers.js";
12
11
  import "./render-component.svelte";
13
12
  import RenderRepeatedBlock from "./render-repeated-block.svelte";
14
13
  import { TARGET } from "../../constants/target.js";
@@ -34,9 +33,6 @@ const setAttrs = (node, attrs = {}) => {
34
33
  };
35
34
  export let block;
36
35
  export let context;
37
- $: tag = () => {
38
- return getBlockTag(useBlock());
39
- };
40
36
  $: useBlock = () => {
41
37
  return repeatItemData
42
38
  ? block
@@ -56,26 +52,10 @@ $: canShowBlock = () => {
56
52
  }
57
53
  return true;
58
54
  };
59
- $: proxyState = () => {
60
- if (typeof Proxy === "undefined") {
61
- console.error("no Proxy available in this environment, cannot proxy state.");
62
- return context.state;
63
- }
64
- const useState = new Proxy(context.state, {
65
- set: (obj, prop, value) => {
66
- // set the value on the state object, so that the event handler instantly gets the update.
67
- obj[prop] = value;
68
- // set the value in the context, so that the rest of the app gets the update.
69
- context.setState?.(obj);
70
- return true;
71
- },
72
- });
73
- return useState;
74
- };
75
55
  $: actions = () => {
76
56
  return getBlockActions({
77
57
  block: useBlock(),
78
- state: proxyState(),
58
+ state: TARGET === "qwik" ? context.state : proxyState,
79
59
  context: context.context,
80
60
  });
81
61
  };
@@ -94,28 +74,6 @@ $: attributes = () => {
94
74
  : {}),
95
75
  };
96
76
  };
97
- $: renderComponentProps = () => {
98
- return {
99
- blockChildren: useBlock().children ?? [],
100
- componentRef: component?.component,
101
- componentOptions: {
102
- ...getBlockComponentOptions(useBlock()),
103
- /**
104
- * These attributes are passed to the wrapper element when there is one. If `noWrap` is set to true, then
105
- * they are provided to the component itself directly.
106
- */
107
- ...(!component?.noWrap
108
- ? {}
109
- : {
110
- attributes: {
111
- ...attributes(),
112
- ...actions(),
113
- },
114
- }),
115
- },
116
- context: childrenContext(),
117
- };
118
- };
119
77
  $: childrenWithoutParentComponent = () => {
120
78
  /**
121
79
  * When there is no `componentRef`, there might still be children that need to be rendered. In this case,
@@ -148,6 +106,28 @@ $: childrenContext = () => {
148
106
  inheritedStyles: getInheritedTextStyles(),
149
107
  };
150
108
  };
109
+ $: renderComponentProps = () => {
110
+ return {
111
+ blockChildren: useBlock().children ?? [],
112
+ componentRef: component?.component,
113
+ componentOptions: {
114
+ ...getBlockComponentOptions(useBlock()),
115
+ /**
116
+ * These attributes are passed to the wrapper element when there is one. If `noWrap` is set to true, then
117
+ * they are provided to the component itself directly.
118
+ */
119
+ ...(!component?.noWrap
120
+ ? {}
121
+ : {
122
+ attributes: {
123
+ ...attributes(),
124
+ ...actions(),
125
+ },
126
+ }),
127
+ },
128
+ context: childrenContext(),
129
+ };
130
+ };
151
131
  let component = getComponent({
152
132
  block: block,
153
133
  context: context,
@@ -156,22 +136,24 @@ let repeatItemData = getRepeatItemData({
156
136
  block: block,
157
137
  context: context,
158
138
  });
139
+ let tag = block.tagName || "div";
140
+ let proxyState = getProxyState(context);
159
141
  </script>
160
142
 
161
143
  {#if canShowBlock()}
162
144
  {#if !component?.noWrap}
163
- {#if isEmptyHtmlElement(tag())}
164
- <svelte:element this={tag()} {...attributes()} use:setAttrs={actions()} />
145
+ {#if isEmptyHtmlElement(tag)}
146
+ <svelte:element this={tag} {...attributes()} use:setAttrs={actions()} />
165
147
  {/if}
166
148
 
167
- {#if !isEmptyHtmlElement(tag()) && repeatItemData}
149
+ {#if !isEmptyHtmlElement(tag) && repeatItemData}
168
150
  {#each repeatItemData as data, index (index)}
169
151
  <RenderRepeatedBlock repeatContext={data.context} block={data.block} />
170
152
  {/each}
171
153
  {/if}
172
154
 
173
- {#if !isEmptyHtmlElement(tag()) && !repeatItemData}
174
- <svelte:element this={tag()} {...attributes()} use:setAttrs={actions()}>
155
+ {#if !isEmptyHtmlElement(tag) && !repeatItemData}
156
+ <svelte:element this={tag} {...attributes()} use:setAttrs={actions()}>
175
157
  <RenderComponent {...renderComponentProps()} />
176
158
 
177
159
  {#each childrenWithoutParentComponent() as child ("render-block-" + child.id)}
@@ -2,14 +2,14 @@
2
2
 
3
3
  <script>import { TARGET } from "../constants/target.js";
4
4
  export let styles;
5
- $: injectedStyleScript = () => {
6
- return `<${tag()}>${styles}</${tag()}>`;
7
- };
8
5
  $: tag = () => {
9
6
  // NOTE: we have to obfusctate the name of the tag due to a limitation in the svelte-preprocessor plugin.
10
7
  // https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
11
8
  return "sty" + "le";
12
9
  };
10
+ $: injectedStyleScript = () => {
11
+ return `<${tag()}>${styles}</${tag()}>`;
12
+ };
13
13
  </script>
14
14
 
15
15
  {#if TARGET === "svelte" || TARGET === "qwik"}
@@ -1,7 +1,8 @@
1
1
  import { flatten } from '../../helpers/flatten.js';
2
2
  import { getBuilderSearchParamsFromWindow, normalizeSearchParams, } from '../get-builder-search-params/index.js';
3
+ import { DEFAULT_API_VERSION } from '../../types/api-version';
3
4
  export const generateContentUrl = (options) => {
4
- const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale, apiVersion = 'v2', } = options;
5
+ const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale, apiVersion = DEFAULT_API_VERSION, } = options;
5
6
  if (!apiKey) {
6
7
  throw new Error('Missing API key');
7
8
  }
@@ -28,6 +28,15 @@ describe('Generate Content URL', () => {
28
28
  });
29
29
  expect(output).toMatchSnapshot();
30
30
  });
31
+ test('generate content url with apiVersion as default', () => {
32
+ const output = generateContentUrl({
33
+ apiKey: testKey,
34
+ model: testModel,
35
+ query: { id: testId },
36
+ options,
37
+ });
38
+ expect(output).toMatchSnapshot();
39
+ });
31
40
  test('generate content url with apiVersion as v2', () => {
32
41
  const output = generateContentUrl({
33
42
  apiKey: testKey,
@@ -39,7 +39,7 @@ export interface GetContentOptions {
39
39
  /**
40
40
  * If provided, sets the Builder API version used to fetch content.
41
41
  *
42
- * Defaults to `v2`.
42
+ * Defaults to `v3`.
43
43
  */
44
44
  apiVersion?: 'v2' | 'v3';
45
45
  }
@@ -1 +1,2 @@
1
1
  export type ApiVersion = 'v2' | 'v3';
2
+ export declare const DEFAULT_API_VERSION: ApiVersion;
@@ -1 +1 @@
1
- export {};
1
+ export const DEFAULT_API_VERSION = 'v3';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-svelte",
3
3
  "description": "Builder.io SDK for Svelte",
4
- "version": "0.1.16",
4
+ "version": "0.2.1",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "package",
@@ -48,7 +48,10 @@
48
48
  const largeStyles = styles?.large;
49
49
  const mediumStyles = styles?.medium;
50
50
  const smallStyles = styles?.small;
51
- const className = useBlock().id!;
51
+ const className = useBlock().id;
52
+ if (!className) {
53
+ return "";
54
+ }
52
55
  const largeStylesClass = largeStyles
53
56
  ? createCssClass({
54
57
  className,
@@ -1,4 +1,7 @@
1
- import type { BuilderContextInterface } from '../../context/types';
1
+ import type {
2
+ BuilderContextInterface,
3
+ BuilderRenderState,
4
+ } from '../../context/types';
2
5
  import { evaluate } from '../../functions/evaluate';
3
6
  import { getProcessedBlock } from '../../functions/get-processed-block';
4
7
  import type { BuilderBlock } from '../../types/builder-block';
@@ -110,3 +113,26 @@ export const getRepeatItemData = ({
110
113
 
111
114
  return repeatArray;
112
115
  };
116
+
117
+ export const getProxyState = (
118
+ context: BuilderContextInterface
119
+ ): BuilderContextInterface['state'] => {
120
+ if (typeof Proxy === 'undefined') {
121
+ console.error(
122
+ 'no Proxy available in this environment, cannot proxy state.'
123
+ );
124
+ return context.state;
125
+ }
126
+
127
+ const useState = new Proxy(context.state, {
128
+ set: (obj, prop: keyof BuilderRenderState, value) => {
129
+ // set the value on the state object, so that the event handler instantly gets the update.
130
+ obj[prop] = value;
131
+
132
+ // set the value in the context, so that the rest of the app gets the update.
133
+ context.setState?.(obj);
134
+ return true;
135
+ },
136
+ });
137
+ return useState;
138
+ };
@@ -6,19 +6,16 @@
6
6
  </script>
7
7
 
8
8
  <script lang="ts">
9
- import {
10
- BuilderContextInterface,
11
- BuilderRenderState,
12
- } from "../../context/types.js";
9
+ import { BuilderContextInterface } from "../../context/types.js";
13
10
  import { getBlockActions } from "../../functions/get-block-actions.js";
14
11
  import { getBlockComponentOptions } from "../../functions/get-block-component-options.js";
15
12
  import { getBlockProperties } from "../../functions/get-block-properties.js";
16
- import { getBlockTag } from "../../functions/get-block-tag.js";
17
13
  import { getProcessedBlock } from "../../functions/get-processed-block.js";
18
14
  import { BuilderBlock } from "../../types/builder-block.js";
19
15
  import BlockStyles from "./block-styles.svelte";
20
16
  import {
21
17
  getComponent,
18
+ getProxyState,
22
19
  getRepeatItemData,
23
20
  isEmptyHtmlElement,
24
21
  } from "./render-block.helpers.js";
@@ -51,9 +48,6 @@
51
48
  export let block: RenderBlockProps["block"];
52
49
  export let context: RenderBlockProps["context"];
53
50
 
54
- $: tag = () => {
55
- return getBlockTag(useBlock());
56
- };
57
51
  $: useBlock = () => {
58
52
  return repeatItemData
59
53
  ? block
@@ -73,29 +67,10 @@
73
67
  }
74
68
  return true;
75
69
  };
76
- $: proxyState = () => {
77
- if (typeof Proxy === "undefined") {
78
- console.error(
79
- "no Proxy available in this environment, cannot proxy state."
80
- );
81
- return context.state;
82
- }
83
- const useState = new Proxy(context.state, {
84
- set: (obj, prop: keyof BuilderRenderState, value) => {
85
- // set the value on the state object, so that the event handler instantly gets the update.
86
- obj[prop] = value;
87
-
88
- // set the value in the context, so that the rest of the app gets the update.
89
- context.setState?.(obj);
90
- return true;
91
- },
92
- });
93
- return useState;
94
- };
95
70
  $: actions = () => {
96
71
  return getBlockActions({
97
72
  block: useBlock(),
98
- state: proxyState(),
73
+ state: TARGET === "qwik" ? context.state : proxyState,
99
74
  context: context.context,
100
75
  });
101
76
  };
@@ -114,28 +89,6 @@
114
89
  : {}),
115
90
  };
116
91
  };
117
- $: renderComponentProps = () => {
118
- return {
119
- blockChildren: useBlock().children ?? [],
120
- componentRef: component?.component,
121
- componentOptions: {
122
- ...getBlockComponentOptions(useBlock()),
123
- /**
124
- * These attributes are passed to the wrapper element when there is one. If `noWrap` is set to true, then
125
- * they are provided to the component itself directly.
126
- */
127
- ...(!component?.noWrap
128
- ? {}
129
- : {
130
- attributes: {
131
- ...attributes(),
132
- ...actions(),
133
- },
134
- }),
135
- },
136
- context: childrenContext(),
137
- };
138
- };
139
92
  $: childrenWithoutParentComponent = () => {
140
93
  /**
141
94
  * When there is no `componentRef`, there might still be children that need to be rendered. In this case,
@@ -171,6 +124,28 @@
171
124
  inheritedStyles: getInheritedTextStyles(),
172
125
  };
173
126
  };
127
+ $: renderComponentProps = () => {
128
+ return {
129
+ blockChildren: useBlock().children ?? [],
130
+ componentRef: component?.component,
131
+ componentOptions: {
132
+ ...getBlockComponentOptions(useBlock()),
133
+ /**
134
+ * These attributes are passed to the wrapper element when there is one. If `noWrap` is set to true, then
135
+ * they are provided to the component itself directly.
136
+ */
137
+ ...(!component?.noWrap
138
+ ? {}
139
+ : {
140
+ attributes: {
141
+ ...attributes(),
142
+ ...actions(),
143
+ },
144
+ }),
145
+ },
146
+ context: childrenContext(),
147
+ };
148
+ };
174
149
 
175
150
  let component = getComponent({
176
151
  block: block,
@@ -180,22 +155,24 @@
180
155
  block: block,
181
156
  context: context,
182
157
  });
158
+ let tag = block.tagName || "div";
159
+ let proxyState = getProxyState(context);
183
160
  </script>
184
161
 
185
162
  {#if canShowBlock()}
186
163
  {#if !component?.noWrap}
187
- {#if isEmptyHtmlElement(tag())}
188
- <svelte:element this={tag()} {...attributes()} use:setAttrs={actions()} />
164
+ {#if isEmptyHtmlElement(tag)}
165
+ <svelte:element this={tag} {...attributes()} use:setAttrs={actions()} />
189
166
  {/if}
190
167
 
191
- {#if !isEmptyHtmlElement(tag()) && repeatItemData}
168
+ {#if !isEmptyHtmlElement(tag) && repeatItemData}
192
169
  {#each repeatItemData as data, index (index)}
193
170
  <RenderRepeatedBlock repeatContext={data.context} block={data.block} />
194
171
  {/each}
195
172
  {/if}
196
173
 
197
- {#if !isEmptyHtmlElement(tag()) && !repeatItemData}
198
- <svelte:element this={tag()} {...attributes()} use:setAttrs={actions()}>
174
+ {#if !isEmptyHtmlElement(tag) && !repeatItemData}
175
+ <svelte:element this={tag} {...attributes()} use:setAttrs={actions()}>
199
176
  <RenderComponent {...renderComponentProps()} />
200
177
 
201
178
  {#each childrenWithoutParentComponent() as child ("render-block-" + child.id)}
@@ -9,14 +9,14 @@
9
9
 
10
10
  export let styles: Props["styles"];
11
11
 
12
- $: injectedStyleScript = () => {
13
- return `<${tag()}>${styles}</${tag()}>`;
14
- };
15
12
  $: tag = () => {
16
13
  // NOTE: we have to obfusctate the name of the tag due to a limitation in the svelte-preprocessor plugin.
17
14
  // https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
18
15
  return "sty" + "le";
19
16
  };
17
+ $: injectedStyleScript = () => {
18
+ return `<${tag()}>${styles}</${tag()}>`;
19
+ };
20
20
  </script>
21
21
 
22
22
  {#if TARGET === "svelte" || TARGET === "qwik"}
@@ -35,6 +35,16 @@ describe('Generate Content URL', () => {
35
35
  expect(output).toMatchSnapshot();
36
36
  });
37
37
 
38
+ test('generate content url with apiVersion as default', () => {
39
+ const output = generateContentUrl({
40
+ apiKey: testKey,
41
+ model: testModel,
42
+ query: { id: testId },
43
+ options,
44
+ });
45
+ expect(output).toMatchSnapshot();
46
+ });
47
+
38
48
  test('generate content url with apiVersion as v2', () => {
39
49
  const output = generateContentUrl({
40
50
  apiKey: testKey,
@@ -4,6 +4,7 @@ import {
4
4
  normalizeSearchParams,
5
5
  } from '../get-builder-search-params/index.js';
6
6
  import type { GetContentOptions } from './types.js';
7
+ import { DEFAULT_API_VERSION } from '../../types/api-version';
7
8
 
8
9
  export const generateContentUrl = (options: GetContentOptions): URL => {
9
10
  const {
@@ -15,7 +16,7 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
15
16
  apiKey,
16
17
  includeRefs = true,
17
18
  locale,
18
- apiVersion = 'v2',
19
+ apiVersion = DEFAULT_API_VERSION,
19
20
  } = options;
20
21
 
21
22
  if (!apiKey) {
@@ -47,7 +47,7 @@ export interface GetContentOptions {
47
47
  /**
48
48
  * If provided, sets the Builder API version used to fetch content.
49
49
  *
50
- * Defaults to `v2`.
50
+ * Defaults to `v3`.
51
51
  */
52
52
  apiVersion?: 'v2' | 'v3';
53
53
  }
@@ -1 +1,2 @@
1
1
  export type ApiVersion = 'v2' | 'v3';
2
+ export const DEFAULT_API_VERSION: ApiVersion = 'v3';
@@ -1,3 +0,0 @@
1
- import type { BuilderBlock } from '../types/builder-block.js';
2
- export type TagName = string;
3
- export declare function getBlockTag(block: BuilderBlock): TagName;
@@ -1,3 +0,0 @@
1
- export function getBlockTag(block) {
2
- return block.tagName || 'div';
3
- }
@@ -1,7 +0,0 @@
1
- import type { BuilderBlock } from '../types/builder-block.js';
2
-
3
- export type TagName = string;
4
-
5
- export function getBlockTag(block: BuilderBlock): TagName {
6
- return block.tagName || 'div';
7
- }