Skip to content

Commit 9031997

Browse files
committed
Fix types
1 parent 0a5daee commit 9031997

File tree

7 files changed

+88
-38
lines changed

7 files changed

+88
-38
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
strategy:
1717
matrix:
18-
node-version: [10.x, 12.x, 14.x, 15.x]
18+
node-version: [16.x, 18.x, 20.x]
1919
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2020

2121
steps:

packages/to-dom-nodes/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
RenderResult,
2121
RenderRule,
2222
StructuredText as StructuredTextGraphQlResponse,
23+
TypesafeStructuredText as TypesafeStructuredTextGraphQlResponse,
2324
} from 'datocms-structured-text-utils';
2425
import hyperscript from 'hyperscript';
2526

@@ -30,6 +31,7 @@ export { renderNodeRule as renderRule };
3031

3132
export type {
3233
StructuredTextDocument,
34+
TypesafeStructuredTextGraphQlResponse,
3335
StructuredTextGraphQlResponse,
3436
StructuredTextGraphQlResponseRecord,
3537
};

packages/to-html-string/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
RenderResult,
2121
RenderRule,
2222
StructuredText as StructuredTextGraphQlResponse,
23+
TypesafeStructuredText as TypesafeStructuredTextGraphQlResponse,
2324
} from 'datocms-structured-text-utils';
2425
import vhtml from 'vhtml';
2526

@@ -30,6 +31,7 @@ export { renderNodeRule as renderRule };
3031

3132
export type {
3233
StructuredTextDocument,
34+
TypesafeStructuredTextGraphQlResponse,
3335
StructuredTextGraphQlResponse,
3436
StructuredTextGraphQlResponseRecord,
3537
};

packages/to-plain-text/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import {
2020
RenderResult,
2121
RenderRule,
2222
StructuredText as StructuredTextGraphQlResponse,
23+
TypesafeStructuredText as TypesafeStructuredTextGraphQlResponse,
2324
} from 'datocms-structured-text-utils';
2425

2526
export { renderNodeRule, renderMarkRule, RenderError };
2627
// deprecated export
2728
export { renderNodeRule as renderRule };
2829
export type {
2930
StructuredTextDocument,
31+
TypesafeStructuredTextGraphQlResponse,
3032
StructuredTextGraphQlResponse,
3133
StructuredTextGraphQlResponseRecord,
3234
};

packages/utils/src/guards.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import {
1515
WithChildrenNode,
1616
InlineNode,
1717
NodeType,
18-
Record,
18+
Record as DatoCmsRecord,
1919
StructuredText,
2020
ThematicBreak,
2121
Document,
2222
} from './types';
2323

2424
import {
25+
allowedNodeTypes,
2526
headingNodeType,
2627
spanNodeType,
2728
rootNodeType,
@@ -98,33 +99,50 @@ export function isThematicBreak(node: Node): node is ThematicBreak {
9899
return node.type === thematicBreakNodeType;
99100
}
100101

101-
export function isStructuredText<R1 extends Record, R2 extends Record = R1>(
102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
103-
obj: any,
104-
): obj is StructuredText<R1, R2> {
105-
return obj && 'value' in obj && isDocument(obj.value);
102+
function isObject(obj: unknown): obj is Record<string, unknown> {
103+
return Boolean(typeof obj === 'object' && obj);
106104
}
107105

108-
export function isDocument(
109-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
110-
obj: any,
111-
): obj is Document {
112-
return obj && 'schema' in obj && 'document' in obj;
106+
export function isNodeType(value: string): value is NodeType {
107+
return allowedNodeTypes.includes(value as NodeType);
113108
}
114109

115-
export function isEmptyDocument(
116-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
117-
obj: any,
118-
): boolean {
110+
export function isNode(obj: unknown): obj is Node {
111+
return Boolean(
112+
isObject(obj) &&
113+
'type' in obj &&
114+
typeof obj.type === 'string' &&
115+
isNodeType(obj.type),
116+
);
117+
}
118+
119+
export function isStructuredText<
120+
R1 extends DatoCmsRecord,
121+
R2 extends DatoCmsRecord = R1
122+
>(obj: unknown): obj is StructuredText<R1, R2> {
123+
return Boolean(isObject(obj) && 'value' in obj && isDocument(obj.value));
124+
}
125+
126+
export function isDocument(obj: unknown): obj is Document {
127+
return Boolean(
128+
isObject(obj) &&
129+
'schema' in obj &&
130+
'document' in obj &&
131+
obj.schema === 'dast',
132+
);
133+
}
134+
135+
export function isEmptyDocument(obj: unknown): boolean {
119136
if (!obj) {
120137
return true;
121138
}
122139

123-
const document = isStructuredText(obj)
124-
? obj.value
125-
: isDocument(obj)
126-
? obj
127-
: null;
140+
const document =
141+
isStructuredText(obj) && isDocument(obj.value)
142+
? obj.value
143+
: isDocument(obj)
144+
? obj
145+
: null;
128146

129147
if (!document) {
130148
throw new Error(
@@ -133,7 +151,6 @@ export function isEmptyDocument(
133151
}
134152

135153
return (
136-
document.schema === 'dast' &&
137154
document.document.children.length === 1 &&
138155
document.document.children[0].type === 'paragraph' &&
139156
document.document.children[0].children.length === 1 &&

packages/utils/src/render.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Node, Record, Document, StructuredText } from './types';
2-
import { hasChildren, isDocument, isStructuredText } from './guards';
2+
import { hasChildren, isDocument, isNode, isStructuredText } from './guards';
33
import { flatten } from 'array-flatten';
44

55
export class RenderError extends Error {
@@ -90,12 +90,11 @@ export function transformNode<
9090

9191
if (matchingTransform) {
9292
return matchingTransform.apply({ adapter, node, children, key, ancestors });
93-
} else {
94-
throw new RenderError(
95-
`Don't know how to render a node with type "${node.type}". Please specify a custom renderRule for it!`,
96-
node,
97-
);
9893
}
94+
throw new RenderError(
95+
`Don't know how to render a node with type "${node.type}". Please specify a custom renderRule for it!`,
96+
node,
97+
);
9998
}
10099

101100
export type Adapter<
@@ -128,18 +127,23 @@ export function render<
128127
return null;
129128
}
130129

131-
const result = transformNode(
132-
adapter,
133-
isStructuredText<R1, R2>(structuredTextOrNode)
130+
const node =
131+
isStructuredText<R1, R2>(structuredTextOrNode) &&
132+
isDocument(structuredTextOrNode.value)
134133
? structuredTextOrNode.value.document
135134
: isDocument(structuredTextOrNode)
136135
? structuredTextOrNode.document
137-
: structuredTextOrNode,
136+
: isNode(structuredTextOrNode)
137+
? structuredTextOrNode
138+
: undefined;
138139

139-
't-0',
140-
[],
141-
renderRules,
142-
);
140+
if (!node) {
141+
throw new Error(
142+
'Passed object is neither null, a Structured Text value, a DAST document or a DAST node',
143+
);
144+
}
145+
146+
const result = transformNode(adapter, node, 't-0', [], renderRules);
143147

144148
return result;
145149
}

packages/utils/src/types.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,31 @@ export type NodeType =
440440
* and embedded within the flow of the text.
441441
*/
442442

443-
export type StructuredText<R1 extends Record = Record, R2 extends Record = R1> = {
444-
/** A DatoCMS compatible document */
443+
export type StructuredText<
444+
R1 extends Record = Record,
445+
R2 extends Record = R1
446+
> = {
447+
/**
448+
* A DatoCMS "dast" document
449+
*
450+
* https://www.datocms.com/docs/structured-text/dast
451+
*/
452+
value: Document | unknown;
453+
/** Blocks associated with the Structured Text */
454+
blocks?: R1[];
455+
/** Links associated with the Structured Text */
456+
links?: R2[];
457+
};
458+
459+
export type TypesafeStructuredText<
460+
R1 extends Record = Record,
461+
R2 extends Record = R1
462+
> = {
463+
/**
464+
* A DatoCMS "dast" document
465+
*
466+
* https://www.datocms.com/docs/structured-text/dast
467+
*/
445468
value: Document;
446469
/** Blocks associated with the Structured Text */
447470
blocks?: R1[];

0 commit comments

Comments
 (0)