@storyblok/astro 4.1.0-alpha.2 → 4.1.0-alpha.3

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,8 +1,3 @@
1
- import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
2
- export interface RawCodeItem {
3
- options?: {
4
- apiOptions?: ISbStoriesParams;
5
- bridgeOptions?: StoryblokBridgeConfigV2;
6
- };
7
- }
8
- export declare function generateFinalBridgeObject(rawCode: RawCodeItem[]): StoryblokBridgeConfigV2;
1
+ import type { StoryblokBridgeConfigV2 } from "@storyblok/js";
2
+ import type { RawCode } from "../vite-plugins/vite-plugin-storyblok-bridge";
3
+ export declare function generateFinalBridgeObject(rawCode: RawCode): StoryblokBridgeConfigV2;
@@ -1 +1,7 @@
1
- export declare function parseAstRawCode(astCode: any): any;
1
+ import type { RawCodeItemOptions } from "../vite-plugins/vite-plugin-storyblok-bridge";
2
+ import type { Rollup } from "vite";
3
+ /**
4
+ * Parses through the Abstract Syntax Tree (AST) code to locate the 'useStoryblok' function and its properties.
5
+ * This functionality is crucial for generating a virtual module that can be utilized during the initialization of the Storyblok bridge.
6
+ */
7
+ export declare function parseAstRawCode(astCode: Rollup.ProgramNode): RawCodeItemOptions;
@@ -1,2 +1,12 @@
1
+ import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
1
2
  import type { Plugin } from "vite";
3
+ export interface RawCodeItem {
4
+ url: string;
5
+ options?: RawCodeItemOptions;
6
+ }
7
+ export type RawCode = RawCodeItem[];
8
+ export interface RawCodeItemOptions {
9
+ apiOptions?: ISbStoriesParams;
10
+ bridgeOptions?: StoryblokBridgeConfigV2;
11
+ }
2
12
  export declare function vitePluginStoryblokBridge(experimentalLivePreview: boolean, output: string): Plugin;
@@ -17,7 +17,7 @@ export async function handleStoryblokMessage(event: {
17
17
  // Debounce the getNewHTMLBody function
18
18
  const debouncedGetNewHTMLBody = async () => {
19
19
  const newBody = await getNewHTMLBody(story);
20
- const currentBody = document.body as HTMLElement;
20
+ const currentBody = document.body;
21
21
  if (newBody.outerHTML === currentBody.outerHTML) return;
22
22
  // Get current focused element in Storyblok
23
23
  const focusedElem = document.querySelector('[data-blok-focused="true"]');
@@ -58,6 +58,7 @@ function updateDOMWithNewBody(
58
58
  }
59
59
 
60
60
  async function getNewHTMLBody(story: ISbStoryData) {
61
+ //TODO How to handel (50x, 405, etc.)
61
62
  const result = await fetch(location.href, {
62
63
  method: "POST",
63
64
  body: JSON.stringify({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storyblok/astro",
3
- "version": "4.1.0-alpha.2",
3
+ "version": "4.1.0-alpha.3",
4
4
  "description": "Official Astro integration for the Storyblok Headless CMS",
5
5
  "main": "./dist/storyblok-astro.js",
6
6
  "module": "./dist/storyblok-astro.mjs",
@@ -1,27 +1,18 @@
1
- import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
1
+ import type { StoryblokBridgeConfigV2 } from "@storyblok/js";
2
+ import type { RawCode } from "../vite-plugins/vite-plugin-storyblok-bridge";
2
3
 
3
- export interface RawCodeItem {
4
- options?: {
5
- apiOptions?: ISbStoriesParams;
6
- bridgeOptions?: StoryblokBridgeConfigV2;
7
- };
8
- }
9
- export function generateFinalBridgeObject(
10
- rawCode: RawCodeItem[]
11
- ): StoryblokBridgeConfigV2 {
12
- let mergedOptions = {
4
+ export function generateFinalBridgeObject(rawCode: RawCode) {
5
+ let mergedOptions: StoryblokBridgeConfigV2 = {
13
6
  resolveRelations: [],
14
7
  };
15
8
 
16
- function addToResolveRelations(
17
- resolveRelations: string[] | string | undefined
18
- ) {
19
- if (resolveRelations) {
20
- if (Array.isArray(resolveRelations)) {
21
- mergedOptions.resolveRelations.push(...resolveRelations);
22
- } else {
23
- mergedOptions.resolveRelations.push(resolveRelations);
24
- }
9
+ function addToResolveRelations(resolveRelations?: string[] | string) {
10
+ if (resolveRelations && Array.isArray(mergedOptions.resolveRelations)) {
11
+ mergedOptions.resolveRelations.push(
12
+ ...(Array.isArray(resolveRelations)
13
+ ? resolveRelations
14
+ : [resolveRelations])
15
+ );
25
16
  }
26
17
  }
27
18
 
@@ -1,22 +1,34 @@
1
1
  import mergeWith from "lodash.mergewith";
2
+ import type { ISbStoriesParams, StoryblokBridgeConfigV2 } from "@storyblok/js";
3
+ import type { RawCodeItemOptions } from "../vite-plugins/vite-plugin-storyblok-bridge";
4
+ import type { Rollup } from "vite";
5
+ import type { SpreadElement, Property } from "estree";
2
6
 
3
- export function parseAstRawCode(astCode) {
4
- let obj;
5
- function customizer(_, srcValue) {
7
+ /**
8
+ * Parses through the Abstract Syntax Tree (AST) code to locate the 'useStoryblok' function and its properties.
9
+ * This functionality is crucial for generating a virtual module that can be utilized during the initialization of the Storyblok bridge.
10
+ */
11
+
12
+ export function parseAstRawCode(astCode: Rollup.ProgramNode) {
13
+ let obj: RawCodeItemOptions = {};
14
+
15
+ function customizer(_: any, srcValue: any) {
6
16
  if (
7
17
  srcValue?.type === "AwaitExpression" &&
8
- srcValue?.argument?.callee?.name === "useStoryblok"
18
+ srcValue.argument.type === "CallExpression" &&
19
+ srcValue.argument.callee.type === "Identifier" &&
20
+ srcValue.argument.callee.name === "useStoryblok"
9
21
  ) {
10
- const props = srcValue?.argument?.arguments;
11
- if (props[1]?.type === "ObjectExpression") {
12
- const apiOptions = getAstPropToObj(props[1]?.properties);
22
+ const props = srcValue.argument.arguments;
23
+ if (props && props[1].type === "ObjectExpression") {
24
+ const apiOptions = getAstPropToObj(props[1].properties);
13
25
  obj = {
14
26
  ...obj,
15
27
  apiOptions,
16
28
  };
17
29
  }
18
- if (props[2]?.type === "ObjectExpression") {
19
- const bridgeOptions = getAstPropToObj(props[2]?.properties);
30
+ if (props && props[2].type === "ObjectExpression") {
31
+ const bridgeOptions = getAstPropToObj(props[2].properties);
20
32
  obj = {
21
33
  ...obj,
22
34
  bridgeOptions,
@@ -29,15 +41,26 @@ export function parseAstRawCode(astCode) {
29
41
  return obj;
30
42
  }
31
43
 
32
- function getAstPropToObj(properties) {
33
- return properties.reduce((options, { key, value }) => {
44
+ function getAstPropToObj(properties: (SpreadElement | Property)[]) {
45
+ const option: ISbStoriesParams | StoryblokBridgeConfigV2 = {};
46
+
47
+ return properties.reduce((options, property) => {
48
+ if (property.type !== "Property") return options;
49
+ const { key, value } = property;
34
50
  const { type } = value;
35
- options[key.name] =
36
- type === "Literal"
37
- ? value.value
38
- : type === "ArrayExpression"
39
- ? value.elements.map((v) => v.value)
40
- : value.value;
51
+ if (key.type !== "Identifier") return options;
52
+
53
+ if (type === "Literal") {
54
+ options[key.name] = value.value;
55
+ } else if (type === "ArrayExpression") {
56
+ const arrayValues = value.elements.reduce((acc, element) => {
57
+ if (element.type === "Literal" && element.value) {
58
+ return [...acc, element.value];
59
+ }
60
+ return acc;
61
+ }, []);
62
+ options[key.name] = arrayValues;
63
+ }
41
64
  return options;
42
- }, {});
65
+ }, option);
43
66
  }