@builder.io/sdk-svelte 0.0.10 → 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/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.json +1 -4
- 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/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.10
|
|
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.
|
|
@@ -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;
|
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
|
}
|
|
@@ -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
|
}
|