@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
package/src/index.ts DELETED
@@ -1,22 +0,0 @@
1
- // Types
2
- export type { TBlockDispatcherProps } from './components/BlockDispatcher/interface'
3
- export type {
4
- TBlockSerializerConfig,
5
- TBlockSerializerProps,
6
- TBlock,
7
- TBlockSerializerConfigComponent,
8
- } from './components/BlockSerializer/interface'
9
- export type {
10
- TAdditionalProps,
11
- TCommonBlockFragment,
12
- TBlockQuery,
13
- TBlockDocument,
14
- TBlockVariables,
15
- } from './types/block'
16
-
17
- // Blocks
18
- export { default as BlockWysiwyg } from './blocks/BlockWysiwyg'
19
-
20
- // Configs
21
- export { default as blockWysiwygConfig } from './blocks/BlockWysiwyg/config'
22
- export { default as baseConfig } from './components/BlockDispatcher/config'
@@ -1,40 +0,0 @@
1
- /* eslint-disable @typescript-eslint/naming-convention */
2
- import type { DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core'
3
- import type { Nullable } from '@okam/stack-ui'
4
- import type { Variables } from 'graphql-request'
5
- import type { FragmentType } from '../generated/fragment-masking'
6
- import type { BlockSettingsFragment } from '../generated/graphql'
7
-
8
- export type TAdditionalProps = { [key: string]: unknown }
9
-
10
- type BlockSettings = FragmentType<DocumentTypeDecoration<BlockSettingsFragment, unknown>>
11
-
12
- export type TCommonBlockFragment = {
13
- id?: Nullable<string>
14
- settings?: Nullable<BlockSettings>
15
- } & Record<string, unknown>
16
-
17
- export type TBlockQuery<BlockFragment extends TCommonBlockFragment> = {
18
- __typename?: 'Query'
19
- } & {
20
- [blockKey: string]:
21
- | {
22
- ' $fragmentRefs'?:
23
- | {
24
- [blockFragmentKey: string]: BlockFragment
25
- }
26
- | null
27
- | undefined
28
- }
29
- | null
30
- | undefined
31
- }
32
-
33
- export type TBlockVariables<BlockVariables extends Variables = Variables> = {
34
- id: string
35
- } & BlockVariables
36
-
37
- export type TBlockDocument<
38
- BlockFragment extends TCommonBlockFragment,
39
- BlockVariables extends Variables = Variables,
40
- > = TypedDocumentNode<TBlockQuery<BlockFragment>, TBlockVariables<BlockVariables>>
@@ -1,81 +0,0 @@
1
- 'server-only'
2
-
3
- import { queryGql } from '@okam/directus-query'
4
- import type { Nullable } from '@okam/stack-ui'
5
- import type { Variables } from 'graphql-request'
6
- import { isEmpty } from 'radash'
7
- import type { TBlockDocument, TBlockQuery, TBlockVariables, TCommonBlockFragment } from '../types/block'
8
-
9
- type TGetBlockPropsParams<BlockFragment extends TCommonBlockFragment, BlockVariables extends Variables = Variables> = {
10
- document?: TBlockDocument<BlockFragment, BlockVariables>
11
- item?: Nullable<NonNullable<NonNullable<TBlockQuery<BlockFragment>[string]>[' $fragmentRefs']>[string]>
12
- blockKey?: string
13
- variables?: TBlockVariables<BlockVariables>
14
- }
15
-
16
- function isVariables<BlockVariables extends Variables>(
17
- maybeVariables: Nullable<Variables>,
18
- ): maybeVariables is BlockVariables {
19
- return !!maybeVariables
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
-
43
- /**
44
- * Returns the passed item if it is defined. Otherwise, queried its own block
45
- * @param params.blockKey Key of the queried field
46
- * @param params.item Item of the block. If null or only contains the block's id, the function will make a query
47
- * @returns The block data
48
- */
49
- export default async function getBlockProps<
50
- BlockFragment extends TCommonBlockFragment,
51
- BlockVariables extends Variables = Variables,
52
- >(params: TGetBlockPropsParams<BlockFragment, BlockVariables>): Promise<BlockFragment | null | undefined> {
53
- const { document, item, blockKey, variables } = params
54
-
55
- if (item) {
56
- // If the item actually contains the block's data, just return it
57
- if (!isOnlyIdInItem(item)) {
58
- return item
59
- }
60
-
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
73
-
74
- const queriedBlockProps = await queryGql(document, variables)
75
-
76
- if (!queriedBlockProps || typeof queriedBlockProps !== 'object' || !blockKey) return null
77
-
78
- const queriedBlockFragment = queriedBlockProps[blockKey]
79
-
80
- return queriedBlockFragment as BlockFragment
81
- }
@@ -1,5 +0,0 @@
1
- import { useFragment } from '../generated/fragment-masking'
2
-
3
- export { default as getBlockProps } from './get-block-props'
4
- export { default as mergeConfigs } from './merge-configs'
5
- export { useFragment as getFragment }
@@ -1,21 +0,0 @@
1
- import type { Nullable } from '@okam/stack-ui'
2
- import type { TBlockSerializerConfig } from '../components/BlockSerializer/interface'
3
-
4
- /**
5
- * Merges multiple block dispatcher configs
6
- * @param baseConfig The base configuration. Other configurations will be prioritized over this one, which acts as a fallback
7
- * @param configs Array of block dispatcher serializers to merge. Later elements will always be prioritized over first elements
8
- * @returns Merged config
9
- */
10
- export default function mergeConfigs(
11
- baseConfig: TBlockSerializerConfig,
12
- ...configs: Nullable<TBlockSerializerConfig>[]
13
- ): TBlockSerializerConfig {
14
- const finalConfig = configs.reduce<TBlockSerializerConfig>((mergedConfig, config) => {
15
- if (!config) return mergedConfig
16
-
17
- return { components: { ...mergedConfig.components, ...config.components } }
18
- }, baseConfig)
19
-
20
- return finalConfig
21
- }
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "jsx": "react-jsx",
4
- "allowJs": false,
5
- "esModuleInterop": false,
6
- "allowSyntheticDefaultImports": true,
7
- "strict": true
8
- },
9
- "files": [],
10
- "include": [],
11
- "references": [
12
- {
13
- "path": "./tsconfig.lib.json"
14
- }
15
- ],
16
- "extends": "../../../tsconfig.base.json"
17
- }
package/tsconfig.lib.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "../../../dist/out-tsc",
5
- "types": ["node", "next"]
6
- },
7
- "paths": { "@workspace/*": ["dist/libs/*"] },
8
- "files": [
9
- "../../../node_modules/@nx/react/typings/cssmodule.d.ts",
10
- "../../../node_modules/@nx/react/typings/image.d.ts"
11
- ],
12
- "exclude": [
13
- "**/*.spec.ts",
14
- "**/*.test.ts",
15
- "**/*.spec.tsx",
16
- "**/*.test.tsx",
17
- "**/*.spec.js",
18
- "**/*.test.js",
19
- "**/*.spec.jsx",
20
- "**/*.test.jsx",
21
- ],
22
- "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
23
- }
package/vite.config.ts DELETED
@@ -1,51 +0,0 @@
1
- /* eslint-disable import/no-relative-packages */
2
- /// <reference types="vitest" />
3
- import * as path from 'path'
4
- import react from '@vitejs/plugin-react'
5
- import { defineConfig } from 'vite'
6
- import dts from 'vite-plugin-dts'
7
- import viteTsConfigPaths from 'vite-tsconfig-paths'
8
- import externalDeps from '../../../config/external-deps'
9
-
10
- export default defineConfig({
11
- cacheDir: '../../../node_modules/.vite/directus-block',
12
-
13
- plugins: [
14
- dts({
15
- entryRoot: 'src',
16
- tsConfigFilePath: path.join(__dirname, 'tsconfig.lib.json'),
17
- skipDiagnostics: true,
18
- }),
19
- react(),
20
- viteTsConfigPaths({
21
- root: '../../../',
22
- }),
23
- ],
24
-
25
- // Uncomment this if you are using workers.
26
- // worker: {
27
- // plugins: [
28
- // viteTsConfigPaths({
29
- // root: '../../../',
30
- // }),
31
- // ],
32
- // },
33
-
34
- // Configuration for building your library.
35
- // See: https://vitejs.dev/guide/build.html#library-mode
36
- build: {
37
- lib: {
38
- // Could also be a dictionary or array of multiple entry points.
39
- entry: 'src/index.ts',
40
- name: 'directus-block',
41
- fileName: 'index',
42
- // Change this to the formats you want to support.
43
- // Don't forget to update your package.json as well.
44
- formats: ['es', 'cjs'],
45
- },
46
- rollupOptions: {
47
- // External packages that should not be bundled into your library.
48
- external: externalDeps,
49
- },
50
- },
51
- })