@okam/directus-block 1.2.1 → 1.2.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.
Files changed (49) hide show
  1. package/blocks/BlockWysiwyg/config.d.ts +4 -0
  2. package/blocks/BlockWysiwyg/index.d.ts +4 -0
  3. package/blocks/BlockWysiwyg/interface.d.ts +17 -0
  4. package/components/BlockDispatcher/config.d.ts +3 -0
  5. package/components/BlockDispatcher/index.d.ts +5 -0
  6. package/components/BlockDispatcher/interface.d.ts +16 -0
  7. package/components/BlockSerializer/index.d.ts +3 -0
  8. package/components/BlockSerializer/interface.d.ts +31 -0
  9. package/config-BNCxf6ZM.mjs +117 -0
  10. package/config-Wm_s8RKf.js +116 -0
  11. package/generated/fragment-masking.d.ts +15 -0
  12. package/generated/graphql.d.ts +64 -0
  13. package/index.d.ts +6 -0
  14. package/index.js +6 -0
  15. package/index.mjs +6 -0
  16. package/package.json +5 -1
  17. package/{src/server.ts → server.d.ts} +4 -6
  18. package/server.js +59 -0
  19. package/server.mjs +60 -0
  20. package/types/block.d.ts +27 -0
  21. package/utils/get-block-props.d.ts +17 -0
  22. package/utils/index.d.ts +4 -0
  23. package/utils/merge-configs.d.ts +9 -0
  24. package/.eslintrc.js +0 -21
  25. package/.storybook/.eslintrc.js +0 -29
  26. package/.storybook/main.ts +0 -33
  27. package/.storybook/preview.ts +0 -0
  28. package/.storybook/tsconfig.json +0 -17
  29. package/CHANGELOG.md +0 -25
  30. package/README.md +0 -167
  31. package/project.json +0 -36
  32. package/src/blocks/BlockWysiwyg/config.tsx +0 -10
  33. package/src/blocks/BlockWysiwyg/index.tsx +0 -38
  34. package/src/blocks/BlockWysiwyg/interface.ts +0 -13
  35. package/src/components/BlockDispatcher/config.ts +0 -10
  36. package/src/components/BlockDispatcher/index.tsx +0 -30
  37. package/src/components/BlockDispatcher/interface.ts +0 -17
  38. package/src/components/BlockSerializer/index.tsx +0 -43
  39. package/src/components/BlockSerializer/interface.ts +0 -51
  40. package/src/generated/fragment-masking.ts +0 -66
  41. package/src/generated/graphql.ts +0 -31
  42. package/src/index.ts +0 -22
  43. package/src/types/block.ts +0 -40
  44. package/src/utils/get-block-props.ts +0 -81
  45. package/src/utils/index.ts +0 -5
  46. package/src/utils/merge-configs.ts +0 -21
  47. package/tsconfig.json +0 -17
  48. package/tsconfig.lib.json +0 -23
  49. package/vite.config.ts +0 -51
@@ -0,0 +1,4 @@
1
+ import type { TBlockSerializerConfigComponent } from '../../components/BlockSerializer/interface';
2
+ import type { BlockWysiwygFragment } from './interface';
3
+ declare const blockWysiwygConfig: TBlockSerializerConfigComponent<BlockWysiwygFragment>;
4
+ export default blockWysiwygConfig;
@@ -0,0 +1,4 @@
1
+ import type { TBlockSerializerProps } from '../../components/BlockSerializer/interface';
2
+ import type { BlockWysiwygFragment } from './interface';
3
+ declare const BlockWysiwyg: (props: TBlockSerializerProps<BlockWysiwygFragment>) => Promise<import("react/jsx-runtime").JSX.Element | null>;
4
+ export default BlockWysiwyg;
@@ -0,0 +1,17 @@
1
+ import type { BlockSettingsFragment } from '../../generated/graphql';
2
+ export type BlockWysiwygFragment = {
3
+ __typename?: 'block_wysiwyg';
4
+ title?: string | null;
5
+ content?: string | null;
6
+ level?: string | null;
7
+ variant?: string | null;
8
+ settings?: ({
9
+ __typename?: 'block_settings';
10
+ } & {
11
+ ' $fragmentRefs'?: {
12
+ BlockSettingsFragment: BlockSettingsFragment;
13
+ };
14
+ }) | null;
15
+ } & {
16
+ ' $fragmentName'?: 'BlockWysiwygFragment';
17
+ };
@@ -0,0 +1,3 @@
1
+ import type { TBlockSerializerConfig } from '../BlockSerializer/interface';
2
+ declare const baseConfig: TBlockSerializerConfig;
3
+ export default baseConfig;
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ /// <reference types="react/experimental" />
3
+ import type { TBlockDispatcherProps } from './interface';
4
+ declare const BlockDispatcher: (props: TBlockDispatcherProps) => string | number | boolean | Iterable<import("react").ReactNode> | import("react").PromiseLikeOfReactNode | import("react/jsx-runtime").JSX.Element | (string | number | boolean | Iterable<import("react").ReactNode> | import("react").PromiseLikeOfReactNode | import("react/jsx-runtime").JSX.Element | null | undefined)[] | null | undefined;
5
+ export default BlockDispatcher;
@@ -0,0 +1,16 @@
1
+ import type { TDefaultComponent, Nullable } from '@okam/stack-ui';
2
+ import type React from 'react';
3
+ import type { TAdditionalProps } from '../../types/block';
4
+ import type { TBlockSerializerProps, TBlockSerializerConfig } from '../BlockSerializer/interface';
5
+ interface TBaseBlockDispatcherProps<AdditionalProps extends TAdditionalProps = TAdditionalProps> extends Omit<TDefaultComponent, 'children'> {
6
+ config?: TBlockSerializerConfig;
7
+ defaultVariant?: string;
8
+ additionalProps?: AdditionalProps;
9
+ children?: (props: TBlockSerializerProps) => React.ReactNode;
10
+ }
11
+ export type TBlockDispatcherProps<AdditionalProps extends TAdditionalProps = TAdditionalProps> = (TBaseBlockDispatcherProps<AdditionalProps> & {
12
+ block: Nullable<TBlockSerializerProps>;
13
+ }) | (TBaseBlockDispatcherProps<AdditionalProps> & {
14
+ blocks: Nullable<TBlockSerializerProps>[];
15
+ });
16
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { TBlockSerializerProps } from './interface';
2
+ declare const BlockSerializer: (props: TBlockSerializerProps) => import("react/jsx-runtime").JSX.Element | null;
3
+ export default BlockSerializer;
@@ -0,0 +1,31 @@
1
+ import { type TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ import type { Nullable, TDefaultComponent } from '@okam/stack-ui';
3
+ import type { Variables } from 'graphql-request';
4
+ import type { FunctionComponent } from 'react';
5
+ import type { TCommonBlockFragment, TAdditionalProps, TBlockQuery, TBlockVariables, TBlockDocument } from '../../types/block';
6
+ export interface TBlock<BlockFragment extends TCommonBlockFragment = TCommonBlockFragment, BlockVariables extends Variables = Variables> {
7
+ collection?: Nullable<string>;
8
+ item?: Nullable<BlockFragment>;
9
+ variables?: TBlockVariables<BlockVariables>;
10
+ document?: TypedDocumentNode<TBlockQuery<BlockFragment>, BlockVariables>;
11
+ }
12
+ export type TBlockSerializerProps<BlockFragment extends TCommonBlockFragment = TCommonBlockFragment, BlockVariables extends Variables = TBlockVariables, AdditionalProps extends TAdditionalProps = TAdditionalProps> = TBlock<BlockFragment, BlockVariables> & TDefaultComponent & {
13
+ config?: TBlockSerializerConfig;
14
+ defaultVariant?: string;
15
+ additionalProps?: AdditionalProps;
16
+ };
17
+ export type TBlockFunctionComponent<BlockFragment extends TCommonBlockFragment = TCommonBlockFragment> = FunctionComponent<TBlockSerializerProps<BlockFragment>>;
18
+ export type TBlockSerializerConfigComponent<BlockFragment extends TCommonBlockFragment = TCommonBlockFragment> = {
19
+ [blockKey: string]: {
20
+ default: TBlockFunctionComponent<BlockFragment>;
21
+ document?: TBlockDocument<BlockFragment>;
22
+ defaultVariant?: string;
23
+ getVariant?: (props: TBlockSerializerProps<BlockFragment>) => Nullable<string>;
24
+ variants?: {
25
+ [blockVariant: string]: TBlockFunctionComponent<BlockFragment>;
26
+ };
27
+ };
28
+ };
29
+ export interface TBlockSerializerConfig {
30
+ components: TBlockSerializerConfigComponent;
31
+ }
@@ -0,0 +1,117 @@
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
+ };
@@ -0,0 +1,116 @@
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;
@@ -0,0 +1,15 @@
1
+ import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ import { Incremental } from './graphql';
3
+ export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<infer TType, any> ? [TType] extends [{
4
+ ' $fragmentName'?: infer TKey;
5
+ }] ? TKey extends string ? {
6
+ ' $fragmentRefs'?: {
7
+ [key in TKey]: TType;
8
+ };
9
+ } : never : never : never;
10
+ export declare function useFragment<TType>(_documentNode: DocumentTypeDecoration<TType, any>, fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>): TType;
11
+ export declare function useFragment<TType>(_documentNode: DocumentTypeDecoration<TType, any>, fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined): TType | null | undefined;
12
+ export declare function useFragment<TType>(_documentNode: DocumentTypeDecoration<TType, any>, fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>): ReadonlyArray<TType>;
13
+ export declare function useFragment<TType>(_documentNode: DocumentTypeDecoration<TType, any>, fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined): ReadonlyArray<TType> | null | undefined;
14
+ export declare function makeFragmentData<F extends DocumentTypeDecoration<any, any>, FT extends ResultOf<F>>(data: FT, _fragment: F): FragmentType<F>;
15
+ export declare function isFragmentReady<TQuery, TFrag>(queryNode: DocumentTypeDecoration<TQuery, any>, fragmentNode: TypedDocumentNode<TFrag>, data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined): data is FragmentType<typeof fragmentNode>;
@@ -0,0 +1,64 @@
1
+ import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
2
+ export type Exact<T extends {
3
+ [key: string]: unknown;
4
+ }> = {
5
+ [K in keyof T]: T[K];
6
+ };
7
+ export type Incremental<T> = T | {
8
+ [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never;
9
+ };
10
+ export type Scalars = {
11
+ ID: {
12
+ input: string;
13
+ output: string;
14
+ };
15
+ String: {
16
+ input: string;
17
+ output: string;
18
+ };
19
+ Boolean: {
20
+ input: boolean;
21
+ output: boolean;
22
+ };
23
+ Int: {
24
+ input: number;
25
+ output: number;
26
+ };
27
+ Float: {
28
+ input: number;
29
+ output: number;
30
+ };
31
+ /** ISO8601 Date values */
32
+ Date: {
33
+ input: any;
34
+ output: any;
35
+ };
36
+ /** BigInt value */
37
+ GraphQLBigInt: {
38
+ input: any;
39
+ output: any;
40
+ };
41
+ /** A Float or a String */
42
+ GraphQLStringOrFloat: {
43
+ input: any;
44
+ output: any;
45
+ };
46
+ /** Hashed string values */
47
+ Hash: {
48
+ input: any;
49
+ output: any;
50
+ };
51
+ /** The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). */
52
+ JSON: {
53
+ input: any;
54
+ output: any;
55
+ };
56
+ };
57
+ export type BlockSettingsFragment = {
58
+ __typename?: 'block_settings';
59
+ tokens?: any | null;
60
+ variant?: string | null;
61
+ } & {
62
+ ' $fragmentName'?: 'BlockSettingsFragment';
63
+ };
64
+ export declare const BlockSettingsFragmentDoc: DocumentNode<BlockSettingsFragment, unknown>;
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export type { TBlockDispatcherProps } from './components/BlockDispatcher/interface';
2
+ export type { TBlockSerializerConfig, TBlockSerializerProps, TBlock, TBlockSerializerConfigComponent, } from './components/BlockSerializer/interface';
3
+ export type { TAdditionalProps, TCommonBlockFragment, TBlockQuery, TBlockDocument, TBlockVariables, } from './types/block';
4
+ export { default as BlockWysiwyg } from './blocks/BlockWysiwyg';
5
+ export { default as blockWysiwygConfig } from './blocks/BlockWysiwyg/config';
6
+ export { default as baseConfig } from './components/BlockDispatcher/config';
package/index.js ADDED
@@ -0,0 +1,6 @@
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 ADDED
@@ -0,0 +1,6 @@
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/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@okam/directus-block",
3
+ "version": "1.2.3",
3
4
  "main": "./index.js",
4
- "version": "1.2.1",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
7
7
  ".": {
8
8
  "import": "./index.mjs",
9
9
  "require": "./index.js"
10
+ },
11
+ "./server": {
12
+ "import": "./server.mjs",
13
+ "require": "./server.js"
10
14
  }
11
15
  },
12
16
  "publishConfig": {
@@ -1,6 +1,4 @@
1
- 'server-only'
2
-
3
- export { default as BlockDispatcher } from './components/BlockDispatcher'
4
- export { default as BlockSerializer } from './components/BlockSerializer'
5
- export { default as getBlockProps } from './utils/get-block-props'
6
- export { default as mergeConfigs } from './utils/merge-configs'
1
+ export { default as BlockDispatcher } from './components/BlockDispatcher';
2
+ export { default as BlockSerializer } from './components/BlockSerializer';
3
+ export { default as getBlockProps } from './utils/get-block-props';
4
+ export { default as mergeConfigs } from './utils/merge-configs';
package/server.js ADDED
@@ -0,0 +1,59 @@
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;
package/server.mjs ADDED
@@ -0,0 +1,60 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import "@tanstack/react-query";
3
+ 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 "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
+ }
14
+ const BlockSerializer = (props) => {
15
+ var _a, _b, _c;
16
+ const { item, collection, config, variables, defaultVariant, ...rest } = props;
17
+ if (!collection || !config) return null;
18
+ const blockConfig = (_a = config == null ? void 0 : config.components) == null ? void 0 : _a[collection];
19
+ if (!blockConfig) return null;
20
+ const { settings } = item ?? {};
21
+ const id = (item == null ? void 0 : item.id) ?? (variables == null ? void 0 : variables.id);
22
+ if (!id) return null;
23
+ const { variant } = useFragment(BlockSettingsFragmentDoc, settings) ?? {};
24
+ const variantWithFallback = ((_b = blockConfig.getVariant) == null ? void 0 : _b.call(blockConfig, props)) ?? variant ?? defaultVariant;
25
+ const { default: defaultBlockComponent, document } = blockConfig;
26
+ const variantBlockComponent = (_c = blockConfig.variants) == null ? void 0 : _c[variantWithFallback ?? ""];
27
+ const BlockComponent = variantBlockComponent ?? defaultBlockComponent;
28
+ if (!BlockComponent) return null;
29
+ return /* @__PURE__ */ jsx(
30
+ BlockComponent,
31
+ {
32
+ document,
33
+ config,
34
+ collection,
35
+ item,
36
+ ...id ? { variables: { ...variables, id } } : { variables },
37
+ ...rest
38
+ },
39
+ id
40
+ );
41
+ };
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
+ export {
56
+ BlockDispatcher,
57
+ BlockSerializer,
58
+ g as getBlockProps,
59
+ mergeConfigs
60
+ };
@@ -0,0 +1,27 @@
1
+ import type { DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ import type { Nullable } from '@okam/stack-ui';
3
+ import type { Variables } from 'graphql-request';
4
+ import type { FragmentType } from '../generated/fragment-masking';
5
+ import type { BlockSettingsFragment } from '../generated/graphql';
6
+ export type TAdditionalProps = {
7
+ [key: string]: unknown;
8
+ };
9
+ type BlockSettings = FragmentType<DocumentTypeDecoration<BlockSettingsFragment, unknown>>;
10
+ export type TCommonBlockFragment = {
11
+ id?: Nullable<string>;
12
+ settings?: Nullable<BlockSettings>;
13
+ } & Record<string, unknown>;
14
+ export type TBlockQuery<BlockFragment extends TCommonBlockFragment> = {
15
+ __typename?: 'Query';
16
+ } & {
17
+ [blockKey: string]: {
18
+ ' $fragmentRefs'?: {
19
+ [blockFragmentKey: string]: BlockFragment;
20
+ } | null | undefined;
21
+ } | null | undefined;
22
+ };
23
+ export type TBlockVariables<BlockVariables extends Variables = Variables> = {
24
+ id: string;
25
+ } & BlockVariables;
26
+ export type TBlockDocument<BlockFragment extends TCommonBlockFragment, BlockVariables extends Variables = Variables> = TypedDocumentNode<TBlockQuery<BlockFragment>, TBlockVariables<BlockVariables>>;
27
+ export {};
@@ -0,0 +1,17 @@
1
+ import type { Nullable } from '@okam/stack-ui';
2
+ import type { Variables } from 'graphql-request';
3
+ import type { TBlockDocument, TBlockQuery, TBlockVariables, TCommonBlockFragment } from '../types/block';
4
+ type TGetBlockPropsParams<BlockFragment extends TCommonBlockFragment, BlockVariables extends Variables = Variables> = {
5
+ document?: TBlockDocument<BlockFragment, BlockVariables>;
6
+ item?: Nullable<NonNullable<NonNullable<TBlockQuery<BlockFragment>[string]>[' $fragmentRefs']>[string]>;
7
+ blockKey?: string;
8
+ variables?: TBlockVariables<BlockVariables>;
9
+ };
10
+ /**
11
+ * Returns the passed item if it is defined. Otherwise, queried its own block
12
+ * @param params.blockKey Key of the queried field
13
+ * @param params.item Item of the block. If null or only contains the block's id, the function will make a query
14
+ * @returns The block data
15
+ */
16
+ export default function getBlockProps<BlockFragment extends TCommonBlockFragment, BlockVariables extends Variables = Variables>(params: TGetBlockPropsParams<BlockFragment, BlockVariables>): Promise<BlockFragment | null | undefined>;
17
+ export {};
@@ -0,0 +1,4 @@
1
+ import { useFragment } from '../generated/fragment-masking';
2
+ export { default as getBlockProps } from './get-block-props';
3
+ export { default as mergeConfigs } from './merge-configs';
4
+ export { useFragment as getFragment };
@@ -0,0 +1,9 @@
1
+ import type { Nullable } from '@okam/stack-ui';
2
+ import type { TBlockSerializerConfig } from '../components/BlockSerializer/interface';
3
+ /**
4
+ * Merges multiple block dispatcher configs
5
+ * @param baseConfig The base configuration. Other configurations will be prioritized over this one, which acts as a fallback
6
+ * @param configs Array of block dispatcher serializers to merge. Later elements will always be prioritized over first elements
7
+ * @returns Merged config
8
+ */
9
+ export default function mergeConfigs(baseConfig: TBlockSerializerConfig, ...configs: Nullable<TBlockSerializerConfig>[]): TBlockSerializerConfig;