@datocms/astro 0.3.5 → 0.4.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.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@datocms/astro",
3
3
  "description": "A set of components and utilities to work faster with DatoCMS in Astro projects.",
4
4
  "type": "module",
5
- "version": "0.3.5",
5
+ "version": "0.4.0",
6
6
  "sideEffects": false,
7
7
  "repository": {
8
8
  "type": "git",
@@ -89,7 +89,7 @@ function findRecordInLinks(node: ItemLink | InlineItem) {
89
89
  return record;
90
90
  }
91
91
 
92
- const defaultComponents: Record<NodeType, AstroComponent> = {
92
+ const defaultComponents: NodeOverrides = {
93
93
  paragraph: Paragraph,
94
94
  root: Root,
95
95
  span: Span,
@@ -106,11 +106,11 @@ const defaultComponents: Record<NodeType, AstroComponent> = {
106
106
  };
107
107
 
108
108
  const otherNodeComponents: Record<NodeType, AstroComponent> = {
109
- ...defaultComponents,
109
+ ...(defaultComponents as Record<NodeType, AstroComponent>),
110
110
  ...nodeOverrides,
111
111
  };
112
112
 
113
- const Component = otherNodeComponents[node.type];
113
+ const Component = otherNodeComponents[node.type] as AstroComponent;
114
114
  ---
115
115
 
116
116
  <>
@@ -18,11 +18,13 @@ import type {
18
18
  MarkOverrides,
19
19
  } from './types';
20
20
 
21
- // It would be better to type this as:
21
+ // It would be better to type this as a generic:
22
22
  //
23
23
  // interface Props<R1 extends DatocmsRecord = any, R2 extends DatocmsRecord = any>
24
24
  //
25
25
  // but it's currently not possible: https://github.com/withastro/roadmap/discussions/601#discussioncomment-10333959
26
+ //
27
+ // In the meantime, you can use ensureValidStructuredTextProps()
26
28
 
27
29
  interface Props {
28
30
  /** The actual [field value](https://www.datocms.com/docs/structured-text/dast) you get from a DatoCMS Structured Text field */
@@ -0,0 +1,68 @@
1
+ import {
2
+ type Node as DastNode,
3
+ type Record as DatocmsRecord,
4
+ type Document,
5
+ type StructuredText,
6
+ } from 'datocms-structured-text-utils';
7
+ import type { BlockComponents, InlineRecordComponents, LinkToRecordComponents } from './types';
8
+
9
+ type Props<R1 extends DatocmsRecord, R2 extends DatocmsRecord> =
10
+ | {
11
+ /** The actual [field value](https://www.datocms.com/docs/structured-text/dast) you get from a DatoCMS Structured Text field */
12
+ data: Document | DastNode | null | undefined;
13
+ blockComponents?: never;
14
+ linkToRecordComponents?: never;
15
+ inlineRecordComponents?: never;
16
+ }
17
+ | {
18
+ /** The actual [field value](https://www.datocms.com/docs/structured-text/dast) you get from a DatoCMS Structured Text field */
19
+ data:
20
+ | (Omit<StructuredText<R1, R2>, 'blocks' | 'links'> & { blocks: R1[]; links: R2[] })
21
+ | null
22
+ | undefined;
23
+ /** An object in which the keys are the `__typename` of the blocks to be rendered, and the values are the Astro components */
24
+ blockComponents: BlockComponents<R1, R2>;
25
+ /** An object in which the keys are the `__typename` of the records to be rendered, and the values are the Astro components */
26
+ linkToRecordComponents: LinkToRecordComponents<R1, R2>;
27
+ /** An object in which the keys are the `__typename` of the records to be rendered, and the values are the Astro components */
28
+ inlineRecordComponents: InlineRecordComponents<R1, R2>;
29
+ }
30
+ | {
31
+ /** The actual [field value](https://www.datocms.com/docs/structured-text/dast) you get from a DatoCMS Structured Text field */
32
+ data:
33
+ | (Omit<StructuredText<R1, R2>, 'blocks' | 'links'> & { blocks: R1[]; links?: never })
34
+ | null
35
+ | undefined;
36
+ /** An object in which the keys are the `__typename` of the blocks to be rendered, and the values are the Astro components */
37
+ blockComponents: BlockComponents<R1, R2>;
38
+ linkToRecordComponents?: never;
39
+ inlineRecordComponents?: never;
40
+ }
41
+ | {
42
+ /** The actual [field value](https://www.datocms.com/docs/structured-text/dast) you get from a DatoCMS Structured Text field */
43
+ data:
44
+ | (Omit<StructuredText<R1, R2>, 'blocks' | 'links'> & { blocks?: never; links: R2[] })
45
+ | null
46
+ | undefined;
47
+ blockComponents?: never;
48
+ /** An object in which the keys are the `__typename` of the records to be rendered, and the values are the Astro components */
49
+ linkToRecordComponents: LinkToRecordComponents<R1, R2>;
50
+ /** An object in which the keys are the `__typename` of the records to be rendered, and the values are the Astro components */
51
+ inlineRecordComponents: InlineRecordComponents<R1, R2>;
52
+ }
53
+ | {
54
+ /** The actual [field value](https://www.datocms.com/docs/structured-text/dast) you get from a DatoCMS Structured Text field */
55
+ data:
56
+ | (Omit<StructuredText<R1, R2>, 'blocks' | 'links'> & { blocks?: never; links?: never })
57
+ | null
58
+ | undefined;
59
+ blockComponents?: never;
60
+ linkToRecordComponents?: never;
61
+ inlineRecordComponents?: never;
62
+ };
63
+
64
+ export function ensureValidStructuredTextProps<R1 extends DatocmsRecord, R2 extends DatocmsRecord>(
65
+ props: Props<R1, R2>,
66
+ ): Props<R1, R2> {
67
+ return props;
68
+ }
@@ -5,4 +5,6 @@ export {
5
5
  type TransformedMeta,
6
6
  } from 'datocms-structured-text-generic-html-renderer';
7
7
 
8
+ export { ensureValidStructuredTextProps } from './ensureValidStructuredTextProps';
9
+
8
10
  export { StructuredText };
@@ -1,23 +1,60 @@
1
- import type { Record as DatocmsRecord, Mark, NodeType } from 'datocms-structured-text-utils';
1
+ import type { TransformedMeta } from 'datocms-structured-text-generic-html-renderer';
2
+ import type {
3
+ Block,
4
+ Record as DatocmsRecord,
5
+ InlineItem,
6
+ ItemLink,
7
+ Mark,
8
+ Node,
9
+ Span,
10
+ } from 'datocms-structured-text-utils';
2
11
 
3
12
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
4
- export type AstroComponent = (props: any) => any;
13
+ export type AstroComponent<P = any> = (props: P) => any;
5
14
 
6
- export type BlockComponents<R1 extends DatocmsRecord, R2 extends DatocmsRecord> = Record<
7
- R1['__typename'],
8
- AstroComponent
9
- >;
15
+ export type BlockComponents<R1 extends DatocmsRecord, _R2 extends DatocmsRecord> = {
16
+ [R in R1 as R['__typename']]: AstroComponent<{ block: R }>;
17
+ };
10
18
 
11
- export type LinkToRecordComponents<R1 extends DatocmsRecord, R2 extends DatocmsRecord> = Record<
12
- R2['__typename'],
13
- AstroComponent
14
- >;
19
+ export type LinkToRecordComponents<_R1 extends DatocmsRecord, R2 extends DatocmsRecord> = {
20
+ [R in R2 as R['__typename']]: AstroComponent<{
21
+ node: ItemLink;
22
+ attrs: TransformedMeta;
23
+ record: R;
24
+ }>;
25
+ };
15
26
 
16
- export type InlineRecordComponents<R1 extends DatocmsRecord, R2 extends DatocmsRecord> = Record<
17
- R2['__typename'],
18
- AstroComponent
19
- >;
27
+ export type InlineRecordComponents<_R1 extends DatocmsRecord, R2 extends DatocmsRecord> = {
28
+ [R in R2 as R['__typename']]: AstroComponent<{ record: R }>;
29
+ };
20
30
 
21
- export type NodeOverrides = Partial<Record<NodeType, AstroComponent>>;
31
+ export type NodeOverrides = Partial<{
32
+ [N in Node as N['type']]: AstroComponent<
33
+ N extends ItemLink
34
+ ? {
35
+ node: ItemLink;
36
+ record: DatocmsRecord;
37
+ linkToRecordComponents?: LinkToRecordComponents<DatocmsRecord, DatocmsRecord>;
38
+ }
39
+ : N extends InlineItem
40
+ ? {
41
+ node: InlineItem;
42
+ record: DatocmsRecord;
43
+ inlineRecordComponents?: InlineRecordComponents<DatocmsRecord, DatocmsRecord>;
44
+ }
45
+ : N extends Block
46
+ ? {
47
+ node: Block;
48
+ block: DatocmsRecord;
49
+ blockComponents?: BlockComponents<DatocmsRecord, DatocmsRecord>;
50
+ }
51
+ : N extends Span
52
+ ? {
53
+ node: N;
54
+ markOverrides?: MarkOverrides;
55
+ }
56
+ : { node: N }
57
+ >;
58
+ }>;
22
59
 
23
- export type MarkOverrides = Partial<Record<Mark, AstroComponent>>;
60
+ export type MarkOverrides = Partial<Record<Mark, AstroComponent | string>>;