@okam/directus-block 1.2.3 → 1.3.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.
Files changed (38) hide show
  1. package/directus-block/src/blocks/BlockWysiwyg/config.js +9 -0
  2. package/directus-block/src/blocks/BlockWysiwyg/config.mjs +10 -0
  3. package/directus-block/src/blocks/BlockWysiwyg/index.js +16 -0
  4. package/directus-block/src/blocks/BlockWysiwyg/index.mjs +17 -0
  5. package/directus-block/src/components/BlockDispatcher/config.js +8 -0
  6. package/directus-block/src/components/BlockDispatcher/config.mjs +9 -0
  7. package/directus-block/src/components/BlockDispatcher/index.js +24 -0
  8. package/directus-block/src/components/BlockDispatcher/index.mjs +25 -0
  9. package/directus-block/src/components/BlockSerializer/index.js +38 -0
  10. package/{server.mjs → directus-block/src/components/BlockSerializer/index.mjs} +5 -26
  11. package/directus-block/src/generated/fragment-masking.js +6 -0
  12. package/directus-block/src/generated/fragment-masking.mjs +6 -0
  13. package/directus-block/src/generated/graphql.js +4 -0
  14. package/directus-block/src/generated/graphql.mjs +4 -0
  15. package/directus-block/src/hooks/useBlock.js +19 -0
  16. package/directus-block/src/hooks/useBlock.mjs +20 -0
  17. package/directus-block/src/index.js +10 -0
  18. package/directus-block/src/index.mjs +10 -0
  19. package/directus-block/src/server.js +11 -0
  20. package/directus-block/src/server.mjs +11 -0
  21. package/directus-block/src/utils/get-block-props.js +42 -0
  22. package/directus-block/src/utils/get-block-props.mjs +43 -0
  23. package/directus-block/src/utils/merge-configs.js +9 -0
  24. package/directus-block/src/utils/merge-configs.mjs +10 -0
  25. package/directus-query/src/lib/config.js +3 -0
  26. package/directus-query/src/lib/config.mjs +2 -0
  27. package/directus-query/src/lib/query.js +10 -0
  28. package/directus-query/src/lib/query.mjs +10 -0
  29. package/directus-query/src/lib/request.js +38 -0
  30. package/directus-query/src/lib/request.mjs +38 -0
  31. package/hooks/useBlock.d.ts +8 -0
  32. package/index.d.ts +2 -0
  33. package/package.json +1 -1
  34. package/config-BNCxf6ZM.mjs +0 -117
  35. package/config-Wm_s8RKf.js +0 -116
  36. package/index.js +0 -6
  37. package/index.mjs +0 -6
  38. package/server.js +0 -59
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ const jsxRuntime = require("react/jsx-runtime");
3
+ const index = require("./index.js");
4
+ const blockWysiwygConfig = {
5
+ block_wysiwyg: {
6
+ default: (props) => /* @__PURE__ */ jsxRuntime.jsx(index, { ...props })
7
+ }
8
+ };
9
+ module.exports = blockWysiwygConfig;
@@ -0,0 +1,10 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import BlockWysiwyg from "./index.mjs";
3
+ const blockWysiwygConfig = {
4
+ block_wysiwyg: {
5
+ default: (props) => /* @__PURE__ */ jsx(BlockWysiwyg, { ...props })
6
+ }
7
+ };
8
+ export {
9
+ blockWysiwygConfig as default
10
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ const jsxRuntime = require("react/jsx-runtime");
3
+ const stackUi = require("@okam/stack-ui");
4
+ const React = require("react");
5
+ const useBlock = require("../../hooks/useBlock.js");
6
+ const BlockWysiwyg = async (props) => {
7
+ const key = "block_wysiwyg_by_id";
8
+ const { themeName = "wysiwyg", tokens } = props;
9
+ const { content, title, level, cmsTokens } = await useBlock(props, key);
10
+ if (!content && !(title && level)) return null;
11
+ return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
12
+ title && level && /* @__PURE__ */ jsxRuntime.jsx(stackUi.Box, { as: "span", themeName, tokens: { ...tokens, ...cmsTokens }, children: React.createElement(level, {}, title) }),
13
+ content && /* @__PURE__ */ jsxRuntime.jsx(stackUi.WysiwygBlock, { themeName, tokens: { ...tokens, ...cmsTokens }, content })
14
+ ] });
15
+ };
16
+ module.exports = BlockWysiwyg;
@@ -0,0 +1,17 @@
1
+ import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
+ import { Box, WysiwygBlock } from "@okam/stack-ui";
3
+ import React from "react";
4
+ import useBlock from "../../hooks/useBlock.mjs";
5
+ const BlockWysiwyg = async (props) => {
6
+ const key = "block_wysiwyg_by_id";
7
+ const { themeName = "wysiwyg", tokens } = props;
8
+ const { content, title, level, cmsTokens } = await useBlock(props, key);
9
+ if (!content && !(title && level)) return null;
10
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
11
+ title && level && /* @__PURE__ */ jsx(Box, { as: "span", themeName, tokens: { ...tokens, ...cmsTokens }, children: React.createElement(level, {}, title) }),
12
+ content && /* @__PURE__ */ jsx(WysiwygBlock, { themeName, tokens: { ...tokens, ...cmsTokens }, content })
13
+ ] });
14
+ };
15
+ export {
16
+ BlockWysiwyg as default
17
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ const config = require("../../blocks/BlockWysiwyg/config.js");
3
+ const baseConfig = {
4
+ components: {
5
+ ...config
6
+ }
7
+ };
8
+ module.exports = baseConfig;
@@ -0,0 +1,9 @@
1
+ import blockWysiwygConfig from "../../blocks/BlockWysiwyg/config.mjs";
2
+ const baseConfig = {
3
+ components: {
4
+ ...blockWysiwygConfig
5
+ }
6
+ };
7
+ export {
8
+ baseConfig as default
9
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ const jsxRuntime = require("react/jsx-runtime");
3
+ require("@tanstack/react-query");
4
+ require("radash");
5
+ require("../../../../directus-query/src/lib/request.js");
6
+ require("graphql-request");
7
+ require("../../../../directus-query/src/lib/config.js");
8
+ const mergeConfigs = require("../../utils/merge-configs.js");
9
+ const index = require("../BlockSerializer/index.js");
10
+ const config = require("./config.js");
11
+ const BlockDispatcher = (props) => {
12
+ const { children, config: config$1, ...rest } = props;
13
+ const finalConfig = mergeConfigs(config, config$1);
14
+ const renderBlock = (blockProps) => {
15
+ return children ? children({ ...rest, ...blockProps, config: finalConfig }) : /* @__PURE__ */ jsxRuntime.jsx(index, { ...rest, ...blockProps, config: finalConfig });
16
+ };
17
+ if ("blocks" in props) {
18
+ const { blocks } = props;
19
+ return blocks.map(renderBlock);
20
+ }
21
+ const { block } = props;
22
+ return renderBlock(block);
23
+ };
24
+ module.exports = BlockDispatcher;
@@ -0,0 +1,25 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import "@tanstack/react-query";
3
+ import "radash";
4
+ import "../../../../directus-query/src/lib/request.mjs";
5
+ import "graphql-request";
6
+ import "../../../../directus-query/src/lib/config.mjs";
7
+ import mergeConfigs from "../../utils/merge-configs.mjs";
8
+ import BlockSerializer from "../BlockSerializer/index.mjs";
9
+ import baseConfig from "./config.mjs";
10
+ const BlockDispatcher = (props) => {
11
+ const { children, config, ...rest } = props;
12
+ const finalConfig = mergeConfigs(baseConfig, config);
13
+ const renderBlock = (blockProps) => {
14
+ return children ? children({ ...rest, ...blockProps, config: finalConfig }) : /* @__PURE__ */ jsx(BlockSerializer, { ...rest, ...blockProps, config: finalConfig });
15
+ };
16
+ if ("blocks" in props) {
17
+ const { blocks } = props;
18
+ return blocks.map(renderBlock);
19
+ }
20
+ const { block } = props;
21
+ return renderBlock(block);
22
+ };
23
+ export {
24
+ BlockDispatcher as default
25
+ };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ const jsxRuntime = require("react/jsx-runtime");
3
+ const graphql = require("../../generated/graphql.js");
4
+ const fragmentMasking = require("../../generated/fragment-masking.js");
5
+ require("@tanstack/react-query");
6
+ require("radash");
7
+ require("../../../../directus-query/src/lib/request.js");
8
+ require("graphql-request");
9
+ require("../../../../directus-query/src/lib/config.js");
10
+ const BlockSerializer = (props) => {
11
+ var _a, _b, _c;
12
+ const { item, collection, config, variables, defaultVariant, ...rest } = props;
13
+ if (!collection || !config) return null;
14
+ const blockConfig = (_a = config == null ? void 0 : config.components) == null ? void 0 : _a[collection];
15
+ if (!blockConfig) return null;
16
+ const { settings } = item ?? {};
17
+ const id = (item == null ? void 0 : item.id) ?? (variables == null ? void 0 : variables.id);
18
+ if (!id) return null;
19
+ const { variant } = fragmentMasking.useFragment(graphql.BlockSettingsFragmentDoc, settings) ?? {};
20
+ const variantWithFallback = ((_b = blockConfig.getVariant) == null ? void 0 : _b.call(blockConfig, props)) ?? variant ?? defaultVariant;
21
+ const { default: defaultBlockComponent, document } = blockConfig;
22
+ const variantBlockComponent = (_c = blockConfig.variants) == null ? void 0 : _c[variantWithFallback ?? ""];
23
+ const BlockComponent = variantBlockComponent ?? defaultBlockComponent;
24
+ if (!BlockComponent) return null;
25
+ return /* @__PURE__ */ jsxRuntime.jsx(
26
+ BlockComponent,
27
+ {
28
+ document,
29
+ config,
30
+ collection,
31
+ item,
32
+ ...id ? { variables: { ...variables, id } } : { variables },
33
+ ...rest
34
+ },
35
+ id
36
+ );
37
+ };
38
+ module.exports = BlockSerializer;
@@ -1,16 +1,11 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
+ import { BlockSettingsFragmentDoc } from "../../generated/graphql.mjs";
3
+ import { useFragment } from "../../generated/fragment-masking.mjs";
2
4
  import "@tanstack/react-query";
3
5
  import "radash";
4
- import { u as useFragment, c as BlockSettingsFragmentDoc, a as baseConfig } from "./config-BNCxf6ZM.mjs";
5
- import { g } from "./config-BNCxf6ZM.mjs";
6
+ import "../../../../directus-query/src/lib/request.mjs";
6
7
  import "graphql-request";
7
- function mergeConfigs(baseConfig2, ...configs) {
8
- const finalConfig = configs.reduce((mergedConfig, config) => {
9
- if (!config) return mergedConfig;
10
- return { components: { ...mergedConfig.components, ...config.components } };
11
- }, baseConfig2);
12
- return finalConfig;
13
- }
8
+ import "../../../../directus-query/src/lib/config.mjs";
14
9
  const BlockSerializer = (props) => {
15
10
  var _a, _b, _c;
16
11
  const { item, collection, config, variables, defaultVariant, ...rest } = props;
@@ -39,22 +34,6 @@ const BlockSerializer = (props) => {
39
34
  id
40
35
  );
41
36
  };
42
- const BlockDispatcher = (props) => {
43
- const { children, config, ...rest } = props;
44
- const finalConfig = mergeConfigs(baseConfig, config);
45
- const renderBlock = (blockProps) => {
46
- return children ? children({ ...rest, ...blockProps, config: finalConfig }) : /* @__PURE__ */ jsx(BlockSerializer, { ...rest, ...blockProps, config: finalConfig });
47
- };
48
- if ("blocks" in props) {
49
- const { blocks } = props;
50
- return blocks.map(renderBlock);
51
- }
52
- const { block } = props;
53
- return renderBlock(block);
54
- };
55
37
  export {
56
- BlockDispatcher,
57
- BlockSerializer,
58
- g as getBlockProps,
59
- mergeConfigs
38
+ BlockSerializer as default
60
39
  };
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function useFragment(_documentNode, fragmentType) {
4
+ return fragmentType;
5
+ }
6
+ exports.useFragment = useFragment;
@@ -0,0 +1,6 @@
1
+ function useFragment(_documentNode, fragmentType) {
2
+ return fragmentType;
3
+ }
4
+ export {
5
+ useFragment
6
+ };
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const BlockSettingsFragmentDoc = { "kind": "Document", "definitions": [{ "kind": "FragmentDefinition", "name": { "kind": "Name", "value": "BlockSettings" }, "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "block_settings" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "tokens" } }, { "kind": "Field", "name": { "kind": "Name", "value": "variant" } }] } }] };
4
+ exports.BlockSettingsFragmentDoc = BlockSettingsFragmentDoc;
@@ -0,0 +1,4 @@
1
+ const BlockSettingsFragmentDoc = { "kind": "Document", "definitions": [{ "kind": "FragmentDefinition", "name": { "kind": "Name", "value": "BlockSettings" }, "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "block_settings" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "tokens" } }, { "kind": "Field", "name": { "kind": "Name", "value": "variant" } }] } }] };
2
+ export {
3
+ BlockSettingsFragmentDoc
4
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ const radash = require("radash");
3
+ const graphql = require("../generated/graphql.js");
4
+ const fragmentMasking = require("../generated/fragment-masking.js");
5
+ const getBlockProps = require("../utils/get-block-props.js");
6
+ async function useBlock(props, blockKey, doc) {
7
+ const item = radash.get(props, "item");
8
+ const document = doc ?? radash.get(props, "document");
9
+ const variables = radash.get(props, "variables");
10
+ const propsWithFallback = await getBlockProps({
11
+ item,
12
+ blockKey,
13
+ document,
14
+ variables
15
+ });
16
+ const { tokens } = fragmentMasking.useFragment(graphql.BlockSettingsFragmentDoc, propsWithFallback == null ? void 0 : propsWithFallback.settings) ?? {};
17
+ return { ...propsWithFallback, cmsTokens: tokens };
18
+ }
19
+ module.exports = useBlock;
@@ -0,0 +1,20 @@
1
+ import { get } from "radash";
2
+ import { BlockSettingsFragmentDoc } from "../generated/graphql.mjs";
3
+ import { useFragment } from "../generated/fragment-masking.mjs";
4
+ import getBlockProps from "../utils/get-block-props.mjs";
5
+ async function useBlock(props, blockKey, doc) {
6
+ const item = get(props, "item");
7
+ const document = doc ?? get(props, "document");
8
+ const variables = get(props, "variables");
9
+ const propsWithFallback = await getBlockProps({
10
+ item,
11
+ blockKey,
12
+ document,
13
+ variables
14
+ });
15
+ const { tokens } = useFragment(BlockSettingsFragmentDoc, propsWithFallback == null ? void 0 : propsWithFallback.settings) ?? {};
16
+ return { ...propsWithFallback, cmsTokens: tokens };
17
+ }
18
+ export {
19
+ useBlock as default
20
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const index = require("./blocks/BlockWysiwyg/index.js");
4
+ const useBlock = require("./hooks/useBlock.js");
5
+ const config = require("./blocks/BlockWysiwyg/config.js");
6
+ const config$1 = require("./components/BlockDispatcher/config.js");
7
+ exports.BlockWysiwyg = index;
8
+ exports.useBlock = useBlock;
9
+ exports.blockWysiwygConfig = config;
10
+ exports.baseConfig = config$1;
@@ -0,0 +1,10 @@
1
+ import { default as default2 } from "./blocks/BlockWysiwyg/index.mjs";
2
+ import { default as default3 } from "./hooks/useBlock.mjs";
3
+ import { default as default4 } from "./blocks/BlockWysiwyg/config.mjs";
4
+ import { default as default5 } from "./components/BlockDispatcher/config.mjs";
5
+ export {
6
+ default2 as BlockWysiwyg,
7
+ default5 as baseConfig,
8
+ default4 as blockWysiwygConfig,
9
+ default3 as useBlock
10
+ };
@@ -0,0 +1,11 @@
1
+ "server-only";
2
+ "use strict";
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
4
+ const index = require("./components/BlockDispatcher/index.js");
5
+ const index$1 = require("./components/BlockSerializer/index.js");
6
+ const getBlockProps = require("./utils/get-block-props.js");
7
+ const mergeConfigs = require("./utils/merge-configs.js");
8
+ exports.BlockDispatcher = index;
9
+ exports.BlockSerializer = index$1;
10
+ exports.getBlockProps = getBlockProps;
11
+ exports.mergeConfigs = mergeConfigs;
@@ -0,0 +1,11 @@
1
+ "server-only";
2
+ import { default as default2 } from "./components/BlockDispatcher/index.mjs";
3
+ import { default as default3 } from "./components/BlockSerializer/index.mjs";
4
+ import { default as default4 } from "./utils/get-block-props.mjs";
5
+ import { default as default5 } from "./utils/merge-configs.mjs";
6
+ export {
7
+ default2 as BlockDispatcher,
8
+ default3 as BlockSerializer,
9
+ default4 as getBlockProps,
10
+ default5 as mergeConfigs
11
+ };
@@ -0,0 +1,42 @@
1
+ "server-only";
2
+ "use strict";
3
+ require("@tanstack/react-query");
4
+ const query = require("../../../directus-query/src/lib/query.js");
5
+ require("../../../directus-query/src/lib/request.js");
6
+ require("graphql-request");
7
+ require("../../../directus-query/src/lib/config.js");
8
+ const radash = require("radash");
9
+ function isVariables(maybeVariables) {
10
+ return !!maybeVariables;
11
+ }
12
+ function isOnlyIdInItem(item) {
13
+ return !radash.isEmpty(item) && Object.keys(item).length === 1 && Object.keys(item)[0] === "id" && !!item.id;
14
+ }
15
+ async function queryFromVariables(params) {
16
+ const { document, blockKey, variables } = params;
17
+ if (!document || !isVariables(variables)) return null;
18
+ const queriedBlockProps = await query.queryGql(document, variables);
19
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
20
+ const queriedBlockFragment = queriedBlockProps[blockKey];
21
+ return queriedBlockFragment;
22
+ }
23
+ async function getBlockProps(params) {
24
+ const { document, item, blockKey, variables } = params;
25
+ if (item) {
26
+ if (!isOnlyIdInItem(item)) {
27
+ return item;
28
+ }
29
+ const variablesWithFallback = { id: item.id, ...variables };
30
+ if (!isVariables(variablesWithFallback)) return null;
31
+ return queryFromVariables({
32
+ ...params,
33
+ variables: variablesWithFallback
34
+ });
35
+ }
36
+ if (!document || !isVariables(variables)) return null;
37
+ const queriedBlockProps = await query.queryGql(document, variables);
38
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
39
+ const queriedBlockFragment = queriedBlockProps[blockKey];
40
+ return queriedBlockFragment;
41
+ }
42
+ module.exports = getBlockProps;
@@ -0,0 +1,43 @@
1
+ "server-only";
2
+ import "@tanstack/react-query";
3
+ import { queryGql } from "../../../directus-query/src/lib/query.mjs";
4
+ import "../../../directus-query/src/lib/request.mjs";
5
+ import "graphql-request";
6
+ import "../../../directus-query/src/lib/config.mjs";
7
+ import { isEmpty } from "radash";
8
+ function isVariables(maybeVariables) {
9
+ return !!maybeVariables;
10
+ }
11
+ function isOnlyIdInItem(item) {
12
+ return !isEmpty(item) && Object.keys(item).length === 1 && Object.keys(item)[0] === "id" && !!item.id;
13
+ }
14
+ async function queryFromVariables(params) {
15
+ const { document, blockKey, variables } = params;
16
+ if (!document || !isVariables(variables)) return null;
17
+ const queriedBlockProps = await queryGql(document, variables);
18
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
19
+ const queriedBlockFragment = queriedBlockProps[blockKey];
20
+ return queriedBlockFragment;
21
+ }
22
+ async function getBlockProps(params) {
23
+ const { document, item, blockKey, variables } = params;
24
+ if (item) {
25
+ if (!isOnlyIdInItem(item)) {
26
+ return item;
27
+ }
28
+ const variablesWithFallback = { id: item.id, ...variables };
29
+ if (!isVariables(variablesWithFallback)) return null;
30
+ return queryFromVariables({
31
+ ...params,
32
+ variables: variablesWithFallback
33
+ });
34
+ }
35
+ if (!document || !isVariables(variables)) return null;
36
+ const queriedBlockProps = await queryGql(document, variables);
37
+ if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
38
+ const queriedBlockFragment = queriedBlockProps[blockKey];
39
+ return queriedBlockFragment;
40
+ }
41
+ export {
42
+ getBlockProps as default
43
+ };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ function mergeConfigs(baseConfig, ...configs) {
3
+ const finalConfig = configs.reduce((mergedConfig, config) => {
4
+ if (!config) return mergedConfig;
5
+ return { components: { ...mergedConfig.components, ...config.components } };
6
+ }, baseConfig);
7
+ return finalConfig;
8
+ }
9
+ module.exports = mergeConfigs;
@@ -0,0 +1,10 @@
1
+ function mergeConfigs(baseConfig, ...configs) {
2
+ const finalConfig = configs.reduce((mergedConfig, config) => {
3
+ if (!config) return mergedConfig;
4
+ return { components: { ...mergedConfig.components, ...config.components } };
5
+ }, baseConfig);
6
+ return finalConfig;
7
+ }
8
+ export {
9
+ mergeConfigs as default
10
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ process.env.NEXT_PUBLIC_GRAPHQL_URL;
3
+ process.env.NEXT_PUBLIC_API_TOKEN;
@@ -0,0 +1,2 @@
1
+ process.env.NEXT_PUBLIC_GRAPHQL_URL;
2
+ process.env.NEXT_PUBLIC_API_TOKEN;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("radash");
4
+ const request = require("./request.js");
5
+ function queryGql(document, queryKey, client = request.graphqlRequestClient) {
6
+ return client.request(document, {
7
+ ...queryKey
8
+ });
9
+ }
10
+ exports.queryGql = queryGql;
@@ -0,0 +1,10 @@
1
+ import "radash";
2
+ import { graphqlRequestClient } from "./request.mjs";
3
+ function queryGql(document, queryKey, client = graphqlRequestClient) {
4
+ return client.request(document, {
5
+ ...queryKey
6
+ });
7
+ }
8
+ export {
9
+ queryGql
10
+ };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const reactQuery = require("@tanstack/react-query");
4
+ const graphqlRequest = require("graphql-request");
5
+ const GRAPHQL_ENDPOINT = process.env.NEXT_PUBLIC_GRAPHQL_URL;
6
+ const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
7
+ const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
8
+ const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
9
+ const graphqlRequestClient = new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT, {
10
+ credentials: "include",
11
+ mode: "cors",
12
+ fetch,
13
+ headers: {
14
+ Authorization: `Bearer ${AUTH_TOKEN}`
15
+ }
16
+ });
17
+ new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
18
+ credentials: "include",
19
+ mode: "cors",
20
+ fetch,
21
+ headers: {
22
+ Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
23
+ }
24
+ });
25
+ new reactQuery.QueryClient({
26
+ queryCache: new reactQuery.QueryCache({
27
+ onError: (error) => {
28
+ console.error(error);
29
+ }
30
+ }),
31
+ defaultOptions: {
32
+ queries: {
33
+ staleTime: 5 * 1e3
34
+ }
35
+ }
36
+ });
37
+ exports.default = graphqlRequestClient;
38
+ exports.graphqlRequestClient = graphqlRequestClient;
@@ -0,0 +1,38 @@
1
+ import { QueryClient, QueryCache } from "@tanstack/react-query";
2
+ import { GraphQLClient } from "graphql-request";
3
+ const GRAPHQL_ENDPOINT = process.env.NEXT_PUBLIC_GRAPHQL_URL;
4
+ const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
5
+ const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
6
+ const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
7
+ const graphqlRequestClient = new GraphQLClient(GRAPHQL_ENDPOINT, {
8
+ credentials: "include",
9
+ mode: "cors",
10
+ fetch,
11
+ headers: {
12
+ Authorization: `Bearer ${AUTH_TOKEN}`
13
+ }
14
+ });
15
+ new GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
16
+ credentials: "include",
17
+ mode: "cors",
18
+ fetch,
19
+ headers: {
20
+ Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
21
+ }
22
+ });
23
+ new QueryClient({
24
+ queryCache: new QueryCache({
25
+ onError: (error) => {
26
+ console.error(error);
27
+ }
28
+ }),
29
+ defaultOptions: {
30
+ queries: {
31
+ staleTime: 5 * 1e3
32
+ }
33
+ }
34
+ });
35
+ export {
36
+ graphqlRequestClient as default,
37
+ graphqlRequestClient
38
+ };
@@ -0,0 +1,8 @@
1
+ import type { TToken } from '@okam/stack-ui';
2
+ import type { TypedQueryDocumentNode } from 'graphql';
3
+ import type { TBlockSerializerProps } from '../components/BlockSerializer/interface';
4
+ import type { TBlockQuery, TBlockVariables, TCommonBlockFragment } from '../types/block';
5
+ declare function useBlock<T extends TCommonBlockFragment>(props: TBlockSerializerProps<T>, blockKey: string, doc?: TypedQueryDocumentNode<TBlockQuery<T>, TBlockVariables>): Promise<T & {
6
+ cmsTokens: TToken;
7
+ }>;
8
+ export default useBlock;
package/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
+ export type { BlockSettingsFragment } from './generated/graphql';
1
2
  export type { TBlockDispatcherProps } from './components/BlockDispatcher/interface';
2
3
  export type { TBlockSerializerConfig, TBlockSerializerProps, TBlock, TBlockSerializerConfigComponent, } from './components/BlockSerializer/interface';
3
4
  export type { TAdditionalProps, TCommonBlockFragment, TBlockQuery, TBlockDocument, TBlockVariables, } from './types/block';
4
5
  export { default as BlockWysiwyg } from './blocks/BlockWysiwyg';
6
+ export { default as useBlock } from './hooks/useBlock';
5
7
  export { default as blockWysiwygConfig } from './blocks/BlockWysiwyg/config';
6
8
  export { default as baseConfig } from './components/BlockDispatcher/config';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okam/directus-block",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
@@ -1,117 +0,0 @@
1
- import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
- import { Box, WysiwygBlock } from "@okam/stack-ui";
3
- import React from "react";
4
- import { QueryClient, QueryCache } from "@tanstack/react-query";
5
- import { isEmpty } from "radash";
6
- import { GraphQLClient } from "graphql-request";
7
- const BlockSettingsFragmentDoc = { "kind": "Document", "definitions": [{ "kind": "FragmentDefinition", "name": { "kind": "Name", "value": "BlockSettings" }, "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "block_settings" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "tokens" } }, { "kind": "Field", "name": { "kind": "Name", "value": "variant" } }] } }] };
8
- function useFragment(_documentNode, fragmentType) {
9
- return fragmentType;
10
- }
11
- const GRAPHQL_ENDPOINT = process.env.NEXT_PUBLIC_GRAPHQL_URL;
12
- const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
13
- const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
14
- const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
15
- const graphqlRequestClient = new GraphQLClient(GRAPHQL_ENDPOINT, {
16
- credentials: "include",
17
- mode: "cors",
18
- fetch,
19
- headers: {
20
- Authorization: `Bearer ${AUTH_TOKEN}`
21
- }
22
- });
23
- new GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
24
- credentials: "include",
25
- mode: "cors",
26
- fetch,
27
- headers: {
28
- Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
29
- }
30
- });
31
- new QueryClient({
32
- queryCache: new QueryCache({
33
- onError: (error) => {
34
- console.error(error);
35
- }
36
- }),
37
- defaultOptions: {
38
- queries: {
39
- staleTime: 5 * 1e3
40
- }
41
- }
42
- });
43
- function queryGql(document, queryKey, client = graphqlRequestClient) {
44
- return client.request(document, {
45
- ...queryKey
46
- });
47
- }
48
- process.env.NEXT_PUBLIC_GRAPHQL_URL;
49
- process.env.NEXT_PUBLIC_API_TOKEN;
50
- function isVariables(maybeVariables) {
51
- return !!maybeVariables;
52
- }
53
- function isOnlyIdInItem(item) {
54
- return !isEmpty(item) && Object.keys(item).length === 1 && Object.keys(item)[0] === "id" && !!item.id;
55
- }
56
- async function queryFromVariables(params) {
57
- const { document, blockKey, variables } = params;
58
- if (!document || !isVariables(variables)) return null;
59
- const queriedBlockProps = await queryGql(document, variables);
60
- if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
61
- const queriedBlockFragment = queriedBlockProps[blockKey];
62
- return queriedBlockFragment;
63
- }
64
- async function getBlockProps(params) {
65
- const { document, item, blockKey, variables } = params;
66
- if (item) {
67
- if (!isOnlyIdInItem(item)) {
68
- return item;
69
- }
70
- const variablesWithFallback = { id: item.id, ...variables };
71
- if (!isVariables(variablesWithFallback)) return null;
72
- return queryFromVariables({
73
- ...params,
74
- variables: variablesWithFallback
75
- });
76
- }
77
- if (!document || !isVariables(variables)) return null;
78
- const queriedBlockProps = await queryGql(document, variables);
79
- if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
80
- const queriedBlockFragment = queriedBlockProps[blockKey];
81
- return queriedBlockFragment;
82
- }
83
- const BlockWysiwyg = async (props) => {
84
- const { variables, themeName = "wysiwyg", tokens, item, document } = props;
85
- const propsWithFallback = await getBlockProps({
86
- item,
87
- blockKey: "block_wysiwyg_by_id",
88
- document,
89
- variables
90
- });
91
- if (!propsWithFallback) return null;
92
- const { content, title, level, settings } = propsWithFallback;
93
- const { tokens: cmsTokens } = useFragment(BlockSettingsFragmentDoc, settings) ?? {};
94
- if (!content && !(title && level)) return null;
95
- return /* @__PURE__ */ jsxs(Fragment, { children: [
96
- title && level && /* @__PURE__ */ jsx(Box, { as: "span", themeName, tokens: { ...tokens, ...cmsTokens }, children: React.createElement(level, {}, title) }),
97
- content && /* @__PURE__ */ jsx(WysiwygBlock, { themeName, tokens: { ...tokens, ...cmsTokens }, content })
98
- ] });
99
- };
100
- const blockWysiwygConfig = {
101
- block_wysiwyg: {
102
- default: (props) => /* @__PURE__ */ jsx(BlockWysiwyg, { ...props })
103
- }
104
- };
105
- const baseConfig = {
106
- components: {
107
- ...blockWysiwygConfig
108
- }
109
- };
110
- export {
111
- BlockWysiwyg as B,
112
- baseConfig as a,
113
- blockWysiwygConfig as b,
114
- BlockSettingsFragmentDoc as c,
115
- getBlockProps as g,
116
- useFragment as u
117
- };
@@ -1,116 +0,0 @@
1
- "use strict";
2
- const jsxRuntime = require("react/jsx-runtime");
3
- const stackUi = require("@okam/stack-ui");
4
- const React = require("react");
5
- const reactQuery = require("@tanstack/react-query");
6
- const radash = require("radash");
7
- const graphqlRequest = require("graphql-request");
8
- const BlockSettingsFragmentDoc = { "kind": "Document", "definitions": [{ "kind": "FragmentDefinition", "name": { "kind": "Name", "value": "BlockSettings" }, "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "block_settings" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "tokens" } }, { "kind": "Field", "name": { "kind": "Name", "value": "variant" } }] } }] };
9
- function useFragment(_documentNode, fragmentType) {
10
- return fragmentType;
11
- }
12
- const GRAPHQL_ENDPOINT = process.env.NEXT_PUBLIC_GRAPHQL_URL;
13
- const GRAPHQL_ENDPOINT_ADMIN = process.env.NEXT_GRAPHQL_URL_ADMIN;
14
- const AUTH_TOKEN = process.env.NEXT_PUBLIC_API_TOKEN ?? "";
15
- const AUTH_TOKEN_ADMIN = process.env.NEXT_PUBLIC_API_TOKEN;
16
- const graphqlRequestClient = new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT, {
17
- credentials: "include",
18
- mode: "cors",
19
- fetch,
20
- headers: {
21
- Authorization: `Bearer ${AUTH_TOKEN}`
22
- }
23
- });
24
- new graphqlRequest.GraphQLClient(GRAPHQL_ENDPOINT_ADMIN, {
25
- credentials: "include",
26
- mode: "cors",
27
- fetch,
28
- headers: {
29
- Authorization: `Bearer ${AUTH_TOKEN_ADMIN}`
30
- }
31
- });
32
- new reactQuery.QueryClient({
33
- queryCache: new reactQuery.QueryCache({
34
- onError: (error) => {
35
- console.error(error);
36
- }
37
- }),
38
- defaultOptions: {
39
- queries: {
40
- staleTime: 5 * 1e3
41
- }
42
- }
43
- });
44
- function queryGql(document, queryKey, client = graphqlRequestClient) {
45
- return client.request(document, {
46
- ...queryKey
47
- });
48
- }
49
- process.env.NEXT_PUBLIC_GRAPHQL_URL;
50
- process.env.NEXT_PUBLIC_API_TOKEN;
51
- function isVariables(maybeVariables) {
52
- return !!maybeVariables;
53
- }
54
- function isOnlyIdInItem(item) {
55
- return !radash.isEmpty(item) && Object.keys(item).length === 1 && Object.keys(item)[0] === "id" && !!item.id;
56
- }
57
- async function queryFromVariables(params) {
58
- const { document, blockKey, variables } = params;
59
- if (!document || !isVariables(variables)) return null;
60
- const queriedBlockProps = await queryGql(document, variables);
61
- if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
62
- const queriedBlockFragment = queriedBlockProps[blockKey];
63
- return queriedBlockFragment;
64
- }
65
- async function getBlockProps(params) {
66
- const { document, item, blockKey, variables } = params;
67
- if (item) {
68
- if (!isOnlyIdInItem(item)) {
69
- return item;
70
- }
71
- const variablesWithFallback = { id: item.id, ...variables };
72
- if (!isVariables(variablesWithFallback)) return null;
73
- return queryFromVariables({
74
- ...params,
75
- variables: variablesWithFallback
76
- });
77
- }
78
- if (!document || !isVariables(variables)) return null;
79
- const queriedBlockProps = await queryGql(document, variables);
80
- if (!queriedBlockProps || typeof queriedBlockProps !== "object" || !blockKey) return null;
81
- const queriedBlockFragment = queriedBlockProps[blockKey];
82
- return queriedBlockFragment;
83
- }
84
- const BlockWysiwyg = async (props) => {
85
- const { variables, themeName = "wysiwyg", tokens, item, document } = props;
86
- const propsWithFallback = await getBlockProps({
87
- item,
88
- blockKey: "block_wysiwyg_by_id",
89
- document,
90
- variables
91
- });
92
- if (!propsWithFallback) return null;
93
- const { content, title, level, settings } = propsWithFallback;
94
- const { tokens: cmsTokens } = useFragment(BlockSettingsFragmentDoc, settings) ?? {};
95
- if (!content && !(title && level)) return null;
96
- return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
97
- title && level && /* @__PURE__ */ jsxRuntime.jsx(stackUi.Box, { as: "span", themeName, tokens: { ...tokens, ...cmsTokens }, children: React.createElement(level, {}, title) }),
98
- content && /* @__PURE__ */ jsxRuntime.jsx(stackUi.WysiwygBlock, { themeName, tokens: { ...tokens, ...cmsTokens }, content })
99
- ] });
100
- };
101
- const blockWysiwygConfig = {
102
- block_wysiwyg: {
103
- default: (props) => /* @__PURE__ */ jsxRuntime.jsx(BlockWysiwyg, { ...props })
104
- }
105
- };
106
- const baseConfig = {
107
- components: {
108
- ...blockWysiwygConfig
109
- }
110
- };
111
- exports.BlockSettingsFragmentDoc = BlockSettingsFragmentDoc;
112
- exports.BlockWysiwyg = BlockWysiwyg;
113
- exports.baseConfig = baseConfig;
114
- exports.blockWysiwygConfig = blockWysiwygConfig;
115
- exports.getBlockProps = getBlockProps;
116
- exports.useFragment = useFragment;
package/index.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const config = require("./config-Wm_s8RKf.js");
4
- exports.BlockWysiwyg = config.BlockWysiwyg;
5
- exports.baseConfig = config.baseConfig;
6
- exports.blockWysiwygConfig = config.blockWysiwygConfig;
package/index.mjs DELETED
@@ -1,6 +0,0 @@
1
- import { B, a, b } from "./config-BNCxf6ZM.mjs";
2
- export {
3
- B as BlockWysiwyg,
4
- a as baseConfig,
5
- b as blockWysiwygConfig
6
- };
package/server.js DELETED
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const jsxRuntime = require("react/jsx-runtime");
4
- require("@tanstack/react-query");
5
- require("radash");
6
- const config = require("./config-Wm_s8RKf.js");
7
- require("graphql-request");
8
- function mergeConfigs(baseConfig, ...configs) {
9
- const finalConfig = configs.reduce((mergedConfig, config2) => {
10
- if (!config2) return mergedConfig;
11
- return { components: { ...mergedConfig.components, ...config2.components } };
12
- }, baseConfig);
13
- return finalConfig;
14
- }
15
- const BlockSerializer = (props) => {
16
- var _a, _b, _c;
17
- const { item, collection, config: config$1, variables, defaultVariant, ...rest } = props;
18
- if (!collection || !config$1) return null;
19
- const blockConfig = (_a = config$1 == null ? void 0 : config$1.components) == null ? void 0 : _a[collection];
20
- if (!blockConfig) return null;
21
- const { settings } = item ?? {};
22
- const id = (item == null ? void 0 : item.id) ?? (variables == null ? void 0 : variables.id);
23
- if (!id) return null;
24
- const { variant } = config.useFragment(config.BlockSettingsFragmentDoc, settings) ?? {};
25
- const variantWithFallback = ((_b = blockConfig.getVariant) == null ? void 0 : _b.call(blockConfig, props)) ?? variant ?? defaultVariant;
26
- const { default: defaultBlockComponent, document } = blockConfig;
27
- const variantBlockComponent = (_c = blockConfig.variants) == null ? void 0 : _c[variantWithFallback ?? ""];
28
- const BlockComponent = variantBlockComponent ?? defaultBlockComponent;
29
- if (!BlockComponent) return null;
30
- return /* @__PURE__ */ jsxRuntime.jsx(
31
- BlockComponent,
32
- {
33
- document,
34
- config: config$1,
35
- collection,
36
- item,
37
- ...id ? { variables: { ...variables, id } } : { variables },
38
- ...rest
39
- },
40
- id
41
- );
42
- };
43
- const BlockDispatcher = (props) => {
44
- const { children, config: config$1, ...rest } = props;
45
- const finalConfig = mergeConfigs(config.baseConfig, config$1);
46
- const renderBlock = (blockProps) => {
47
- return children ? children({ ...rest, ...blockProps, config: finalConfig }) : /* @__PURE__ */ jsxRuntime.jsx(BlockSerializer, { ...rest, ...blockProps, config: finalConfig });
48
- };
49
- if ("blocks" in props) {
50
- const { blocks } = props;
51
- return blocks.map(renderBlock);
52
- }
53
- const { block } = props;
54
- return renderBlock(block);
55
- };
56
- exports.getBlockProps = config.getBlockProps;
57
- exports.BlockDispatcher = BlockDispatcher;
58
- exports.BlockSerializer = BlockSerializer;
59
- exports.mergeConfigs = mergeConfigs;