@okam/directus-block 1.2.1 → 1.2.2

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 (44) 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/interface.d.ts +16 -0
  6. package/components/BlockSerializer/interface.d.ts +31 -0
  7. package/generated/fragment-masking.d.ts +15 -0
  8. package/generated/graphql.d.ts +64 -0
  9. package/index.d.ts +6 -0
  10. package/index.js +96 -0
  11. package/index.mjs +5966 -0
  12. package/package.json +1 -1
  13. package/style.css +1 -0
  14. package/types/block.d.ts +27 -0
  15. package/utils/get-block-props.d.ts +17 -0
  16. package/utils/index.d.ts +4 -0
  17. package/utils/merge-configs.d.ts +9 -0
  18. package/.eslintrc.js +0 -21
  19. package/.storybook/.eslintrc.js +0 -29
  20. package/.storybook/main.ts +0 -33
  21. package/.storybook/preview.ts +0 -0
  22. package/.storybook/tsconfig.json +0 -17
  23. package/CHANGELOG.md +0 -25
  24. package/README.md +0 -167
  25. package/project.json +0 -36
  26. package/src/blocks/BlockWysiwyg/config.tsx +0 -10
  27. package/src/blocks/BlockWysiwyg/index.tsx +0 -38
  28. package/src/blocks/BlockWysiwyg/interface.ts +0 -13
  29. package/src/components/BlockDispatcher/config.ts +0 -10
  30. package/src/components/BlockDispatcher/index.tsx +0 -30
  31. package/src/components/BlockDispatcher/interface.ts +0 -17
  32. package/src/components/BlockSerializer/index.tsx +0 -43
  33. package/src/components/BlockSerializer/interface.ts +0 -51
  34. package/src/generated/fragment-masking.ts +0 -66
  35. package/src/generated/graphql.ts +0 -31
  36. package/src/index.ts +0 -22
  37. package/src/server.ts +0 -6
  38. package/src/types/block.ts +0 -40
  39. package/src/utils/get-block-props.ts +0 -81
  40. package/src/utils/index.ts +0 -5
  41. package/src/utils/merge-configs.ts +0 -21
  42. package/tsconfig.json +0 -17
  43. package/tsconfig.lib.json +0 -23
  44. package/vite.config.ts +0 -51
@@ -1,66 +0,0 @@
1
- /* eslint-disable */
2
- import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
3
- import { FragmentDefinitionNode } from 'graphql'
4
- import { Incremental } from './graphql';
5
-
6
- export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
7
- infer TType,
8
- any
9
- >
10
- ? [TType] extends [{ ' $fragmentName'?: infer TKey }]
11
- ? TKey extends string
12
- ? { ' $fragmentRefs'?: { [key in TKey]: TType } }
13
- : never
14
- : never
15
- : never;
16
-
17
- // return non-nullable if `fragmentType` is non-nullable
18
- export function useFragment<TType>(
19
- _documentNode: DocumentTypeDecoration<TType, any>,
20
- fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
21
- ): TType;
22
- // return nullable if `fragmentType` is nullable
23
- export function useFragment<TType>(
24
- _documentNode: DocumentTypeDecoration<TType, any>,
25
- fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
26
- ): TType | null | undefined;
27
- // return array of non-nullable if `fragmentType` is array of non-nullable
28
- export function useFragment<TType>(
29
- _documentNode: DocumentTypeDecoration<TType, any>,
30
- fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
31
- ): ReadonlyArray<TType>;
32
- // return array of nullable if `fragmentType` is array of nullable
33
- export function useFragment<TType>(
34
- _documentNode: DocumentTypeDecoration<TType, any>,
35
- fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
36
- ): ReadonlyArray<TType> | null | undefined;
37
- export function useFragment<TType>(
38
- _documentNode: DocumentTypeDecoration<TType, any>,
39
- fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
40
- ): TType | ReadonlyArray<TType> | null | undefined {
41
- return fragmentType as any;
42
- }
43
-
44
- export function makeFragmentData<
45
- F extends DocumentTypeDecoration<any, any>,
46
- FT extends ResultOf<F>
47
- >(data: FT, _fragment: F): FragmentType<F> {
48
- return data as FragmentType<F>;
49
- }
50
- export function isFragmentReady<TQuery, TFrag>(
51
- queryNode: DocumentTypeDecoration<TQuery, any>,
52
- fragmentNode: TypedDocumentNode<TFrag>,
53
- data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
54
- ): data is FragmentType<typeof fragmentNode> {
55
- const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
56
- ?.deferredFields;
57
-
58
- if (!deferredFields) return true;
59
-
60
- const fragDef = (fragmentNode as any).definitions[0] as FragmentDefinitionNode | undefined;
61
- const fragName = fragDef?.name?.value;
62
-
63
- const fields = (fragName && deferredFields[fragName]) || [];
64
- //@ts-ignore
65
- return fields.length > 0 && fields.every((field) => data && field in data);
66
- }
@@ -1,31 +0,0 @@
1
- /* eslint-disable prettier/prettier */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
- /* eslint-disable @typescript-eslint/naming-convention */
4
- import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'
5
-
6
- export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
7
- export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never }
8
-
9
- export type Scalars = {
10
- ID: { input: string; output: string; }
11
- String: { input: string; output: string; }
12
- Boolean: { input: boolean; output: boolean; }
13
- Int: { input: number; output: number; }
14
- Float: { input: number; output: number; }
15
- /** ISO8601 Date values */
16
- Date: { input: any; output: any; }
17
- /** BigInt value */
18
- GraphQLBigInt: { input: any; output: any; }
19
- /** A Float or a String */
20
- GraphQLStringOrFloat: { input: any; output: any; }
21
- /** Hashed string values */
22
- Hash: { input: any; output: any; }
23
- /** The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). */
24
- JSON: { input: any; output: any; }
25
- };
26
-
27
- export type BlockSettingsFragment = { __typename?: 'block_settings'; tokens?: any | null; variant?: string | null } & {
28
- ' $fragmentName'?: 'BlockSettingsFragment'
29
- }
30
-
31
- export 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"}}]}}]} as unknown as DocumentNode<BlockSettingsFragment, unknown>;
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'
package/src/server.ts DELETED
@@ -1,6 +0,0 @@
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,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
- })