@okam/directus-block 1.2.0 → 1.2.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/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.2.1 (2024-07-05)
2
+
3
+ This was a version bump only for directus-block to align it with other projects, there were no code changes.
4
+
1
5
  ## 1.2.0 (2024-06-28)
2
6
 
3
7
 
package/README.md CHANGED
@@ -35,6 +35,12 @@ This would allow to remove a lot of repetitive code accross blocks definitions,
35
35
 
36
36
  This component calls the good component in the configuration from the `collection` prop
37
37
 
38
+ #### Props
39
+
40
+ - item: The block's data. Can either contain just the block's `id`, be null (in that case, the `id` will need to be sent using the block's variables). If `item` only contains the id, it will be sent to the variables for making the query
41
+ - variables: The block's variables. Passing the id is necessary
42
+ - document: Can also be passed in the config. The document that will be used to make a query
43
+
38
44
  ## Configuration
39
45
 
40
46
  A configuration uses the `components` prop to map a key value, like so:
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@okam/directus-block",
3
3
  "main": "./index.js",
4
- "version": "1.2.0",
4
+ "version": "1.2.1",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
7
7
  ".": {
package/project.json CHANGED
@@ -13,44 +13,23 @@
13
13
  }
14
14
  },
15
15
  "build": {
16
- "executor": "@nx/vite:build",
16
+ "executor": "@nx/rollup:rollup",
17
17
  "outputs": ["{options.outputPath}"],
18
- "defaultConfiguration": "production",
19
18
  "options": {
20
- "outputPath": "dist/libs/directus/directus-block"
21
- },
22
- "configurations": {
23
- "development": {
24
- "mode": "development"
25
- },
26
- "production": {
27
- "mode": "production"
28
- }
29
- }
30
- },
31
- "storybook": {
32
- "executor": "@nx/storybook:storybook",
33
- "options": {
34
- "port": 4400,
35
- "configDir": "libs/directus/directus-block/.storybook"
36
- },
37
- "configurations": {
38
- "ci": {
39
- "quiet": true
40
- }
41
- }
42
- },
43
- "build-storybook": {
44
- "executor": "@nx/storybook:build",
45
- "outputs": ["{options.outputDir}"],
46
- "options": {
47
- "outputDir": "dist/storybook/directus-block",
48
- "configDir": "libs/directus/directus-block/.storybook"
49
- },
50
- "configurations": {
51
- "ci": {
52
- "quiet": true
53
- }
19
+ "outputPath": "dist/libs/directus/directus-block",
20
+ "tsConfig": "libs/directus/directus-block/tsconfig.lib.json",
21
+ "project": "libs/directus/directus-block/package.json",
22
+ "entryFile": "libs/directus/directus-block/src/index.ts",
23
+ "external": "all",
24
+ "rollupConfig": "@nx/react/plugins/bundle-rollup",
25
+ "compiler": "swc",
26
+ "assets": [
27
+ {
28
+ "glob": "libs/directus/directus-next-component/README.md",
29
+ "input": ".",
30
+ "output": "."
31
+ }
32
+ ]
54
33
  }
55
34
  }
56
35
  }
@@ -5,13 +5,16 @@ import type { TBlockSerializerProps } from './interface'
5
5
  const BlockSerializer = (props: TBlockSerializerProps) => {
6
6
  const { item, collection, config, variables, defaultVariant, ...rest } = props
7
7
 
8
- if (!collection || !item || !config) return null
8
+ if (!collection || !config) return null
9
9
 
10
10
  const blockConfig = config?.components?.[collection]
11
11
 
12
12
  if (!blockConfig) return null
13
13
 
14
- const { id, settings } = item
14
+ const { settings } = item ?? {}
15
+ const id = item?.id ?? variables?.id
16
+
17
+ if (!id) return null
15
18
 
16
19
  const { variant } = getFragment(BlockSettingsFragmentDoc, settings) ?? {}
17
20
 
package/src/index.ts CHANGED
@@ -1,7 +1,3 @@
1
- // Components
2
- export { default as BlockDispatcher } from './components/BlockDispatcher'
3
- export { default as BlockSerializer } from './components/BlockSerializer'
4
-
5
1
  // Types
6
2
  export type { TBlockDispatcherProps } from './components/BlockDispatcher/interface'
7
3
  export type {
@@ -21,10 +17,6 @@ export type {
21
17
  // Blocks
22
18
  export { default as BlockWysiwyg } from './blocks/BlockWysiwyg'
23
19
 
24
- // Utils
25
- export { default as getBlockProps } from './utils/get-block-props'
26
- export { default as mergeConfigs } from './utils/merge-configs'
27
-
28
20
  // Configs
29
21
  export { default as blockWysiwygConfig } from './blocks/BlockWysiwyg/config'
30
22
  export { default as baseConfig } from './components/BlockDispatcher/config'
package/src/server.ts ADDED
@@ -0,0 +1,6 @@
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'
@@ -3,6 +3,7 @@
3
3
  import { queryGql } from '@okam/directus-query'
4
4
  import type { Nullable } from '@okam/stack-ui'
5
5
  import type { Variables } from 'graphql-request'
6
+ import { isEmpty } from 'radash'
6
7
  import type { TBlockDocument, TBlockQuery, TBlockVariables, TCommonBlockFragment } from '../types/block'
7
8
 
8
9
  type TGetBlockPropsParams<BlockFragment extends TCommonBlockFragment, BlockVariables extends Variables = Variables> = {
@@ -18,10 +19,31 @@ function isVariables<BlockVariables extends Variables>(
18
19
  return !!maybeVariables
19
20
  }
20
21
 
22
+ function isOnlyIdInItem(item: TCommonBlockFragment): item is TBlockVariables {
23
+ return !isEmpty(item) && Object.keys(item).length === 1 && Object.keys(item)[0] === 'id' && !!item.id
24
+ }
25
+
26
+ async function queryFromVariables<
27
+ BlockFragment extends TCommonBlockFragment,
28
+ BlockVariables extends Variables = Variables,
29
+ >(params: TGetBlockPropsParams<BlockFragment, BlockVariables>) {
30
+ const { document, blockKey, variables } = params
31
+
32
+ if (!document || !isVariables<BlockVariables>(variables)) return null
33
+
34
+ const queriedBlockProps = await queryGql(document, variables)
35
+
36
+ if (!queriedBlockProps || typeof queriedBlockProps !== 'object' || !blockKey) return null
37
+
38
+ const queriedBlockFragment = queriedBlockProps[blockKey]
39
+
40
+ return queriedBlockFragment as BlockFragment
41
+ }
42
+
21
43
  /**
22
44
  * Returns the passed item if it is defined. Otherwise, queried its own block
23
45
  * @param params.blockKey Key of the queried field
24
- * @param params.item Item of the block. If null, the function will make a query
46
+ * @param params.item Item of the block. If null or only contains the block's id, the function will make a query
25
47
  * @returns The block data
26
48
  */
27
49
  export default async function getBlockProps<
@@ -30,9 +52,24 @@ export default async function getBlockProps<
30
52
  >(params: TGetBlockPropsParams<BlockFragment, BlockVariables>): Promise<BlockFragment | null | undefined> {
31
53
  const { document, item, blockKey, variables } = params
32
54
 
33
- if (item) return item
55
+ if (item) {
56
+ // If the item actually contains the block's data, just return it
57
+ if (!isOnlyIdInItem(item)) {
58
+ return item
59
+ }
34
60
 
35
- if (!document || !isVariables<BlockVariables>(variables)) return null
61
+ // Otherwise, the id necessary to make the query might be inside the item. Just in case, we have a fallback
62
+ const variablesWithFallback = { id: item.id, ...variables }
63
+
64
+ if (!isVariables<BlockVariables>(variablesWithFallback)) return null
65
+
66
+ return queryFromVariables({
67
+ ...params,
68
+ variables: variablesWithFallback,
69
+ })
70
+ }
71
+
72
+ if (!document || !isVariables(variables)) return null
36
73
 
37
74
  const queriedBlockProps = await queryGql(document, variables)
38
75
 
package/tsconfig.json CHANGED
@@ -11,9 +11,6 @@
11
11
  "references": [
12
12
  {
13
13
  "path": "./tsconfig.lib.json"
14
- },
15
- {
16
- "path": "./.storybook/tsconfig.json"
17
14
  }
18
15
  ],
19
16
  "extends": "../../../tsconfig.base.json"
package/tsconfig.lib.json CHANGED
@@ -18,8 +18,6 @@
18
18
  "**/*.test.js",
19
19
  "**/*.spec.jsx",
20
20
  "**/*.test.jsx",
21
- "**/*.stories.ts",
22
- "**/*.stories.js"
23
21
  ],
24
22
  "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
25
23
  }