@builder.io/sdk-svelte 0.4.5 → 0.5.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.
- package/package/--package.json +41 -29
- package/package/CHANGELOG.md +8 -0
- package/package/blocks/BaseText.svelte +1 -1
- package/package/blocks/button/button.svelte +18 -33
- package/package/blocks/button/button.svelte.d.ts +3 -1
- package/package/blocks/button/component-info.d.ts +1 -1
- package/package/blocks/button/component-info.js +9 -13
- package/package/blocks/columns/columns.svelte +12 -18
- package/package/blocks/columns/columns.svelte.d.ts +11 -7
- package/package/blocks/columns/component-info.d.ts +1 -1
- package/package/blocks/columns/component-info.js +62 -83
- package/package/blocks/custom-code/component-info.d.ts +1 -1
- package/package/blocks/custom-code/component-info.js +7 -11
- package/package/blocks/embed/component-info.d.ts +1 -1
- package/package/blocks/embed/component-info.js +8 -15
- package/package/blocks/embed/helpers.js +1 -5
- package/package/blocks/form/component-info.d.ts +1 -1
- package/package/blocks/form/component-info.js +94 -129
- package/package/blocks/form/form.svelte +2 -289
- package/package/blocks/form/form.svelte.d.ts +1 -57
- package/package/blocks/fragment/component-info.d.ts +1 -1
- package/package/blocks/fragment/component-info.js +1 -1
- package/package/blocks/helpers.d.ts +13 -0
- package/package/blocks/helpers.js +40 -0
- package/package/blocks/image/component-info.d.ts +1 -1
- package/package/blocks/image/component-info.js +33 -63
- package/package/blocks/image/image.helpers.js +2 -11
- package/package/blocks/image/image.svelte.d.ts +0 -1
- package/package/blocks/img/component-info.d.ts +1 -1
- package/package/blocks/img/component-info.js +4 -6
- package/package/blocks/img/img.svelte +4 -29
- package/package/blocks/img/img.svelte.d.ts +3 -1
- package/package/blocks/input/component-info.d.ts +1 -1
- package/package/blocks/input/component-info.js +15 -40
- package/package/blocks/input/input.svelte +4 -29
- package/package/blocks/input/input.svelte.d.ts +3 -1
- package/package/blocks/raw-text/component-info.d.ts +1 -1
- package/package/blocks/raw-text/component-info.js +3 -5
- package/package/blocks/raw-text/raw-text.svelte +3 -5
- package/package/blocks/raw-text/raw-text.svelte.d.ts +1 -1
- package/package/blocks/section/component-info.d.ts +1 -1
- package/package/blocks/section/component-info.js +13 -18
- package/package/blocks/section/section.svelte +4 -29
- package/package/blocks/section/section.svelte.d.ts +3 -1
- package/package/blocks/select/component-info.d.ts +1 -1
- package/package/blocks/select/component-info.js +22 -34
- package/package/blocks/select/select.svelte +4 -29
- package/package/blocks/select/select.svelte.d.ts +3 -1
- package/package/blocks/submit-button/component-info.d.ts +1 -1
- package/package/blocks/submit-button/component-info.js +5 -7
- package/package/blocks/submit-button/submit-button.svelte +4 -29
- package/package/blocks/submit-button/submit-button.svelte.d.ts +3 -1
- package/package/blocks/symbol/component-info.d.ts +1 -1
- package/package/blocks/symbol/component-info.js +12 -17
- package/package/blocks/symbol/symbol.helpers.d.ts +14 -0
- package/package/blocks/symbol/symbol.helpers.js +31 -0
- package/package/blocks/symbol/symbol.svelte +37 -87
- package/package/blocks/symbol/symbol.svelte.d.ts +13 -7
- package/package/blocks/text/component-info.d.ts +1 -1
- package/package/blocks/text/component-info.js +6 -7
- package/package/blocks/text/text.svelte +1 -1
- package/package/blocks/textarea/component-info.d.ts +1 -1
- package/package/blocks/textarea/component-info.js +13 -19
- package/package/blocks/textarea/textarea.svelte +4 -29
- package/package/blocks/textarea/textarea.svelte.d.ts +3 -1
- package/package/blocks/video/component-info.d.ts +1 -1
- package/package/blocks/video/component-info.js +35 -55
- package/package/blocks/video/video.svelte +4 -30
- package/package/blocks/video/video.svelte.d.ts +2 -0
- package/package/components/block/block.helpers.d.ts +13 -0
- package/package/components/{render-block/render-block.helpers.js → block/block.helpers.js} +12 -29
- package/package/components/block/block.svelte +135 -0
- package/package/components/block/block.svelte.d.ts +26 -0
- package/package/components/{render-block → block/components}/block-styles.svelte +20 -22
- package/package/components/{render-block → block/components}/block-styles.svelte.d.ts +2 -2
- package/package/components/block/components/block-wrapper.svelte +53 -0
- package/package/components/block/components/block-wrapper.svelte.d.ts +33 -0
- package/package/components/block/components/component-ref/component-ref.helpers.d.ts +36 -0
- package/package/components/block/components/component-ref/component-ref.helpers.js +22 -0
- package/package/components/block/components/component-ref/component-ref.svelte +37 -0
- package/package/components/block/components/component-ref/component-ref.svelte.d.ts +24 -0
- package/package/components/block/components/interactive-element.svelte +29 -0
- package/package/components/block/components/interactive-element.svelte.d.ts +31 -0
- package/package/components/{render-block/render-repeated-block.svelte → block/components/repeated-block.svelte} +4 -3
- package/package/components/block/components/repeated-block.svelte.d.ts +25 -0
- package/package/components/{render-blocks.svelte → blocks/blocks-wrapper.svelte} +3 -20
- package/package/components/blocks/blocks-wrapper.svelte.d.ts +29 -0
- package/package/components/blocks/blocks.svelte +36 -0
- package/package/components/blocks/blocks.svelte.d.ts +28 -0
- package/package/components/{render-content/render-content.svelte → content/components/enable-editor.svelte} +47 -166
- package/package/components/content/components/enable-editor.svelte.d.ts +36 -0
- package/package/components/{render-content/components/render-styles.helpers.d.ts → content/components/styles.helpers.d.ts} +1 -1
- package/package/components/{render-content/components/render-styles.helpers.js → content/components/styles.helpers.js} +3 -4
- package/package/components/{render-content/components/render-styles.svelte → content/components/styles.svelte} +2 -2
- package/package/components/{render-content/components/render-styles.svelte.d.ts → content/components/styles.svelte.d.ts} +5 -5
- package/package/components/content/content.helpers.d.ts +7 -0
- package/package/components/content/content.helpers.js +30 -0
- package/package/components/content/content.svelte +118 -0
- package/package/components/content/content.svelte.d.ts +30 -0
- package/package/components/{render-content/render-content.types.d.ts → content/content.types.d.ts} +4 -4
- package/package/components/content/index.d.ts +1 -0
- package/package/components/content/index.js +1 -0
- package/package/components/{render-content-variants/render-content-variants.svelte → content-variants/content-variants.svelte} +20 -19
- package/package/components/{render-content-variants/render-content-variants.svelte.d.ts → content-variants/content-variants.svelte.d.ts} +6 -6
- package/package/components/{render-content-variants/render-content-variants.types.d.ts → content-variants/content-variants.types.d.ts} +5 -5
- package/package/components/{render-content-variants → content-variants}/helpers.d.ts +7 -7
- package/package/components/{render-content-variants → content-variants}/helpers.js +13 -22
- package/package/constants/builder-registered-components.js +34 -13
- package/package/constants/device-sizes.js +9 -8
- package/package/constants/sdk-version.d.ts +1 -1
- package/package/constants/sdk-version.js +1 -1
- package/package/constants/target.d.ts +1 -1
- package/package/context/builder.context.d.ts +1 -1
- package/package/context/builder.context.js +1 -1
- package/package/context/components.context.d.ts +7 -0
- package/package/context/components.context.js +5 -0
- package/package/context/types.d.ts +1 -1
- package/package/functions/apply-patch-with-mutation.d.ts +10 -0
- package/package/functions/apply-patch-with-mutation.js +54 -0
- package/package/functions/evaluate/acorn.d.ts +0 -0
- package/package/functions/evaluate/acorn.js +2433 -0
- package/package/functions/{evaluate.d.ts → evaluate/evaluate.d.ts} +5 -2
- package/package/functions/{evaluate.js → evaluate/evaluate.js} +34 -13
- package/package/functions/evaluate/index.d.ts +1 -0
- package/package/functions/evaluate/index.js +1 -0
- package/package/functions/evaluate/interpreter.d.ts +2 -0
- package/package/functions/evaluate/interpreter.js +3853 -0
- package/package/functions/evaluate/non-node-runtime.d.ts +2 -0
- package/package/functions/evaluate/non-node-runtime.js +84 -0
- package/package/functions/evaluate/types.d.ts +10 -0
- package/package/functions/extract-text-styles.js +2 -14
- package/package/functions/get-block-actions-handler.js +3 -3
- package/package/functions/get-block-actions.d.ts +1 -0
- package/package/functions/get-block-actions.js +14 -1
- package/package/functions/get-block-component-options.js +1 -1
- package/package/functions/get-block-properties.d.ts +5 -1
- package/package/functions/get-block-properties.js +15 -9
- package/package/functions/get-builder-search-params/index.js +3 -7
- package/package/functions/get-content/generate-content-url.js +7 -5
- package/package/functions/get-content/index.d.ts +2 -6
- package/package/functions/get-content/index.js +26 -6
- package/package/functions/get-processed-block.d.ts +1 -1
- package/package/functions/get-processed-block.js +11 -7
- package/package/functions/get-react-native-block-styles.d.ts +1 -1
- package/package/functions/get-react-native-block-styles.js +2 -2
- package/package/functions/if-target.d.ts +1 -1
- package/package/functions/if-target.js +1 -1
- package/package/functions/is-editing.js +1 -3
- package/package/functions/is-non-node-server.d.ts +4 -0
- package/package/functions/is-non-node-server.js +8 -0
- package/package/functions/on-change.js +1 -1
- package/package/functions/register-component.d.ts +2 -1
- package/package/functions/register-component.js +20 -8
- package/package/functions/register.js +2 -2
- package/package/functions/sanitize-react-native-block-styles.js +13 -5
- package/package/functions/set-editor-settings.js +1 -1
- package/package/functions/set.js +2 -9
- package/package/functions/track/helpers.js +6 -11
- package/package/functions/track/index.js +26 -14
- package/package/functions/track/interaction.js +15 -16
- package/package/helpers/ab-tests.d.ts +2 -2
- package/package/helpers/ab-tests.js +42 -30
- package/package/helpers/canTrack.js +1 -1
- package/package/helpers/cookie.d.ts +2 -2
- package/package/helpers/cookie.js +13 -26
- package/package/helpers/css.d.ts +1 -1
- package/package/helpers/css.js +1 -1
- package/package/helpers/flatten.js +8 -8
- package/package/helpers/localStorage.d.ts +2 -2
- package/package/helpers/localStorage.js +2 -2
- package/package/helpers/logger.js +1 -1
- package/package/helpers/preview-lru-cache/get.d.ts +1 -0
- package/package/helpers/preview-lru-cache/get.js +7 -0
- package/package/helpers/preview-lru-cache/helpers.d.ts +1 -0
- package/package/helpers/preview-lru-cache/helpers.js +9 -0
- package/package/helpers/preview-lru-cache/init.d.ts +6 -0
- package/package/helpers/preview-lru-cache/init.js +15 -0
- package/package/helpers/preview-lru-cache/set.d.ts +7 -0
- package/package/helpers/preview-lru-cache/set.js +9 -0
- package/package/helpers/preview-lru-cache/types.d.ts +12 -0
- package/package/helpers/preview-lru-cache/types.js +6 -0
- package/package/helpers/sessionId.d.ts +2 -2
- package/package/helpers/sessionId.js +11 -4
- package/package/helpers/uuid.js +1 -1
- package/package/helpers/visitorId.d.ts +1 -1
- package/package/helpers/visitorId.js +7 -4
- package/package/index-helpers/blocks-exports.d.ts +2 -2
- package/package/index-helpers/blocks-exports.js +2 -2
- package/package/index.d.ts +2 -2
- package/package/index.js +1 -1
- package/package/scripts/init-editing.js +65 -57
- package/package/types/builder-props.d.ts +10 -0
- package/package/types/components.d.ts +8 -2
- package/package/types/enforced-partials.d.ts +1 -1
- package/package/types/targets.d.ts +1 -3
- package/package/types/typescript.d.ts +3 -0
- package/package.json +2 -2
- package/src/blocks/BaseText.svelte +1 -1
- package/src/blocks/button/button.svelte +21 -34
- package/src/blocks/button/component-info.ts +20 -27
- package/src/blocks/columns/columns.svelte +26 -30
- package/src/blocks/columns/component-info.ts +205 -240
- package/src/blocks/custom-code/component-info.ts +20 -27
- package/src/blocks/embed/component-info.ts +34 -51
- package/src/blocks/embed/helpers.ts +2 -8
- package/src/blocks/form/component-info.ts +204 -253
- package/src/blocks/form/form.svelte +1 -368
- package/src/blocks/fragment/component-info.ts +3 -5
- package/src/blocks/helpers.ts +43 -0
- package/src/blocks/image/component-info.ts +113 -161
- package/src/blocks/image/image.helpers.ts +5 -33
- package/src/blocks/img/component-info.ts +11 -16
- package/src/blocks/img/img.svelte +7 -31
- package/src/blocks/input/component-info.ts +35 -66
- package/src/blocks/input/input.svelte +7 -31
- package/src/blocks/raw-text/component-info.ts +8 -12
- package/src/blocks/raw-text/raw-text.svelte +2 -5
- package/src/blocks/section/component-info.ts +28 -37
- package/src/blocks/section/section.svelte +7 -30
- package/src/blocks/select/component-info.ts +38 -55
- package/src/blocks/select/select.svelte +7 -31
- package/src/blocks/submit-button/component-info.ts +10 -15
- package/src/blocks/submit-button/submit-button.svelte +7 -30
- package/src/blocks/symbol/component-info.ts +31 -39
- package/src/blocks/symbol/symbol.helpers.ts +47 -0
- package/src/blocks/symbol/symbol.svelte +50 -97
- package/src/blocks/text/component-info.ts +14 -18
- package/src/blocks/text/text.svelte +1 -1
- package/src/blocks/textarea/component-info.ts +26 -36
- package/src/blocks/textarea/textarea.svelte +7 -30
- package/src/blocks/video/component-info.ts +77 -103
- package/src/blocks/video/video.svelte +4 -31
- package/src/components/{render-block/render-block.helpers.ts → block/block.helpers.ts} +22 -49
- package/src/components/block/block.svelte +158 -0
- package/src/components/{render-block → block/components}/block-styles.svelte +23 -24
- package/src/components/block/components/block-wrapper.svelte +70 -0
- package/src/components/block/components/component-ref/component-ref.helpers.ts +53 -0
- package/src/components/block/components/component-ref/component-ref.svelte +41 -0
- package/src/components/block/components/interactive-element.svelte +42 -0
- package/src/components/block/components/repeated-block.svelte +30 -0
- package/src/components/{render-block → block}/types.ts +1 -2
- package/src/components/{render-blocks.svelte → blocks/blocks-wrapper.svelte} +14 -32
- package/src/components/blocks/blocks.svelte +51 -0
- package/src/components/{render-content/render-content.svelte → content/components/enable-editor.svelte} +71 -199
- package/src/components/{render-content/components/render-styles.helpers.ts → content/components/styles.helpers.ts} +9 -11
- package/src/components/{render-content/components/render-styles.svelte → content/components/styles.svelte} +3 -3
- package/src/components/content/content.helpers.ts +42 -0
- package/src/components/content/content.svelte +144 -0
- package/src/components/{render-content/render-content.types.ts → content/content.types.ts} +5 -9
- package/src/components/content/index.ts +1 -0
- package/src/components/{render-content → content}/wrap-component-ref.ts +1 -1
- package/src/components/{render-content-variants/render-content-variants.svelte → content-variants/content-variants.svelte} +26 -24
- package/src/components/{render-content-variants/render-content-variants.types.ts → content-variants/content-variants.types.ts} +9 -11
- package/src/components/{render-content-variants → content-variants}/helpers.ts +27 -73
- package/src/constants/builder-registered-components.ts +34 -14
- package/src/constants/device-sizes.ts +16 -22
- package/src/constants/sdk-version.ts +1 -1
- package/src/constants/target.ts +3 -1
- package/src/context/builder.context.ts +1 -1
- package/src/context/components.context.ts +6 -0
- package/src/context/types.ts +2 -7
- package/src/functions/apply-patch-with-mutation.ts +61 -0
- package/src/functions/camel-to-kebab-case.ts +1 -2
- package/src/functions/evaluate/acorn.ts +2501 -0
- package/src/functions/evaluate/evaluate.ts +90 -0
- package/src/functions/evaluate/index.ts +1 -0
- package/src/functions/evaluate/interpreter.ts +3887 -0
- package/src/functions/evaluate/non-node-runtime.ts +94 -0
- package/src/functions/evaluate/types.ts +10 -0
- package/src/functions/extract-text-styles.ts +3 -18
- package/src/functions/fast-clone.ts +1 -2
- package/src/functions/get-block-actions-handler.ts +11 -20
- package/src/functions/get-block-actions.ts +22 -14
- package/src/functions/get-block-component-options.ts +2 -3
- package/src/functions/get-block-properties.ts +24 -17
- package/src/functions/get-builder-search-params/index.ts +5 -21
- package/src/functions/get-content/generate-content-url.ts +10 -25
- package/src/functions/get-content/index.ts +34 -37
- package/src/functions/get-content/types.ts +4 -2
- package/src/functions/get-fetch.ts +3 -10
- package/src/functions/get-global-this.ts +1 -2
- package/src/functions/get-processed-block.ts +14 -21
- package/src/functions/get-react-native-block-styles.ts +6 -12
- package/src/functions/if-target.ts +2 -3
- package/src/functions/is-browser.ts +1 -1
- package/src/functions/is-editing.ts +2 -7
- package/src/functions/is-iframe.ts +1 -2
- package/src/functions/is-non-node-server.ts +9 -0
- package/src/functions/is-previewing.ts +1 -4
- package/src/functions/on-change.ts +2 -2
- package/src/functions/register-component.ts +24 -26
- package/src/functions/register.ts +6 -8
- package/src/functions/sanitize-react-native-block-styles.ts +17 -24
- package/src/functions/set-editor-settings.ts +5 -6
- package/src/functions/set.ts +3 -15
- package/src/functions/track/helpers.ts +8 -24
- package/src/functions/track/index.ts +37 -42
- package/src/functions/track/interaction.ts +21 -38
- package/src/functions/transform-block-properties.ts +1 -1
- package/src/functions/transform-block.ts +1 -1
- package/src/helpers/ab-tests.ts +64 -79
- package/src/helpers/canTrack.ts +2 -4
- package/src/helpers/cookie.ts +15 -46
- package/src/helpers/css.ts +4 -13
- package/src/helpers/flatten.ts +11 -16
- package/src/helpers/localStorage.ts +4 -8
- package/src/helpers/logger.ts +2 -2
- package/src/helpers/nullable.ts +1 -3
- package/src/helpers/preview-lru-cache/get.ts +8 -0
- package/src/helpers/preview-lru-cache/helpers.ts +9 -0
- package/src/helpers/preview-lru-cache/init.ts +17 -0
- package/src/helpers/preview-lru-cache/set.ts +18 -0
- package/src/helpers/preview-lru-cache/types.ts +12 -0
- package/src/helpers/sessionId.ts +12 -13
- package/src/helpers/time.ts +1 -3
- package/src/helpers/url.ts +1 -2
- package/src/helpers/uuid.ts +3 -3
- package/src/helpers/visitorId.ts +14 -17
- package/src/index-helpers/blocks-exports.ts +2 -2
- package/src/index-helpers/top-of-file.ts +1 -1
- package/src/index.ts +2 -13
- package/src/scripts/init-editing.ts +85 -98
- package/src/types/api-version.ts +1 -1
- package/src/types/builder-block.ts +1 -3
- package/src/types/builder-content.ts +7 -4
- package/src/types/builder-props.ts +10 -0
- package/src/types/can-track.ts +1 -1
- package/src/types/components.ts +17 -13
- package/src/types/deep-partial.ts +1 -7
- package/src/types/element.ts +1 -3
- package/src/types/enforced-partials.ts +6 -20
- package/src/types/input.ts +6 -9
- package/src/types/targets.ts +1 -6
- package/src/types/typescript.ts +9 -9
- package/package/blocks/util.d.ts +0 -4
- package/package/blocks/util.js +0 -13
- package/package/components/render-block/render-block.helpers.d.ts +0 -12
- package/package/components/render-block/render-block.svelte +0 -152
- package/package/components/render-block/render-block.svelte.d.ts +0 -24
- package/package/components/render-block/render-component.svelte +0 -21
- package/package/components/render-block/render-component.svelte.d.ts +0 -34
- package/package/components/render-block/render-repeated-block.svelte.d.ts +0 -23
- package/package/components/render-blocks.svelte.d.ts +0 -26
- package/package/components/render-content/builder-editing.svelte +0 -4
- package/package/components/render-content/builder-editing.svelte.d.ts +0 -14
- package/package/components/render-content/index.d.ts +0 -1
- package/package/components/render-content/index.js +0 -1
- package/package/components/render-content/render-content.helpers.d.ts +0 -7
- package/package/components/render-content/render-content.helpers.js +0 -30
- package/package/components/render-content/render-content.svelte.d.ts +0 -30
- package/package/functions/evaluate.test.js +0 -17
- package/package/functions/get-builder-search-params/fn.test.js +0 -13
- package/package/functions/get-content/generate-content-url.test.d.ts +0 -1
- package/package/functions/get-content/generate-content-url.test.js +0 -97
- package/package/functions/get-processed-block.test.d.ts +0 -1
- package/package/functions/get-processed-block.test.js +0 -33
- package/package/functions/on-change.test.d.ts +0 -1
- package/package/functions/on-change.test.js +0 -20
- package/package/functions/set.test.d.ts +0 -1
- package/package/functions/set.test.js +0 -16
- package/package/helpers/url.test.d.ts +0 -1
- package/package/helpers/url.test.js +0 -21
- package/src/blocks/util.ts +0 -18
- package/src/components/render-block/render-block.svelte +0 -173
- package/src/components/render-block/render-component.svelte +0 -40
- package/src/components/render-block/render-repeated-block.svelte +0 -25
- package/src/components/render-content/builder-editing.svelte +0 -4
- package/src/components/render-content/index.ts +0 -1
- package/src/components/render-content/render-content.helpers.ts +0 -48
- package/src/functions/evaluate.test.ts +0 -21
- package/src/functions/evaluate.ts +0 -99
- package/src/functions/get-builder-search-params/fn.test.ts +0 -19
- package/src/functions/get-content/generate-content-url.test.ts +0 -112
- package/src/functions/get-processed-block.test.ts +0 -35
- package/src/functions/on-change.test.ts +0 -23
- package/src/functions/set.test.ts +0 -19
- package/src/helpers/url.test.ts +0 -23
- /package/package/components/{render-block → block}/types.d.ts +0 -0
- /package/package/components/{render-block → block}/types.js +0 -0
- /package/package/components/{render-content/render-content.types.js → content/content.types.js} +0 -0
- /package/package/components/{render-content → content}/wrap-component-ref.d.ts +0 -0
- /package/package/components/{render-content → content}/wrap-component-ref.js +0 -0
- /package/package/components/{render-content-variants/render-content-variants.types.js → content-variants/content-variants.types.js} +0 -0
- /package/package/functions/{evaluate.test.d.ts → evaluate/types.js} +0 -0
- /package/package/{functions/get-builder-search-params/fn.test.d.ts → types/builder-props.js} +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { logger } from '../../helpers/logger';
|
|
2
|
+
import { set } from '../set';
|
|
3
|
+
import Interpreter from './interpreter.js';
|
|
4
|
+
import type { ExecutorArgs } from './types';
|
|
5
|
+
const processCode = (code: string) => {
|
|
6
|
+
return code.split('\n').map(line => {
|
|
7
|
+
const trimmed = line.trim();
|
|
8
|
+
|
|
9
|
+
// this async wrapper doesn't work in JS-interpreter, so we drop it.
|
|
10
|
+
if (line.includes('__awaiter')) return undefined;
|
|
11
|
+
|
|
12
|
+
// we find all state setter expressions and append a call to setRootState afterwards
|
|
13
|
+
const isStateSetter = trimmed.startsWith('state.');
|
|
14
|
+
if (!isStateSetter) return line;
|
|
15
|
+
const [lhs, rhs] = trimmed.split('=');
|
|
16
|
+
const setStr = lhs.replace('state.', '').trim();
|
|
17
|
+
const setExpr = `setRootState('${setStr}', ${rhs.trim()})`;
|
|
18
|
+
return `
|
|
19
|
+
${line}
|
|
20
|
+
${setExpr}
|
|
21
|
+
`;
|
|
22
|
+
}).filter(Boolean).join('\n');
|
|
23
|
+
};
|
|
24
|
+
const getJSONValName = (val: string) => val + 'JSON';
|
|
25
|
+
export const runInNonNode = ({
|
|
26
|
+
builder,
|
|
27
|
+
context,
|
|
28
|
+
event,
|
|
29
|
+
rootState,
|
|
30
|
+
localState,
|
|
31
|
+
rootSetState,
|
|
32
|
+
useCode
|
|
33
|
+
}: ExecutorArgs) => {
|
|
34
|
+
const state = {
|
|
35
|
+
...rootState,
|
|
36
|
+
...localState
|
|
37
|
+
};
|
|
38
|
+
const properties = {
|
|
39
|
+
state,
|
|
40
|
+
Builder: builder,
|
|
41
|
+
builder,
|
|
42
|
+
context,
|
|
43
|
+
event
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Deserialize all properties from JSON strings to JS objects
|
|
48
|
+
*/
|
|
49
|
+
const prependedCode = Object.keys(properties).map(key => `var ${key} = JSON.parse(${getJSONValName(key)});`).join('\n');
|
|
50
|
+
const cleanedCode = processCode(useCode);
|
|
51
|
+
if (cleanedCode === '') {
|
|
52
|
+
logger.warn('Skipping evaluation of empty code block.');
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const transformed = `
|
|
56
|
+
function theFunction() {
|
|
57
|
+
${prependedCode}
|
|
58
|
+
|
|
59
|
+
${cleanedCode}
|
|
60
|
+
}
|
|
61
|
+
theFunction();
|
|
62
|
+
`;
|
|
63
|
+
const setRootState = (prop: string, value: any) => {
|
|
64
|
+
const newState = set(state, prop, value);
|
|
65
|
+
rootSetState?.(newState);
|
|
66
|
+
};
|
|
67
|
+
const initFunc = function (interpreter: any, globalObject: any) {
|
|
68
|
+
/**
|
|
69
|
+
* serialize all function args to JSON strings
|
|
70
|
+
*/
|
|
71
|
+
Object.keys(properties).forEach(key => {
|
|
72
|
+
const val = properties[(key as keyof typeof properties)] || {};
|
|
73
|
+
const jsonVal = JSON.stringify(val);
|
|
74
|
+
interpreter.setProperty(globalObject, getJSONValName(key), jsonVal);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Add a JavaScript function "setRootState" to the interpreter's global object, that will be called whenever a
|
|
79
|
+
* state property is set. This function will update the state object.
|
|
80
|
+
*/
|
|
81
|
+
interpreter.setProperty(globalObject, 'setRootState', interpreter.createNativeFunction(setRootState));
|
|
82
|
+
};
|
|
83
|
+
try {
|
|
84
|
+
const myInterpreter = new Interpreter(transformed, initFunc);
|
|
85
|
+
myInterpreter.run();
|
|
86
|
+
const output = myInterpreter.pseudoToNative(myInterpreter.value);
|
|
87
|
+
return output;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
logger.warn('Custom code error in non-node runtime. SDK can only execute ES5 JavaScript.', {
|
|
90
|
+
e
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BuilderContextInterface } from '../../context/types';
|
|
2
|
+
export type ExecutorArgs = Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'> & {
|
|
3
|
+
useCode: string;
|
|
4
|
+
builder: {
|
|
5
|
+
isEditing: boolean | undefined;
|
|
6
|
+
isBrowser: boolean | undefined;
|
|
7
|
+
isServer: boolean | undefined;
|
|
8
|
+
};
|
|
9
|
+
event: Event | undefined;
|
|
10
|
+
}
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
const TEXT_STYLE_KEYS = [
|
|
2
|
-
'color',
|
|
3
|
-
'whiteSpace',
|
|
4
|
-
'direction',
|
|
5
|
-
'hyphens',
|
|
6
|
-
'overflowWrap',
|
|
7
|
-
];
|
|
1
|
+
const TEXT_STYLE_KEYS = ['color', 'whiteSpace', 'direction', 'hyphens', 'overflowWrap'];
|
|
8
2
|
|
|
9
3
|
/**
|
|
10
4
|
* Check if the key represent a CSS style property that applies to text
|
|
@@ -12,15 +6,7 @@ const TEXT_STYLE_KEYS = [
|
|
|
12
6
|
* https://developer.mozilla.org/en-US/docs/Learn/CSS/Styling_text/Fundamentals#summary
|
|
13
7
|
*/
|
|
14
8
|
const isTextStyle = (key: string) => {
|
|
15
|
-
return (
|
|
16
|
-
TEXT_STYLE_KEYS.includes(key) ||
|
|
17
|
-
key.startsWith('font') ||
|
|
18
|
-
key.startsWith('text') ||
|
|
19
|
-
key.startsWith('letter') ||
|
|
20
|
-
key.startsWith('line') ||
|
|
21
|
-
key.startsWith('word') ||
|
|
22
|
-
key.startsWith('writing')
|
|
23
|
-
);
|
|
9
|
+
return TEXT_STYLE_KEYS.includes(key) || key.startsWith('font') || key.startsWith('text') || key.startsWith('letter') || key.startsWith('line') || key.startsWith('word') || key.startsWith('writing');
|
|
24
10
|
};
|
|
25
11
|
|
|
26
12
|
/**
|
|
@@ -34,6 +20,5 @@ export const extractTextStyles = (styles: Partial<CSSStyleDeclaration>) => {
|
|
|
34
20
|
textStyles[key] = value;
|
|
35
21
|
}
|
|
36
22
|
});
|
|
37
|
-
|
|
38
23
|
return textStyles;
|
|
39
|
-
}
|
|
24
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* We need to serialize values to a string in case there are Proxy values, as is the case with SolidJS etc.
|
|
3
3
|
*/
|
|
4
|
-
export const fastClone = <T extends object
|
|
5
|
-
JSON.parse(JSON.stringify(obj));
|
|
4
|
+
export const fastClone = <T extends object,>(obj: T): T => JSON.parse(JSON.stringify(obj))
|
|
@@ -1,25 +1,16 @@
|
|
|
1
1
|
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
2
|
import type { BuilderBlock } from '../types/builder-block.js';
|
|
3
|
-
import { evaluate } from './evaluate
|
|
4
|
-
|
|
3
|
+
import { evaluate } from './evaluate';
|
|
5
4
|
type Options = {
|
|
6
5
|
block: BuilderBlock;
|
|
7
|
-
} & Pick<
|
|
8
|
-
BuilderContextInterface,
|
|
9
|
-
'localState' | 'context' | 'rootState' | 'rootSetState'
|
|
10
|
-
>;
|
|
11
|
-
|
|
6
|
+
} & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>;
|
|
12
7
|
type EventHandler = (event: Event) => any;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
rootSetState: options.rootSetState,
|
|
23
|
-
event,
|
|
24
|
-
isExpression: false,
|
|
25
|
-
});
|
|
8
|
+
export const createEventHandler = (value: string, options: Options): EventHandler => event => evaluate({
|
|
9
|
+
code: value,
|
|
10
|
+
context: options.context,
|
|
11
|
+
localState: options.localState,
|
|
12
|
+
rootState: options.rootState,
|
|
13
|
+
rootSetState: options.rootSetState,
|
|
14
|
+
event,
|
|
15
|
+
isExpression: false
|
|
16
|
+
})
|
|
@@ -1,28 +1,36 @@
|
|
|
1
|
+
import { TARGET } from '../constants/target.js';
|
|
1
2
|
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
3
|
import type { BuilderBlock } from '../types/builder-block.js';
|
|
3
4
|
import { getEventHandlerName } from './event-handler-name.js';
|
|
4
5
|
import { createEventHandler } from './get-block-actions-handler.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export function getBlockActions(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
BuilderContextInterface,
|
|
13
|
-
'localState' | 'context' | 'rootState' | 'rootSetState'
|
|
14
|
-
>
|
|
15
|
-
): Actions {
|
|
6
|
+
type Actions = {
|
|
7
|
+
[index: string]: (event: Event) => any;
|
|
8
|
+
};
|
|
9
|
+
export function getBlockActions(options: {
|
|
10
|
+
block: BuilderBlock;
|
|
11
|
+
stripPrefix?: boolean;
|
|
12
|
+
} & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): Actions {
|
|
16
13
|
const obj: Actions = {};
|
|
17
14
|
const optionActions = options.block.actions ?? {};
|
|
18
|
-
|
|
19
15
|
for (const key in optionActions) {
|
|
20
16
|
// eslint-disable-next-line no-prototype-builtins
|
|
21
17
|
if (!optionActions.hasOwnProperty(key)) {
|
|
22
18
|
continue;
|
|
23
19
|
}
|
|
24
20
|
const value = optionActions[key];
|
|
25
|
-
|
|
21
|
+
let eventHandlerName = getEventHandlerName(key);
|
|
22
|
+
if (options.stripPrefix) {
|
|
23
|
+
switch (TARGET) {
|
|
24
|
+
case 'vue2':
|
|
25
|
+
case 'vue3':
|
|
26
|
+
eventHandlerName = eventHandlerName.replace('v-on:', '');
|
|
27
|
+
break;
|
|
28
|
+
case 'svelte':
|
|
29
|
+
eventHandlerName = eventHandlerName.replace('on:', '');
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
obj[eventHandlerName] = createEventHandler(value, options);
|
|
26
34
|
}
|
|
27
35
|
return obj;
|
|
28
|
-
}
|
|
36
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { BuilderBlock } from '../types/builder-block.js';
|
|
2
|
-
|
|
3
2
|
export function getBlockComponentOptions(block: BuilderBlock) {
|
|
4
3
|
return {
|
|
5
4
|
...block.component?.options,
|
|
@@ -7,6 +6,6 @@ export function getBlockComponentOptions(block: BuilderBlock) {
|
|
|
7
6
|
/**
|
|
8
7
|
* Our built-in components frequently make use of the block, so we provide all of it under `builderBlock`
|
|
9
8
|
*/
|
|
10
|
-
builderBlock: block
|
|
9
|
+
builderBlock: block
|
|
11
10
|
};
|
|
12
|
-
}
|
|
11
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { TARGET } from '../constants/target.js';
|
|
2
|
+
import type { BuilderContextInterface } from '../context/types.js';
|
|
2
3
|
import { convertStyleMapToCSSArray } from '../helpers/css.js';
|
|
3
4
|
import type { BuilderBlock } from '../types/builder-block.js';
|
|
5
|
+
import { getReactNativeBlockStyles } from './get-react-native-block-styles.js';
|
|
4
6
|
import { transformBlockProperties } from './transform-block-properties.js';
|
|
5
|
-
|
|
6
7
|
const extractRelevantRootBlockProperties = (block: BuilderBlock) => {
|
|
7
8
|
// currently we are only spreading the `href` property
|
|
8
9
|
// TO-DO: potentially spread all properties from block, but only if they are not already defined by BuilderBlock
|
|
@@ -33,20 +34,31 @@ const extractRelevantRootBlockProperties = (block: BuilderBlock) => {
|
|
|
33
34
|
// ...remainingBlockProperties
|
|
34
35
|
// } = block;
|
|
35
36
|
|
|
36
|
-
return {
|
|
37
|
+
return {
|
|
38
|
+
href: (block as any).href
|
|
39
|
+
};
|
|
37
40
|
};
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
export function getBlockProperties({
|
|
42
|
+
block,
|
|
43
|
+
context
|
|
44
|
+
}: {
|
|
45
|
+
block: BuilderBlock;
|
|
46
|
+
context: BuilderContextInterface;
|
|
47
|
+
}) {
|
|
40
48
|
const properties = {
|
|
41
49
|
...extractRelevantRootBlockProperties(block),
|
|
42
50
|
...block.properties,
|
|
43
51
|
'builder-id': block.id,
|
|
44
|
-
style: getStyleAttribute(block.style),
|
|
45
|
-
class: [block.id, 'builder-block', block.class, block.properties?.class]
|
|
46
|
-
.filter(Boolean)
|
|
47
|
-
.join(' '),
|
|
52
|
+
style: block.style ? getStyleAttribute(block.style) : undefined,
|
|
53
|
+
class: [block.id, 'builder-block', block.class, block.properties?.class].filter(Boolean).join(' ')
|
|
48
54
|
};
|
|
49
|
-
|
|
55
|
+
if (TARGET === 'reactNative') {
|
|
56
|
+
properties.style = getReactNativeBlockStyles({
|
|
57
|
+
block,
|
|
58
|
+
context,
|
|
59
|
+
blockStyles: properties.style
|
|
60
|
+
});
|
|
61
|
+
}
|
|
50
62
|
return transformBlockProperties(properties);
|
|
51
63
|
}
|
|
52
64
|
/**
|
|
@@ -54,13 +66,7 @@ export function getBlockProperties(block: BuilderBlock) {
|
|
|
54
66
|
*
|
|
55
67
|
* Additionally, Svelte, Vue and other frameworks use kebab-case styles, so we need to convert them.
|
|
56
68
|
*/
|
|
57
|
-
function getStyleAttribute(
|
|
58
|
-
style: Partial<CSSStyleDeclaration> | undefined
|
|
59
|
-
): string | Partial<CSSStyleDeclaration> | undefined {
|
|
60
|
-
if (!style) {
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
|
|
69
|
+
function getStyleAttribute(style: Partial<CSSStyleDeclaration>): string | Partial<CSSStyleDeclaration> {
|
|
64
70
|
switch (TARGET) {
|
|
65
71
|
case 'svelte':
|
|
66
72
|
case 'vue2':
|
|
@@ -70,6 +76,7 @@ function getStyleAttribute(
|
|
|
70
76
|
case 'qwik':
|
|
71
77
|
case 'reactNative':
|
|
72
78
|
case 'react':
|
|
79
|
+
case 'rsc':
|
|
73
80
|
return style;
|
|
74
81
|
}
|
|
75
|
-
}
|
|
82
|
+
}
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { isBrowser } from '../is-browser.js';
|
|
2
|
-
|
|
3
2
|
const BUILDER_SEARCHPARAMS_PREFIX = 'builder.';
|
|
4
3
|
const BUILDER_OPTIONS_PREFIX = 'options.';
|
|
5
4
|
type QueryObject = Record<string, string | string[]>;
|
|
6
|
-
|
|
7
|
-
export const convertSearchParamsToQueryObject = (
|
|
8
|
-
searchParams: URLSearchParams
|
|
9
|
-
): QueryObject => {
|
|
5
|
+
export const convertSearchParamsToQueryObject = (searchParams: URLSearchParams): QueryObject => {
|
|
10
6
|
const options: Record<string, string> = {};
|
|
11
7
|
searchParams.forEach((value, key) => {
|
|
12
8
|
options[key] = value;
|
|
@@ -20,26 +16,20 @@ export const convertSearchParamsToQueryObject = (
|
|
|
20
16
|
*
|
|
21
17
|
* @returns
|
|
22
18
|
*/
|
|
23
|
-
export const getBuilderSearchParams = (
|
|
24
|
-
_options: QueryObject | URLSearchParams | undefined
|
|
25
|
-
) => {
|
|
19
|
+
export const getBuilderSearchParams = (_options: QueryObject | URLSearchParams | undefined) => {
|
|
26
20
|
if (!_options) {
|
|
27
21
|
return {};
|
|
28
22
|
}
|
|
29
23
|
const options = normalizeSearchParams(_options);
|
|
30
|
-
|
|
31
24
|
const newOptions: QueryObject = {};
|
|
32
|
-
Object.keys(options).forEach(
|
|
25
|
+
Object.keys(options).forEach(key => {
|
|
33
26
|
if (key.startsWith(BUILDER_SEARCHPARAMS_PREFIX)) {
|
|
34
|
-
const trimmedKey = key
|
|
35
|
-
.replace(BUILDER_SEARCHPARAMS_PREFIX, '')
|
|
36
|
-
.replace(BUILDER_OPTIONS_PREFIX, '');
|
|
27
|
+
const trimmedKey = key.replace(BUILDER_SEARCHPARAMS_PREFIX, '').replace(BUILDER_OPTIONS_PREFIX, '');
|
|
37
28
|
newOptions[trimmedKey] = options[key];
|
|
38
29
|
}
|
|
39
30
|
});
|
|
40
31
|
return newOptions;
|
|
41
32
|
};
|
|
42
|
-
|
|
43
33
|
export const getBuilderSearchParamsFromWindow = () => {
|
|
44
34
|
if (!isBrowser()) {
|
|
45
35
|
return {};
|
|
@@ -47,10 +37,4 @@ export const getBuilderSearchParamsFromWindow = () => {
|
|
|
47
37
|
const searchParams = new URLSearchParams(window.location.search);
|
|
48
38
|
return getBuilderSearchParams(searchParams);
|
|
49
39
|
};
|
|
50
|
-
|
|
51
|
-
export const normalizeSearchParams = (
|
|
52
|
-
searchParams: QueryObject | URLSearchParams
|
|
53
|
-
): QueryObject =>
|
|
54
|
-
searchParams instanceof URLSearchParams
|
|
55
|
-
? convertSearchParamsToQueryObject(searchParams)
|
|
56
|
-
: searchParams;
|
|
40
|
+
export const normalizeSearchParams = (searchParams: QueryObject | URLSearchParams): QueryObject => searchParams instanceof URLSearchParams ? convertSearchParamsToQueryObject(searchParams) : searchParams
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { flatten } from '../../helpers/flatten.js';
|
|
2
|
-
import {
|
|
3
|
-
getBuilderSearchParamsFromWindow,
|
|
4
|
-
normalizeSearchParams,
|
|
5
|
-
} from '../get-builder-search-params/index.js';
|
|
2
|
+
import { getBuilderSearchParamsFromWindow, normalizeSearchParams } from '../get-builder-search-params/index.js';
|
|
6
3
|
import type { GetContentOptions } from './types.js';
|
|
7
|
-
import { DEFAULT_API_VERSION } from '../../types/api-version';
|
|
8
|
-
|
|
4
|
+
import { DEFAULT_API_VERSION } from '../../types/api-version.js';
|
|
9
5
|
export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
10
6
|
const {
|
|
11
7
|
limit = 30,
|
|
@@ -17,44 +13,33 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
|
17
13
|
includeRefs = true,
|
|
18
14
|
enrich,
|
|
19
15
|
locale,
|
|
20
|
-
apiVersion = DEFAULT_API_VERSION
|
|
16
|
+
apiVersion = DEFAULT_API_VERSION
|
|
21
17
|
} = options;
|
|
22
|
-
|
|
23
18
|
if (!apiKey) {
|
|
24
19
|
throw new Error('Missing API key');
|
|
25
20
|
}
|
|
26
|
-
|
|
27
21
|
if (!['v2', 'v3'].includes(apiVersion)) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`
|
|
30
|
-
);
|
|
22
|
+
throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
|
|
31
23
|
}
|
|
32
|
-
|
|
33
|
-
const url = new URL(
|
|
34
|
-
`https://cdn.builder.io/api/${apiVersion}/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${
|
|
35
|
-
locale ? `&locale=${locale}` : ''
|
|
36
|
-
}${enrich ? `&enrich=${enrich}` : ''}`
|
|
37
|
-
);
|
|
38
|
-
|
|
24
|
+
const url = new URL(`https://cdn.builder.io/api/${apiVersion}/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ''}${enrich ? `&enrich=${enrich}` : ''}`);
|
|
39
25
|
const queryOptions = {
|
|
40
26
|
...getBuilderSearchParamsFromWindow(),
|
|
41
|
-
...normalizeSearchParams(options.options || {})
|
|
27
|
+
...normalizeSearchParams(options.options || {})
|
|
42
28
|
};
|
|
43
|
-
|
|
44
29
|
const flattened = flatten(queryOptions);
|
|
45
30
|
for (const key in flattened) {
|
|
46
31
|
url.searchParams.set(key, String(flattened[key]));
|
|
47
32
|
}
|
|
48
|
-
|
|
49
33
|
if (userAttributes) {
|
|
50
34
|
url.searchParams.set('userAttributes', JSON.stringify(userAttributes));
|
|
51
35
|
}
|
|
52
36
|
if (query) {
|
|
53
|
-
const flattened = flatten({
|
|
37
|
+
const flattened = flatten({
|
|
38
|
+
query
|
|
39
|
+
});
|
|
54
40
|
for (const key in flattened) {
|
|
55
41
|
url.searchParams.set(key, JSON.stringify((flattened as any)[key]));
|
|
56
42
|
}
|
|
57
43
|
}
|
|
58
|
-
|
|
59
44
|
return url;
|
|
60
|
-
}
|
|
45
|
+
}
|
|
@@ -2,41 +2,32 @@ import { TARGET } from '../../constants/target.js';
|
|
|
2
2
|
import { handleABTesting } from '../../helpers/ab-tests.js';
|
|
3
3
|
import { getDefaultCanTrack } from '../../helpers/canTrack.js';
|
|
4
4
|
import { logger } from '../../helpers/logger.js';
|
|
5
|
+
import { getPreviewContent } from '../../helpers/preview-lru-cache/get.js';
|
|
5
6
|
import type { BuilderContent } from '../../types/builder-content.js';
|
|
6
7
|
import { fetch } from '../get-fetch.js';
|
|
7
8
|
import { isBrowser } from '../is-browser.js';
|
|
8
9
|
import { generateContentUrl } from './generate-content-url.js';
|
|
9
10
|
import type { GetContentOptions } from './types.js';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
): Promise<BuilderContent | null> {
|
|
18
|
-
const allContent = await getAllContent({ ...options, limit: 1 });
|
|
19
|
-
if (allContent && checkContentHasResults(allContent)) {
|
|
11
|
+
const checkContentHasResults = (content: ContentResponse): content is ContentResults => 'results' in content;
|
|
12
|
+
export async function getContent(options: GetContentOptions): Promise<BuilderContent | null> {
|
|
13
|
+
const allContent = await getAllContent({
|
|
14
|
+
...options,
|
|
15
|
+
limit: 1
|
|
16
|
+
});
|
|
17
|
+
if (allContent) {
|
|
20
18
|
return allContent.results[0] || null;
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
return null;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
type ContentResults = {
|
|
27
23
|
results: BuilderContent[];
|
|
28
24
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
status: number;
|
|
34
|
-
message: string;
|
|
35
|
-
};
|
|
36
|
-
|
|
25
|
+
type ContentResponse = ContentResults | {
|
|
26
|
+
status: number;
|
|
27
|
+
message: string;
|
|
28
|
+
};
|
|
37
29
|
const fetchContent = async (options: GetContentOptions) => {
|
|
38
30
|
const url = generateContentUrl(options);
|
|
39
|
-
|
|
40
31
|
const res = await fetch(url.href);
|
|
41
32
|
const content = await (res.json() as Promise<ContentResponse>);
|
|
42
33
|
return content;
|
|
@@ -45,12 +36,17 @@ const fetchContent = async (options: GetContentOptions) => {
|
|
|
45
36
|
/**
|
|
46
37
|
* Exported only for testing purposes. Should not be used directly.
|
|
47
38
|
*/
|
|
48
|
-
export const processContentResult = async (
|
|
49
|
-
options: GetContentOptions,
|
|
50
|
-
content: ContentResults
|
|
51
|
-
) => {
|
|
39
|
+
export const processContentResult = async (options: GetContentOptions, content: ContentResults, url: URL = generateContentUrl(options)) => {
|
|
52
40
|
const canTrack = getDefaultCanTrack(options.canTrack);
|
|
53
|
-
|
|
41
|
+
const isPreviewing = url.search.includes(`preview=`);
|
|
42
|
+
if (TARGET === 'rsc' && isPreviewing) {
|
|
43
|
+
const newResults: BuilderContent[] = [];
|
|
44
|
+
for (const item of content.results) {
|
|
45
|
+
const previewContent = getPreviewContent(url.searchParams);
|
|
46
|
+
newResults.push(previewContent || item);
|
|
47
|
+
}
|
|
48
|
+
content.results = newResults;
|
|
49
|
+
}
|
|
54
50
|
if (!canTrack) return content;
|
|
55
51
|
if (!(isBrowser() || TARGET === 'reactNative')) return content;
|
|
56
52
|
|
|
@@ -63,31 +59,32 @@ export const processContentResult = async (
|
|
|
63
59
|
try {
|
|
64
60
|
const newResults: BuilderContent[] = [];
|
|
65
61
|
for (const item of content.results) {
|
|
66
|
-
newResults.push(await handleABTesting({
|
|
62
|
+
newResults.push(await handleABTesting({
|
|
63
|
+
item,
|
|
64
|
+
canTrack
|
|
65
|
+
}));
|
|
67
66
|
}
|
|
68
67
|
content.results = newResults;
|
|
69
68
|
} catch (e) {
|
|
70
69
|
logger.error('Could not process A/B tests. ', e);
|
|
71
70
|
}
|
|
72
|
-
|
|
73
71
|
return content;
|
|
74
72
|
};
|
|
75
|
-
|
|
76
|
-
export async function getAllContent(
|
|
77
|
-
options: GetContentOptions
|
|
78
|
-
): Promise<ContentResponse | null> {
|
|
73
|
+
export async function getAllContent(options: GetContentOptions) {
|
|
79
74
|
try {
|
|
80
75
|
const url = generateContentUrl(options);
|
|
81
76
|
const content = await fetchContent(options);
|
|
82
|
-
|
|
83
77
|
if (!checkContentHasResults(content)) {
|
|
84
|
-
logger.error('Error fetching data. ', {
|
|
85
|
-
|
|
78
|
+
logger.error('Error fetching data. ', {
|
|
79
|
+
url,
|
|
80
|
+
content,
|
|
81
|
+
options
|
|
82
|
+
});
|
|
83
|
+
return null;
|
|
86
84
|
}
|
|
87
|
-
|
|
88
85
|
return processContentResult(options, content);
|
|
89
86
|
} catch (error) {
|
|
90
87
|
logger.error('Error fetching data. ', error);
|
|
91
88
|
return null;
|
|
92
89
|
}
|
|
93
|
-
}
|
|
90
|
+
}
|
|
@@ -9,7 +9,9 @@ export interface GetContentOptions {
|
|
|
9
9
|
limit?: number;
|
|
10
10
|
|
|
11
11
|
/** User attributes to target on, such as { urlPath: '/foo', device: 'mobile', ...etc } */
|
|
12
|
-
userAttributes?: (Record<string, string> & {
|
|
12
|
+
userAttributes?: (Record<string, string> & {
|
|
13
|
+
urlPath?: string;
|
|
14
|
+
}) | null;
|
|
13
15
|
|
|
14
16
|
/** Custom query */
|
|
15
17
|
query?: Record<string, any>;
|
|
@@ -57,4 +59,4 @@ export interface GetContentOptions {
|
|
|
57
59
|
* Defaults to `v3`.
|
|
58
60
|
*/
|
|
59
61
|
apiVersion?: 'v2' | 'v3';
|
|
60
|
-
}
|
|
62
|
+
}
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import { getGlobalThis } from './get-global-this.js';
|
|
2
|
-
|
|
3
2
|
function getFetch(): typeof global.fetch {
|
|
4
3
|
const globalFetch: typeof global.fetch = getGlobalThis().fetch;
|
|
5
|
-
|
|
6
4
|
if (typeof globalFetch === 'undefined') {
|
|
7
|
-
console.warn(
|
|
8
|
-
|
|
9
|
-
For more information, read https://github.com/BuilderIO/this-package-uses-fetch`
|
|
10
|
-
);
|
|
11
|
-
|
|
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`);
|
|
12
7
|
throw new Error('Builder SDK could not find a global `fetch` function');
|
|
13
8
|
}
|
|
14
|
-
|
|
15
9
|
return globalFetch;
|
|
16
10
|
}
|
|
17
|
-
|
|
18
|
-
export const fetch = getFetch();
|
|
11
|
+
export const fetch = getFetch()
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
type Global = typeof global | typeof window | typeof self | typeof globalThis;
|
|
2
|
-
|
|
3
2
|
export function getGlobalThis(): Global {
|
|
4
3
|
if (typeof globalThis !== 'undefined') {
|
|
5
4
|
return globalThis;
|
|
@@ -14,4 +13,4 @@ export function getGlobalThis(): Global {
|
|
|
14
13
|
return self;
|
|
15
14
|
}
|
|
16
15
|
return globalThis;
|
|
17
|
-
}
|
|
16
|
+
}
|