@builder.io/sdk-svelte 0.0.11-0 โ†’ 0.1.2

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.0.10",
4
+ "version": "0.1.1",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "package",
@@ -1,3 +1,7 @@
1
+ ### 0.1.0
2
+
3
+ ๐Ÿงจ Breaking change: we no longer provide a `node-fetch` polyfill. See [the docs](./README.md#fetch) for more information.
4
+
1
5
  ### 0.0.1-9
2
6
 
3
7
  ๐Ÿ› Fix: custom components were not rendering correctly
@@ -15,6 +15,7 @@ import RenderBlocks from "../render-blocks.svelte";
15
15
  import RenderContentStyles from "./components/render-styles.svelte";
16
16
  import BuilderContext from "../../context/builder.context.js";
17
17
  import { registerInsertMenu, setupBrowserForEditing, } from "../../scripts/init-editing.js";
18
+ import { checkIsDefined } from "../../helpers/nullable.js";
18
19
  export let content;
19
20
  export let data;
20
21
  export let canTrack;
@@ -167,7 +168,7 @@ $: useContent = () => {
167
168
  return mergedContent;
168
169
  };
169
170
  $: canTrackToUse = () => {
170
- return canTrack || true;
171
+ return checkIsDefined(canTrack) ? canTrack : true;
171
172
  };
172
173
  $: contentState = () => {
173
174
  return {
@@ -5,4 +5,4 @@ export declare const getContentVariationCookie: ({ contentId, canTrack, }: {
5
5
  export declare const setContentVariationCookie: ({ contentId, canTrack, value, }: {
6
6
  contentId: string;
7
7
  value: string;
8
- } & CanTrack) => Promise<undefined>;
8
+ } & CanTrack) => Promise<void>;
@@ -12,4 +12,4 @@ export declare const setCookie: ({ name, value, expires, canTrack, }: {
12
12
  name: string;
13
13
  value: string;
14
14
  expires?: Date | undefined;
15
- } & CanTrack) => Promise<undefined>;
15
+ } & CanTrack) => Promise<void>;
@@ -1,4 +1,5 @@
1
1
  import { isBrowser } from '../functions/is-browser.js';
2
+ import { checkIsDefined } from './nullable.js';
2
3
  import { getTopLevelDomain } from './url.js';
3
4
  /**
4
5
  * NOTE: This function is `async` because its react-native override is async. Do not remove the `async` keyword!
@@ -19,9 +20,13 @@ export const getCookie = async ({ name, canTrack, }) => {
19
20
  }
20
21
  catch (err) {
21
22
  console.debug('[COOKIE] GET error: ', err);
23
+ return undefined;
22
24
  }
23
25
  };
24
- const stringifyCookie = (cookie) => cookie.map(([key, value]) => (value ? `${key}=${value}` : key)).join('; ');
26
+ const stringifyCookie = (cookie) => cookie
27
+ .map(([key, value]) => (value ? `${key}=${value}` : key))
28
+ .filter(checkIsDefined)
29
+ .join('; ');
25
30
  const SECURE_CONFIG = [
26
31
  ['secure', ''],
27
32
  ['SameSite', 'None'],
@@ -49,7 +54,7 @@ const createCookieString = ({ name, value, expires, }) => {
49
54
  export const setCookie = async ({ name, value, expires, canTrack, }) => {
50
55
  try {
51
56
  if (!canTrack) {
52
- return undefined;
57
+ return;
53
58
  }
54
59
  const cookie = createCookieString({ name, value, expires });
55
60
  document.cookie = cookie;
@@ -1,7 +1,8 @@
1
1
  import type { CanTrack } from '../types/can-track.js';
2
+ import type { Nullable } from './nullable.js';
2
3
  export declare const getLocalStorageItem: ({ key, canTrack, }: {
3
4
  key: string;
4
- } & CanTrack) => string | null | undefined;
5
+ } & CanTrack) => Nullable<string>;
5
6
  export declare const setLocalStorageItem: ({ key, canTrack, value, }: {
6
7
  key: string;
7
8
  value: string;
@@ -9,6 +9,7 @@ export const getLocalStorageItem = ({ key, canTrack, }) => {
9
9
  }
10
10
  catch (err) {
11
11
  console.debug('[LocalStorage] GET error: ', err);
12
+ return undefined;
12
13
  }
13
14
  };
14
15
  export const setLocalStorageItem = ({ key, canTrack, value, }) => {
@@ -1,6 +1,6 @@
1
1
  import type { CanTrack } from '../types/can-track.js';
2
- export declare const getSessionId: ({ canTrack }: CanTrack) => Promise<string | undefined>;
2
+ export declare const getSessionId: ({ canTrack, }: CanTrack) => Promise<string | undefined>;
3
3
  export declare const createSessionId: () => string;
4
4
  export declare const setSessionId: ({ id, canTrack, }: {
5
5
  id: string;
6
- } & CanTrack) => Promise<undefined>;
6
+ } & CanTrack) => Promise<void>;
@@ -2,7 +2,7 @@ import { getCookie, setCookie } from './cookie.js';
2
2
  import { checkIsDefined } from './nullable.js';
3
3
  import { uuid } from './uuid.js';
4
4
  const SESSION_LOCAL_STORAGE_KEY = 'builderSessionId';
5
- export const getSessionId = async ({ canTrack }) => {
5
+ export const getSessionId = async ({ canTrack, }) => {
6
6
  if (!canTrack) {
7
7
  return undefined;
8
8
  }
@@ -16,6 +16,7 @@ export const getSessionId = async ({ canTrack }) => {
16
16
  else {
17
17
  const newSessionId = createSessionId();
18
18
  setSessionId({ id: newSessionId, canTrack });
19
+ return newSessionId;
19
20
  }
20
21
  };
21
22
  export const createSessionId = () => uuid();
@@ -4,6 +4,9 @@
4
4
  * www.example.co.uk -> example.co.uk
5
5
  */
6
6
  export const getTopLevelDomain = (host) => {
7
+ if (host === 'localhost' || host === '127.0.0.1') {
8
+ return host;
9
+ }
7
10
  const parts = host.split('.');
8
11
  if (parts.length > 2) {
9
12
  return parts.slice(1).join('.');
@@ -12,4 +12,10 @@ describe('getTopLevelDomain', () => {
12
12
  const output = getTopLevelDomain('www.example.co.uk');
13
13
  expect(output).toBe('example.co.uk');
14
14
  });
15
+ test('handles localhost', () => {
16
+ const output = getTopLevelDomain('localhost');
17
+ expect(output).toBe('localhost');
18
+ const output2 = getTopLevelDomain('127.0.0.1');
19
+ expect(output2).toBe('127.0.0.1');
20
+ });
15
21
  });
@@ -16,6 +16,7 @@ export const getVisitorId = ({ canTrack }) => {
16
16
  else {
17
17
  const newVisitorId = createVisitorId();
18
18
  setVisitorId({ id: newVisitorId, canTrack });
19
+ return newVisitorId;
19
20
  }
20
21
  };
21
22
  export const createVisitorId = () => uuid();
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.11-0",
4
+ "version": "0.1.2",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "package",
@@ -28,8 +28,9 @@
28
28
  },
29
29
  "types": "./package/index.d.ts",
30
30
  "scripts": {
31
- "release:patch": "yarn run build && npm version patch --no-git-tag-version && npm publish --access public",
32
- "release:dev": "yarn run build && npm version prerelease --no-git-tag-version && npm publish --tag dev --access public",
31
+ "release:patch": "yarn run build && npm version patch && npm publish",
32
+ "release:minor": "yarn run build && npm version minor && npm publish",
33
+ "release:dev": "yarn run build && npm version prerelease && npm publish --tag dev",
33
34
  "dev": "vite dev",
34
35
  "build": "svelte-kit sync && svelte-package",
35
36
  "build:watch": "svelte-kit sync && svelte-package --watch",
@@ -12,35 +12,37 @@ export const componentInfo: ComponentInfo = {
12
12
  required: true,
13
13
  defaultValue: '',
14
14
  helperText: 'e.g. enter a youtube url, google map, etc',
15
- onChange: serializeFn((options: Map<string, any>) => {
16
- const url = options.get('url');
17
- if (url) {
18
- options.set('content', 'Loading...');
19
- // TODO: get this out of here!
20
- const apiKey = 'ae0e60e78201a3f2b0de4b';
21
- return fetch(
22
- `https://iframe.ly/api/iframely?url=${url}&api_key=${apiKey}`
23
- )
24
- .then((res) => res.json())
25
- .then((data) => {
26
- if (options.get('url') === url) {
27
- if (data.html) {
28
- options.set('content', data.html);
29
- } else {
30
- options.set('content', 'Invalid url, please try another');
15
+ onChange: serializeFn(
16
+ (options: Map<string, any>): void | Promise<void> => {
17
+ const url = options.get('url');
18
+ if (url) {
19
+ options.set('content', 'Loading...');
20
+ // TODO: get this out of here!
21
+ const apiKey = 'ae0e60e78201a3f2b0de4b';
22
+ return fetch(
23
+ `https://iframe.ly/api/iframely?url=${url}&api_key=${apiKey}`
24
+ )
25
+ .then((res) => res.json())
26
+ .then((data) => {
27
+ if (options.get('url') === url) {
28
+ if (data.html) {
29
+ options.set('content', data.html);
30
+ } else {
31
+ options.set('content', 'Invalid url, please try another');
32
+ }
31
33
  }
32
- }
33
- })
34
- .catch((_err) => {
35
- options.set(
36
- 'content',
37
- 'There was an error embedding this URL, please try again or another URL'
38
- );
39
- });
40
- } else {
41
- options.delete('content');
34
+ })
35
+ .catch((_err) => {
36
+ options.set(
37
+ 'content',
38
+ 'There was an error embedding this URL, please try again or another URL'
39
+ );
40
+ });
41
+ } else {
42
+ options.delete('content');
43
+ }
42
44
  }
43
- }),
45
+ ),
44
46
  },
45
47
  {
46
48
  name: 'content',
@@ -23,69 +23,71 @@ export const componentInfo: ComponentInfo = {
23
23
  required: true,
24
24
  defaultValue:
25
25
  'https://cdn.builder.io/api/v1/image/assets%2Fpwgjf0RoYWbdnJSbpBAjXNRMe9F2%2Ffb27a7c790324294af8be1c35fe30f4d',
26
- onChange: serializeFn((options: Map<string, any>) => {
27
- const DEFAULT_ASPECT_RATIO = 0.7041;
28
- options.delete('srcset');
29
- options.delete('noWebp');
30
- function loadImage(
31
- url: string,
32
- timeout = 60000
33
- ): Promise<HTMLImageElement> {
34
- return new Promise((resolve, reject) => {
35
- const img = document.createElement('img');
36
- let loaded = false;
37
- img.onload = () => {
38
- loaded = true;
39
- resolve(img);
40
- };
26
+ onChange: serializeFn(
27
+ (options: Map<string, any>): void | Promise<void> => {
28
+ const DEFAULT_ASPECT_RATIO = 0.7041;
29
+ options.delete('srcset');
30
+ options.delete('noWebp');
31
+ function loadImage(
32
+ url: string,
33
+ timeout = 60000
34
+ ): Promise<HTMLImageElement> {
35
+ return new Promise((resolve, reject) => {
36
+ const img = document.createElement('img');
37
+ let loaded = false;
38
+ img.onload = () => {
39
+ loaded = true;
40
+ resolve(img);
41
+ };
41
42
 
42
- img.addEventListener('error', (event) => {
43
- console.warn('Image load failed', event.error);
44
- reject(event.error);
45
- });
43
+ img.addEventListener('error', (event) => {
44
+ console.warn('Image load failed', event.error);
45
+ reject(event.error);
46
+ });
46
47
 
47
- img.src = url;
48
- setTimeout(() => {
49
- if (!loaded) {
50
- reject(new Error('Image load timed out'));
51
- }
52
- }, timeout);
53
- });
54
- }
48
+ img.src = url;
49
+ setTimeout(() => {
50
+ if (!loaded) {
51
+ reject(new Error('Image load timed out'));
52
+ }
53
+ }, timeout);
54
+ });
55
+ }
55
56
 
56
- function round(num: number) {
57
- return Math.round(num * 1000) / 1000;
58
- }
57
+ function round(num: number) {
58
+ return Math.round(num * 1000) / 1000;
59
+ }
59
60
 
60
- const value = options.get('image');
61
- const aspectRatio = options.get('aspectRatio');
61
+ const value = options.get('image');
62
+ const aspectRatio = options.get('aspectRatio');
62
63
 
63
- // For SVG images - don't render as webp, keep them as SVG
64
- fetch(value)
65
- .then((res) => res.blob())
66
- .then((blob) => {
67
- if (blob.type.includes('svg')) {
68
- options.set('noWebp', true);
69
- }
70
- });
64
+ // For SVG images - don't render as webp, keep them as SVG
65
+ fetch(value)
66
+ .then((res) => res.blob())
67
+ .then((blob) => {
68
+ if (blob.type.includes('svg')) {
69
+ options.set('noWebp', true);
70
+ }
71
+ });
71
72
 
72
- if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) {
73
- return loadImage(value).then((img) => {
74
- const possiblyUpdatedAspectRatio = options.get('aspectRatio');
75
- if (
76
- options.get('image') === value &&
77
- (!possiblyUpdatedAspectRatio ||
78
- possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO)
79
- ) {
80
- if (img.width && img.height) {
81
- options.set('aspectRatio', round(img.height / img.width));
82
- options.set('height', img.height);
83
- options.set('width', img.width);
73
+ if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) {
74
+ return loadImage(value).then((img) => {
75
+ const possiblyUpdatedAspectRatio = options.get('aspectRatio');
76
+ if (
77
+ options.get('image') === value &&
78
+ (!possiblyUpdatedAspectRatio ||
79
+ possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO)
80
+ ) {
81
+ if (img.width && img.height) {
82
+ options.set('aspectRatio', round(img.height / img.width));
83
+ options.set('height', img.height);
84
+ options.set('width', img.width);
85
+ }
84
86
  }
85
- }
86
- });
87
+ });
88
+ }
87
89
  }
88
- }),
90
+ ),
89
91
  },
90
92
  {
91
93
  name: 'backgroundSize',
@@ -69,6 +69,7 @@
69
69
  registerInsertMenu,
70
70
  setupBrowserForEditing,
71
71
  } from "../../scripts/init-editing.js";
72
+ import { checkIsDefined } from "../../helpers/nullable.js";
72
73
 
73
74
  export let content: RenderContentProps["content"];
74
75
  export let data: RenderContentProps["data"];
@@ -231,7 +232,7 @@
231
232
  return mergedContent;
232
233
  };
233
234
  $: canTrackToUse = () => {
234
- return canTrack || true;
235
+ return checkIsDefined(canTrack) ? canTrack : true;
235
236
  };
236
237
  $: contentState = () => {
237
238
  return {
@@ -1,5 +1,6 @@
1
1
  import { isBrowser } from '../functions/is-browser.js';
2
2
  import type { CanTrack } from '../types/can-track.js';
3
+ import { checkIsDefined } from './nullable.js';
3
4
  import { getTopLevelDomain } from './url.js';
4
5
 
5
6
  /**
@@ -10,7 +11,7 @@ export const getCookie = async ({
10
11
  canTrack,
11
12
  }: {
12
13
  name: string;
13
- } & CanTrack) => {
14
+ } & CanTrack): Promise<string | undefined> => {
14
15
  try {
15
16
  if (!canTrack) {
16
17
  return undefined;
@@ -26,6 +27,7 @@ export const getCookie = async ({
26
27
  ?.split('=')[1];
27
28
  } catch (err) {
28
29
  console.debug('[COOKIE] GET error: ', err);
30
+ return undefined;
29
31
  }
30
32
  };
31
33
 
@@ -40,7 +42,10 @@ type CookieConfiguration = Array<
40
42
  >;
41
43
 
42
44
  const stringifyCookie = (cookie: CookieConfiguration): string =>
43
- cookie.map(([key, value]) => (value ? `${key}=${value}` : key)).join('; ');
45
+ cookie
46
+ .map(([key, value]) => (value ? `${key}=${value}` : key))
47
+ .filter(checkIsDefined)
48
+ .join('; ');
44
49
 
45
50
  const SECURE_CONFIG: CookieConfiguration = [
46
51
  ['secure', ''],
@@ -89,10 +94,10 @@ export const setCookie = async ({
89
94
  name: string;
90
95
  value: string;
91
96
  expires?: Date;
92
- } & CanTrack) => {
97
+ } & CanTrack): Promise<void> => {
93
98
  try {
94
99
  if (!canTrack) {
95
- return undefined;
100
+ return;
96
101
  }
97
102
  const cookie = createCookieString({ name, value, expires });
98
103
  document.cookie = cookie;
@@ -1,5 +1,6 @@
1
1
  import { isBrowser } from '../functions/is-browser.js';
2
2
  import type { CanTrack } from '../types/can-track.js';
3
+ import type { Nullable } from './nullable.js';
3
4
 
4
5
  const getLocalStorage = () =>
5
6
  isBrowser() && typeof localStorage !== 'undefined' ? localStorage : undefined;
@@ -9,7 +10,7 @@ export const getLocalStorageItem = ({
9
10
  canTrack,
10
11
  }: {
11
12
  key: string;
12
- } & CanTrack) => {
13
+ } & CanTrack): Nullable<string> => {
13
14
  try {
14
15
  if (canTrack) {
15
16
  return getLocalStorage()?.getItem(key);
@@ -17,6 +18,7 @@ export const getLocalStorageItem = ({
17
18
  return undefined;
18
19
  } catch (err) {
19
20
  console.debug('[LocalStorage] GET error: ', err);
21
+ return undefined;
20
22
  }
21
23
  };
22
24
 
@@ -5,7 +5,9 @@ import { uuid } from './uuid.js';
5
5
 
6
6
  const SESSION_LOCAL_STORAGE_KEY = 'builderSessionId';
7
7
 
8
- export const getSessionId = async ({ canTrack }: CanTrack) => {
8
+ export const getSessionId = async ({
9
+ canTrack,
10
+ }: CanTrack): Promise<string | undefined> => {
9
11
  if (!canTrack) {
10
12
  return undefined;
11
13
  }
@@ -20,6 +22,8 @@ export const getSessionId = async ({ canTrack }: CanTrack) => {
20
22
  } else {
21
23
  const newSessionId = createSessionId();
22
24
  setSessionId({ id: newSessionId, canTrack });
25
+
26
+ return newSessionId;
23
27
  }
24
28
  };
25
29
 
@@ -13,4 +13,11 @@ describe('getTopLevelDomain', () => {
13
13
  const output = getTopLevelDomain('www.example.co.uk');
14
14
  expect(output).toBe('example.co.uk');
15
15
  });
16
+ test('handles localhost', () => {
17
+ const output = getTopLevelDomain('localhost');
18
+ expect(output).toBe('localhost');
19
+
20
+ const output2 = getTopLevelDomain('127.0.0.1');
21
+ expect(output2).toBe('127.0.0.1');
22
+ });
16
23
  });
@@ -4,6 +4,10 @@
4
4
  * www.example.co.uk -> example.co.uk
5
5
  */
6
6
  export const getTopLevelDomain = (host: string) => {
7
+ if (host === 'localhost' || host === '127.0.0.1') {
8
+ return host;
9
+ }
10
+
7
11
  const parts = host.split('.');
8
12
  if (parts.length > 2) {
9
13
  return parts.slice(1).join('.');
@@ -5,7 +5,7 @@ import { uuid } from './uuid.js';
5
5
 
6
6
  const VISITOR_LOCAL_STORAGE_KEY = 'builderVisitorId';
7
7
 
8
- export const getVisitorId = ({ canTrack }: CanTrack) => {
8
+ export const getVisitorId = ({ canTrack }: CanTrack): string | undefined => {
9
9
  if (!canTrack) {
10
10
  return undefined;
11
11
  }
@@ -20,6 +20,8 @@ export const getVisitorId = ({ canTrack }: CanTrack) => {
20
20
  } else {
21
21
  const newVisitorId = createVisitorId();
22
22
  setVisitorId({ id: newVisitorId, canTrack });
23
+
24
+ return newVisitorId;
23
25
  }
24
26
  };
25
27