@builder.io/sdk-svelte 0.0.10-0 → 0.0.11-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/README.md +4 -0
- package/package/--package.json +3 -5
- package/package/README.md +4 -0
- package/package/blocks/symbol/symbol.svelte +17 -4
- package/package/blocks/symbol/symbol.svelte.d.ts +2 -1
- package/package/components/render-block/render-block.svelte +4 -1
- package/package/components/render-content/render-content.svelte +2 -3
- package/package/functions/get-content/generate-content-url.d.ts +2 -0
- package/package/functions/get-content/generate-content-url.js +27 -0
- package/package/functions/get-content/{fn.test.d.ts → generate-content-url.test.d.ts} +0 -0
- package/package/functions/get-content/{fn.test.js → generate-content-url.test.js} +1 -1
- package/package/functions/get-content/index.d.ts +1 -2
- package/package/functions/get-content/index.js +4 -30
- package/package/functions/get-fetch.d.ts +1 -1
- package/package/functions/get-fetch.js +7 -5
- package/package/functions/get-global-this.d.ts +2 -1
- package/package/functions/get-global-this.js +1 -1
- package/package/functions/get-react-native-block-styles.d.ts +2 -1
- package/package/functions/get-react-native-block-styles.js +2 -1
- package/package.json +1 -4
- package/src/blocks/symbol/symbol.svelte +17 -4
- package/src/components/render-block/render-block.svelte +4 -1
- package/src/components/render-content/render-content.svelte +2 -3
- package/src/functions/get-content/{fn.test.ts → generate-content-url.test.ts} +1 -1
- package/src/functions/get-content/generate-content-url.ts +51 -0
- package/src/functions/get-content/index.ts +5 -57
- package/src/functions/get-fetch.ts +11 -7
- package/src/functions/get-global-this.ts +2 -2
- package/src/functions/get-react-native-block-styles.ts +3 -0
package/README.md
CHANGED
|
@@ -17,3 +17,7 @@ npm install @builder.io/sdk-svelte@dev
|
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
Take a look at [our example repo](/examples/svelte/sveltekit) for how to use this SDK.
|
|
20
|
+
|
|
21
|
+
## Fetch
|
|
22
|
+
|
|
23
|
+
This Package uses fetch. See [these docs](https://github.com/BuilderIO/this-package-uses-fetch/blob/main/README.md) for more information.
|
package/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.0.
|
|
4
|
+
"version": "0.0.10",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"package",
|
|
@@ -95,7 +95,8 @@
|
|
|
95
95
|
"./functions/get-builder-search-params/fn.test": "./functions/get-builder-search-params/fn.test.js",
|
|
96
96
|
"./functions/get-builder-search-params": "./functions/get-builder-search-params/index.js",
|
|
97
97
|
"./functions/get-content/ab-testing": "./functions/get-content/ab-testing.js",
|
|
98
|
-
"./functions/get-content/
|
|
98
|
+
"./functions/get-content/generate-content-url.test": "./functions/get-content/generate-content-url.test.js",
|
|
99
|
+
"./functions/get-content/generate-content-url": "./functions/get-content/generate-content-url.js",
|
|
99
100
|
"./functions/get-content": "./functions/get-content/index.js",
|
|
100
101
|
"./functions/get-content/types": "./functions/get-content/types.js",
|
|
101
102
|
"./functions/get-fetch": "./functions/get-fetch.js",
|
|
@@ -154,8 +155,5 @@
|
|
|
154
155
|
"tslib": "^2.3.1",
|
|
155
156
|
"typescript": "^4",
|
|
156
157
|
"vite": "^3.0.4"
|
|
157
|
-
},
|
|
158
|
-
"dependencies": {
|
|
159
|
-
"node-fetch": "^2.6.1"
|
|
160
158
|
}
|
|
161
159
|
}
|
package/package/README.md
CHANGED
|
@@ -17,3 +17,7 @@ npm install @builder.io/sdk-svelte@dev
|
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
Take a look at [our example repo](/examples/svelte/sveltekit) for how to use this SDK.
|
|
20
|
+
|
|
21
|
+
## Fetch
|
|
22
|
+
|
|
23
|
+
This Package uses fetch. See [these docs](https://github.com/BuilderIO/this-package-uses-fetch/blob/main/README.md) for more information.
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import RenderContent from "../../components/render-content/render-content.svelte";
|
|
5
5
|
import BuilderContext from "../../context/builder.context.js";
|
|
6
6
|
import { getContent } from "../../functions/get-content/index.js";
|
|
7
|
+
import { TARGET } from "../../constants/target";
|
|
7
8
|
const isEvent = (attr) => attr.startsWith("on:");
|
|
8
9
|
const isNonEvent = (attr) => !attr.startsWith("on:");
|
|
9
10
|
const filterAttrs = (attrs, filter) => {
|
|
@@ -31,13 +32,25 @@ const setAttrs = (node, attrs) => {
|
|
|
31
32
|
},
|
|
32
33
|
};
|
|
33
34
|
};
|
|
34
|
-
export let symbol;
|
|
35
35
|
export let attributes;
|
|
36
|
+
export let symbol;
|
|
37
|
+
export let dynamic;
|
|
36
38
|
let builderContext = getContext(BuilderContext.key);
|
|
39
|
+
$: className = () => {
|
|
40
|
+
return [
|
|
41
|
+
...(TARGET === "vue2" || TARGET === "vue3"
|
|
42
|
+
? Object.keys(attributes.class)
|
|
43
|
+
: [attributes.class]),
|
|
44
|
+
"builder-symbol",
|
|
45
|
+
symbol?.inline ? "builder-inline-symbol" : undefined,
|
|
46
|
+
symbol?.dynamic || dynamic ? "builder-dynamic-symbol" : undefined,
|
|
47
|
+
]
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.join(" ");
|
|
50
|
+
};
|
|
37
51
|
$: contentToUse = () => {
|
|
38
52
|
return symbol?.content || fetchedContent;
|
|
39
53
|
};
|
|
40
|
-
let className = "builder-symbol";
|
|
41
54
|
let fetchedContent = null;
|
|
42
55
|
function onUpdateFn_0() {
|
|
43
56
|
const symbolToUse = symbol;
|
|
@@ -71,9 +84,9 @@ $: onUpdateFn_0(...[symbol, fetchedContent]);
|
|
|
71
84
|
<div
|
|
72
85
|
{...filterAttrs(attributes, isNonEvent)}
|
|
73
86
|
dataSet={{
|
|
74
|
-
class: className,
|
|
87
|
+
class: className(),
|
|
75
88
|
}}
|
|
76
|
-
class={className}
|
|
89
|
+
class={className()}
|
|
77
90
|
use:setAttrs={filterAttrs(attributes, isEvent)}
|
|
78
91
|
>
|
|
79
92
|
<RenderContent
|
|
@@ -19,8 +19,9 @@ export interface SymbolProps {
|
|
|
19
19
|
}
|
|
20
20
|
declare const __propDef: {
|
|
21
21
|
props: {
|
|
22
|
-
symbol: SymbolProps["symbol"];
|
|
23
22
|
attributes: SymbolProps["attributes"];
|
|
23
|
+
symbol: SymbolProps["symbol"];
|
|
24
|
+
dynamic: SymbolProps["dynamic"];
|
|
24
25
|
};
|
|
25
26
|
events: {
|
|
26
27
|
[evt: string]: CustomEvent<any>;
|
|
@@ -75,13 +75,15 @@ $: actions = () => {
|
|
|
75
75
|
});
|
|
76
76
|
};
|
|
77
77
|
$: attributes = () => {
|
|
78
|
+
const blockProperties = getBlockProperties(useBlock());
|
|
78
79
|
return {
|
|
79
|
-
...
|
|
80
|
+
...blockProperties,
|
|
80
81
|
...(TARGET === "reactNative"
|
|
81
82
|
? {
|
|
82
83
|
style: getReactNativeBlockStyles({
|
|
83
84
|
block: useBlock(),
|
|
84
85
|
context: context,
|
|
86
|
+
blockStyles: blockProperties.style,
|
|
85
87
|
}),
|
|
86
88
|
}
|
|
87
89
|
: {}),
|
|
@@ -171,6 +173,7 @@ $: inheritedTextStyles = () => {
|
|
|
171
173
|
const styles = getReactNativeBlockStyles({
|
|
172
174
|
block: useBlock(),
|
|
173
175
|
context: context,
|
|
176
|
+
blockStyles: attributes().style,
|
|
174
177
|
});
|
|
175
178
|
return extractTextStyles(styles);
|
|
176
179
|
};
|
|
@@ -5,7 +5,7 @@ import { getDefaultRegisteredComponents } from "../../constants/builder-register
|
|
|
5
5
|
import { TARGET } from "../../constants/target.js";
|
|
6
6
|
import { evaluate } from "../../functions/evaluate.js";
|
|
7
7
|
import { getContent } from "../../functions/get-content/index.js";
|
|
8
|
-
import {
|
|
8
|
+
import { fetch } from "../../functions/get-fetch.js";
|
|
9
9
|
import { isBrowser } from "../../functions/is-browser.js";
|
|
10
10
|
import { isEditing } from "../../functions/is-editing.js";
|
|
11
11
|
import { isPreviewing } from "../../functions/is-previewing.js";
|
|
@@ -107,8 +107,7 @@ function evalExpression(expression) {
|
|
|
107
107
|
}));
|
|
108
108
|
}
|
|
109
109
|
function handleRequest({ url, key }) {
|
|
110
|
-
|
|
111
|
-
.then((fetch) => fetch(url))
|
|
110
|
+
fetch(url)
|
|
112
111
|
.then((response) => response.json())
|
|
113
112
|
.then((json) => {
|
|
114
113
|
const newOverrideState = {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { flatten } from '../../helpers/flatten.js';
|
|
2
|
+
import { getBuilderSearchParamsFromWindow, normalizeSearchParams, } from '../get-builder-search-params/index.js';
|
|
3
|
+
export const generateContentUrl = (options) => {
|
|
4
|
+
const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale, } = options;
|
|
5
|
+
if (!apiKey) {
|
|
6
|
+
throw new Error('Missing API key');
|
|
7
|
+
}
|
|
8
|
+
const url = new URL(`https://cdn.builder.io/api/v2/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ''}`);
|
|
9
|
+
const queryOptions = {
|
|
10
|
+
...getBuilderSearchParamsFromWindow(),
|
|
11
|
+
...normalizeSearchParams(options.options || {}),
|
|
12
|
+
};
|
|
13
|
+
const flattened = flatten(queryOptions);
|
|
14
|
+
for (const key in flattened) {
|
|
15
|
+
url.searchParams.set(key, String(flattened[key]));
|
|
16
|
+
}
|
|
17
|
+
if (userAttributes) {
|
|
18
|
+
url.searchParams.set('userAttributes', JSON.stringify(userAttributes));
|
|
19
|
+
}
|
|
20
|
+
if (query) {
|
|
21
|
+
const flattened = flatten({ query });
|
|
22
|
+
for (const key in flattened) {
|
|
23
|
+
url.searchParams.set(key, JSON.stringify(flattened[key]));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return url;
|
|
27
|
+
};
|
|
File without changes
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { BuilderContent } from '../../types/builder-content.js';
|
|
2
|
-
|
|
2
|
+
import type { GetContentOptions } from './types.js';
|
|
3
3
|
export declare function getContent(options: GetContentOptions): Promise<BuilderContent | null>;
|
|
4
|
-
export declare const generateContentUrl: (options: GetContentOptions) => URL;
|
|
5
4
|
/**
|
|
6
5
|
* TO-DO: Handle error responses.
|
|
7
6
|
*/
|
|
@@ -1,39 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getBuilderSearchParamsFromWindow, normalizeSearchParams, } from '../get-builder-search-params/index.js';
|
|
3
|
-
import { getFetch } from '../get-fetch.js';
|
|
1
|
+
import { fetch } from '../get-fetch.js';
|
|
4
2
|
import { handleABTesting } from './ab-testing.js';
|
|
3
|
+
import { generateContentUrl } from './generate-content-url.js';
|
|
5
4
|
export async function getContent(options) {
|
|
6
5
|
return (await getAllContent({ ...options, limit: 1 })).results[0] || null;
|
|
7
6
|
}
|
|
8
|
-
export const generateContentUrl = (options) => {
|
|
9
|
-
const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale, } = options;
|
|
10
|
-
if (!apiKey) {
|
|
11
|
-
throw new Error('Missing API key');
|
|
12
|
-
}
|
|
13
|
-
const url = new URL(`https://cdn.builder.io/api/v2/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ''}`);
|
|
14
|
-
const queryOptions = {
|
|
15
|
-
...getBuilderSearchParamsFromWindow(),
|
|
16
|
-
...normalizeSearchParams(options.options || {}),
|
|
17
|
-
};
|
|
18
|
-
const flattened = flatten(queryOptions);
|
|
19
|
-
for (const key in flattened) {
|
|
20
|
-
url.searchParams.set(key, String(flattened[key]));
|
|
21
|
-
}
|
|
22
|
-
if (userAttributes) {
|
|
23
|
-
url.searchParams.set('userAttributes', JSON.stringify(userAttributes));
|
|
24
|
-
}
|
|
25
|
-
if (query) {
|
|
26
|
-
const flattened = flatten({ query });
|
|
27
|
-
for (const key in flattened) {
|
|
28
|
-
url.searchParams.set(key, JSON.stringify(flattened[key]));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return url;
|
|
32
|
-
};
|
|
33
7
|
export async function getAllContent(options) {
|
|
34
8
|
const url = generateContentUrl(options);
|
|
35
|
-
const
|
|
36
|
-
const content = await
|
|
9
|
+
const res = await fetch(url.href);
|
|
10
|
+
const content = await res.json();
|
|
37
11
|
const canTrack = options.canTrack !== false;
|
|
38
12
|
if (canTrack &&
|
|
39
13
|
// This makes sure we have a non-error response with the results array.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare
|
|
1
|
+
export declare const fetch: typeof globalThis.fetch;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { getGlobalThis } from './get-global-this.js';
|
|
2
|
-
|
|
2
|
+
function getFetch() {
|
|
3
3
|
const globalFetch = getGlobalThis().fetch;
|
|
4
|
-
if (typeof globalFetch === 'undefined'
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
if (typeof globalFetch === 'undefined') {
|
|
5
|
+
console.warn(`Builder SDK could not find a global fetch function. Make sure you have a polyfill for fetch in your project.
|
|
6
|
+
For more information, read https://github.com/BuilderIO/this-package-uses-fetch`);
|
|
7
|
+
throw new Error('Builder SDK could not find a global `fetch` function');
|
|
7
8
|
}
|
|
8
|
-
return globalFetch
|
|
9
|
+
return globalFetch;
|
|
9
10
|
}
|
|
11
|
+
export const fetch = getFetch();
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="@types/node" />
|
|
2
|
+
export declare function getGlobalThis(): typeof global;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
2
|
import type { BuilderBlock } from '../types/builder-block.js';
|
|
3
|
-
export declare function getReactNativeBlockStyles({ block, context, }: {
|
|
3
|
+
export declare function getReactNativeBlockStyles({ block, context, blockStyles, }: {
|
|
4
4
|
block: BuilderBlock;
|
|
5
5
|
context: BuilderContextInterface;
|
|
6
|
+
blockStyles: any;
|
|
6
7
|
}): CSSStyleDeclaration | Record<string, string | undefined>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { sanitizeReactNativeBlockStyles } from './sanitize-react-native-block-styles.js';
|
|
2
|
-
export function getReactNativeBlockStyles({ block, context, }) {
|
|
2
|
+
export function getReactNativeBlockStyles({ block, context, blockStyles, }) {
|
|
3
3
|
const responsiveStyles = block.responsiveStyles;
|
|
4
4
|
if (!responsiveStyles) {
|
|
5
5
|
return {};
|
|
@@ -10,6 +10,7 @@ export function getReactNativeBlockStyles({ block, context, }) {
|
|
|
10
10
|
...(responsiveStyles.large || {}),
|
|
11
11
|
...(responsiveStyles.medium || {}),
|
|
12
12
|
...(responsiveStyles.small || {}),
|
|
13
|
+
...blockStyles,
|
|
13
14
|
};
|
|
14
15
|
const newStyles = sanitizeReactNativeBlockStyles(styles);
|
|
15
16
|
return newStyles;
|
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.0.
|
|
4
|
+
"version": "0.0.11-0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"package",
|
|
@@ -46,8 +46,5 @@
|
|
|
46
46
|
"tslib": "^2.3.1",
|
|
47
47
|
"typescript": "^4",
|
|
48
48
|
"vite": "^3.0.4"
|
|
49
|
-
},
|
|
50
|
-
"dependencies": {
|
|
51
|
-
"node-fetch": "^2.6.1"
|
|
52
49
|
}
|
|
53
50
|
}
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
import RenderContent from "../../components/render-content/render-content.svelte";
|
|
31
31
|
import BuilderContext from "../../context/builder.context.js";
|
|
32
32
|
import { getContent } from "../../functions/get-content/index.js";
|
|
33
|
+
import { TARGET } from "../../constants/target";
|
|
33
34
|
|
|
34
35
|
const isEvent = (attr) => attr.startsWith("on:");
|
|
35
36
|
const isNonEvent = (attr) => !attr.startsWith("on:");
|
|
@@ -65,16 +66,28 @@
|
|
|
65
66
|
};
|
|
66
67
|
};
|
|
67
68
|
|
|
68
|
-
export let symbol: SymbolProps["symbol"];
|
|
69
69
|
export let attributes: SymbolProps["attributes"];
|
|
70
|
+
export let symbol: SymbolProps["symbol"];
|
|
71
|
+
export let dynamic: SymbolProps["dynamic"];
|
|
70
72
|
|
|
71
73
|
let builderContext = getContext(BuilderContext.key);
|
|
72
74
|
|
|
75
|
+
$: className = () => {
|
|
76
|
+
return [
|
|
77
|
+
...(TARGET === "vue2" || TARGET === "vue3"
|
|
78
|
+
? Object.keys(attributes.class)
|
|
79
|
+
: [attributes.class]),
|
|
80
|
+
"builder-symbol",
|
|
81
|
+
symbol?.inline ? "builder-inline-symbol" : undefined,
|
|
82
|
+
symbol?.dynamic || dynamic ? "builder-dynamic-symbol" : undefined,
|
|
83
|
+
]
|
|
84
|
+
.filter(Boolean)
|
|
85
|
+
.join(" ");
|
|
86
|
+
};
|
|
73
87
|
$: contentToUse = () => {
|
|
74
88
|
return symbol?.content || fetchedContent;
|
|
75
89
|
};
|
|
76
90
|
|
|
77
|
-
let className = "builder-symbol";
|
|
78
91
|
let fetchedContent = null;
|
|
79
92
|
|
|
80
93
|
function onUpdateFn_0() {
|
|
@@ -112,9 +125,9 @@
|
|
|
112
125
|
<div
|
|
113
126
|
{...filterAttrs(attributes, isNonEvent)}
|
|
114
127
|
dataSet={{
|
|
115
|
-
class: className,
|
|
128
|
+
class: className(),
|
|
116
129
|
}}
|
|
117
|
-
class={className}
|
|
130
|
+
class={className()}
|
|
118
131
|
use:setAttrs={filterAttrs(attributes, isEvent)}
|
|
119
132
|
>
|
|
120
133
|
<RenderContent
|
|
@@ -100,13 +100,15 @@
|
|
|
100
100
|
});
|
|
101
101
|
};
|
|
102
102
|
$: attributes = () => {
|
|
103
|
+
const blockProperties = getBlockProperties(useBlock());
|
|
103
104
|
return {
|
|
104
|
-
...
|
|
105
|
+
...blockProperties,
|
|
105
106
|
...(TARGET === "reactNative"
|
|
106
107
|
? {
|
|
107
108
|
style: getReactNativeBlockStyles({
|
|
108
109
|
block: useBlock(),
|
|
109
110
|
context: context,
|
|
111
|
+
blockStyles: blockProperties.style,
|
|
110
112
|
}),
|
|
111
113
|
}
|
|
112
114
|
: {}),
|
|
@@ -198,6 +200,7 @@
|
|
|
198
200
|
const styles = getReactNativeBlockStyles({
|
|
199
201
|
block: useBlock(),
|
|
200
202
|
context: context,
|
|
203
|
+
blockStyles: attributes().style,
|
|
201
204
|
});
|
|
202
205
|
return extractTextStyles(styles);
|
|
203
206
|
};
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
import { TARGET } from "../../constants/target.js";
|
|
54
54
|
import { evaluate } from "../../functions/evaluate.js";
|
|
55
55
|
import { getContent } from "../../functions/get-content/index.js";
|
|
56
|
-
import {
|
|
56
|
+
import { fetch } from "../../functions/get-fetch.js";
|
|
57
57
|
import { isBrowser } from "../../functions/is-browser.js";
|
|
58
58
|
import { isEditing } from "../../functions/is-editing.js";
|
|
59
59
|
import { isPreviewing } from "../../functions/is-previewing.js";
|
|
@@ -168,8 +168,7 @@
|
|
|
168
168
|
);
|
|
169
169
|
}
|
|
170
170
|
function handleRequest({ url, key }) {
|
|
171
|
-
|
|
172
|
-
.then((fetch) => fetch(url))
|
|
171
|
+
fetch(url)
|
|
173
172
|
.then((response) => response.json())
|
|
174
173
|
.then((json) => {
|
|
175
174
|
const newOverrideState = {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { flatten } from '../../helpers/flatten.js';
|
|
2
|
+
import {
|
|
3
|
+
getBuilderSearchParamsFromWindow,
|
|
4
|
+
normalizeSearchParams,
|
|
5
|
+
} from '../get-builder-search-params/index.js';
|
|
6
|
+
import type { GetContentOptions } from './types.js';
|
|
7
|
+
|
|
8
|
+
export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
9
|
+
const {
|
|
10
|
+
limit = 30,
|
|
11
|
+
userAttributes,
|
|
12
|
+
query,
|
|
13
|
+
noTraverse = false,
|
|
14
|
+
model,
|
|
15
|
+
apiKey,
|
|
16
|
+
includeRefs = true,
|
|
17
|
+
locale,
|
|
18
|
+
} = options;
|
|
19
|
+
|
|
20
|
+
if (!apiKey) {
|
|
21
|
+
throw new Error('Missing API key');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const url = new URL(
|
|
25
|
+
`https://cdn.builder.io/api/v2/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${
|
|
26
|
+
locale ? `&locale=${locale}` : ''
|
|
27
|
+
}`
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const queryOptions = {
|
|
31
|
+
...getBuilderSearchParamsFromWindow(),
|
|
32
|
+
...normalizeSearchParams(options.options || {}),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const flattened = flatten(queryOptions);
|
|
36
|
+
for (const key in flattened) {
|
|
37
|
+
url.searchParams.set(key, String(flattened[key]));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (userAttributes) {
|
|
41
|
+
url.searchParams.set('userAttributes', JSON.stringify(userAttributes));
|
|
42
|
+
}
|
|
43
|
+
if (query) {
|
|
44
|
+
const flattened = flatten({ query });
|
|
45
|
+
for (const key in flattened) {
|
|
46
|
+
url.searchParams.set(key, JSON.stringify((flattened as any)[key]));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return url;
|
|
51
|
+
};
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import { flatten } from '../../helpers/flatten.js';
|
|
2
1
|
import type { BuilderContent } from '../../types/builder-content.js';
|
|
3
|
-
import {
|
|
4
|
-
getBuilderSearchParamsFromWindow,
|
|
5
|
-
normalizeSearchParams,
|
|
6
|
-
} from '../get-builder-search-params/index.js';
|
|
7
|
-
import { getFetch } from '../get-fetch.js';
|
|
2
|
+
import { fetch } from '../get-fetch.js';
|
|
8
3
|
import { handleABTesting } from './ab-testing.js';
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
import { generateContentUrl } from './generate-content-url.js';
|
|
5
|
+
import type { GetContentOptions } from './types.js';
|
|
11
6
|
|
|
12
7
|
export async function getContent(
|
|
13
8
|
options: GetContentOptions
|
|
@@ -15,51 +10,6 @@ export async function getContent(
|
|
|
15
10
|
return (await getAllContent({ ...options, limit: 1 })).results[0] || null;
|
|
16
11
|
}
|
|
17
12
|
|
|
18
|
-
export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
19
|
-
const {
|
|
20
|
-
limit = 30,
|
|
21
|
-
userAttributes,
|
|
22
|
-
query,
|
|
23
|
-
noTraverse = false,
|
|
24
|
-
model,
|
|
25
|
-
apiKey,
|
|
26
|
-
includeRefs = true,
|
|
27
|
-
locale,
|
|
28
|
-
} = options;
|
|
29
|
-
|
|
30
|
-
if (!apiKey) {
|
|
31
|
-
throw new Error('Missing API key');
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const url = new URL(
|
|
35
|
-
`https://cdn.builder.io/api/v2/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${
|
|
36
|
-
locale ? `&locale=${locale}` : ''
|
|
37
|
-
}`
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
const queryOptions = {
|
|
41
|
-
...getBuilderSearchParamsFromWindow(),
|
|
42
|
-
...normalizeSearchParams(options.options || {}),
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const flattened = flatten(queryOptions);
|
|
46
|
-
for (const key in flattened) {
|
|
47
|
-
url.searchParams.set(key, String(flattened[key]));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (userAttributes) {
|
|
51
|
-
url.searchParams.set('userAttributes', JSON.stringify(userAttributes));
|
|
52
|
-
}
|
|
53
|
-
if (query) {
|
|
54
|
-
const flattened = flatten({ query });
|
|
55
|
-
for (const key in flattened) {
|
|
56
|
-
url.searchParams.set(key, JSON.stringify((flattened as any)[key]));
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return url;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
13
|
/**
|
|
64
14
|
* TO-DO: Handle error responses.
|
|
65
15
|
*/
|
|
@@ -72,10 +22,8 @@ export async function getAllContent(
|
|
|
72
22
|
): Promise<ContentResponse> {
|
|
73
23
|
const url = generateContentUrl(options);
|
|
74
24
|
|
|
75
|
-
const
|
|
76
|
-
const content
|
|
77
|
-
res.json()
|
|
78
|
-
);
|
|
25
|
+
const res = await fetch(url.href);
|
|
26
|
+
const content = await (res.json() as Promise<ContentResponse>);
|
|
79
27
|
|
|
80
28
|
const canTrack = options.canTrack !== false;
|
|
81
29
|
if (
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { getGlobalThis } from './get-global-this.js';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
function getFetch(): typeof global.fetch {
|
|
4
4
|
const globalFetch: typeof global.fetch = getGlobalThis().fetch;
|
|
5
5
|
|
|
6
|
-
if (typeof globalFetch === 'undefined'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
if (typeof globalFetch === 'undefined') {
|
|
7
|
+
console.warn(
|
|
8
|
+
`Builder SDK could not find a global fetch function. Make sure you have a polyfill for fetch in your project.
|
|
9
|
+
For more information, read https://github.com/BuilderIO/this-package-uses-fetch`
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
throw new Error('Builder SDK could not find a global `fetch` function');
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
return
|
|
15
|
+
return globalFetch;
|
|
14
16
|
}
|
|
17
|
+
|
|
18
|
+
export const fetch = getFetch();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function getGlobalThis():
|
|
1
|
+
export function getGlobalThis(): typeof global {
|
|
2
2
|
if (typeof globalThis !== 'undefined') {
|
|
3
3
|
return globalThis;
|
|
4
4
|
}
|
|
@@ -11,5 +11,5 @@ export function getGlobalThis(): any {
|
|
|
11
11
|
if (typeof self !== 'undefined') {
|
|
12
12
|
return self;
|
|
13
13
|
}
|
|
14
|
-
return
|
|
14
|
+
return globalThis;
|
|
15
15
|
}
|
|
@@ -5,9 +5,11 @@ import { sanitizeReactNativeBlockStyles } from './sanitize-react-native-block-st
|
|
|
5
5
|
export function getReactNativeBlockStyles({
|
|
6
6
|
block,
|
|
7
7
|
context,
|
|
8
|
+
blockStyles,
|
|
8
9
|
}: {
|
|
9
10
|
block: BuilderBlock;
|
|
10
11
|
context: BuilderContextInterface;
|
|
12
|
+
blockStyles: any;
|
|
11
13
|
}): CSSStyleDeclaration | Record<string, string | undefined> {
|
|
12
14
|
const responsiveStyles = block.responsiveStyles;
|
|
13
15
|
if (!responsiveStyles) {
|
|
@@ -20,6 +22,7 @@ export function getReactNativeBlockStyles({
|
|
|
20
22
|
...(responsiveStyles.large || {}),
|
|
21
23
|
...(responsiveStyles.medium || {}),
|
|
22
24
|
...(responsiveStyles.small || {}),
|
|
25
|
+
...blockStyles,
|
|
23
26
|
} as Record<string, string | number>;
|
|
24
27
|
|
|
25
28
|
const newStyles = sanitizeReactNativeBlockStyles(
|