Skip to content

Commit 007eae2

Browse files
authored
Merge pull request #213 from Eyas/newlib
Factor out common types that don't depend on the exact schema into schema-dts-lib
2 parents 8ad5601 + 6df4f89 commit 007eae2

36 files changed

Lines changed: 487 additions & 198 deletions

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"type": "module",
7272
"workspaces": [
7373
"packages/schema-dts-gen",
74+
"packages/schema-dts-lib",
7475
"packages/schema-dts"
7576
]
7677
}

packages/schema-dts-gen/src/ts/helper_types.ts

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,8 @@ import {arrayOf} from './util/arrayof.js';
1313
import {withComments} from './util/comments.js';
1414
import {typeUnion} from './util/union.js';
1515

16-
function IdPropertyNode() {
17-
return withComments(
18-
'IRI identifying the canonical address of this object.',
19-
factory.createPropertySignature(
20-
/* modifiers= */ [],
21-
factory.createStringLiteral('@id'),
22-
/* questionToken= */ undefined,
23-
/* typeNode= */
24-
factory.createTypeReferenceNode('string', /*typeArguments=*/ []),
25-
),
26-
);
27-
}
28-
2916
function WithContextType(context: Context) {
30-
// export type WithContext<T extends Thing> = T & { "@context": TYPE_NODE }
17+
// export interface WithContext<T extends JsonLdObject | string> extends Exclude<T, string> { "@context": TYPE_NODE }
3118
return withComments(
3219
'Used at the top-level node to indicate the context for the JSON-LD ' +
3320
'objects used. The context provided in this type is compatible ' +
@@ -39,10 +26,13 @@ function WithContextType(context: Context) {
3926
factory.createTypeParameterDeclaration(
4027
/*modifiers=*/ [],
4128
'T' /*constraint=*/,
42-
factory.createTypeReferenceNode(
43-
'Thing',
44-
/*typeArguments=*/ undefined,
45-
),
29+
factory.createUnionTypeNode([
30+
factory.createTypeReferenceNode(
31+
'JsonLdObject',
32+
/*typeArguments=*/ undefined,
33+
),
34+
factory.createKeywordTypeNode(SyntaxKind.StringKeyword),
35+
]),
4636
),
4737
],
4838
factory.createIntersectionTypeNode([
@@ -105,6 +95,42 @@ export function SchemaValueReference(
10595

10696
export function HelperTypes(context: Context, {hasRole}: {hasRole: boolean}) {
10797
return [
98+
factory.createImportDeclaration(
99+
/*modifiers=*/ [],
100+
factory.createImportClause(
101+
SyntaxKind.TypeKeyword,
102+
/*name=*/ undefined,
103+
factory.createNamedImports([
104+
factory.createImportSpecifier(
105+
/*isTypeOnly=*/ false,
106+
/*propertyName=*/ undefined,
107+
/*name=*/ factory.createIdentifier('JsonLdObject'),
108+
),
109+
factory.createImportSpecifier(
110+
/*isTypeOnly=*/ false,
111+
/*propertyName=*/ undefined,
112+
/*name=*/ factory.createIdentifier('IdReference'),
113+
),
114+
]),
115+
),
116+
factory.createStringLiteral('schema-dts-lib'),
117+
),
118+
factory.createExportDeclaration(
119+
/*modifiers=*/ [],
120+
/*isTypeOnly=*/ true,
121+
factory.createNamedExports([
122+
factory.createExportSpecifier(
123+
/*isTypeOnly=*/ false,
124+
/*propertyName=*/ undefined,
125+
/*name=*/ factory.createIdentifier('JsonLdObject'),
126+
),
127+
factory.createExportSpecifier(
128+
/*isTypeOnly=*/ false,
129+
/*propertyName=*/ undefined,
130+
/*name=*/ factory.createIdentifier('IdReference'),
131+
),
132+
]),
133+
),
108134
WithContextType(context),
109135
GraphType(context),
110136
factory.createTypeAliasDeclaration(
@@ -143,12 +169,6 @@ export function HelperTypes(context: Context, {hasRole}: {hasRole: boolean}) {
143169
),
144170
),
145171
),
146-
factory.createTypeAliasDeclaration(
147-
/*modifiers=*/ [],
148-
IdReferenceName,
149-
/*typeParameters=*/ [],
150-
factory.createTypeLiteralNode([IdPropertyNode()]),
151-
),
152172
InputActionConstraintsType,
153173
OutputActionConstraintsType,
154174
WithActionConstraintsType,

packages/schema-dts-gen/test/baselines/category_test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,17 @@ test(`baseline_${basename(import.meta.url)}`, async () => {
4545
);
4646

4747
expect(actual).toMatchInlineSnapshot(`
48-
"/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
49-
export type WithContext<T extends Thing> = T & {
48+
"import type { JsonLdObject, IdReference } from "schema-dts-lib";
49+
export type { JsonLdObject, IdReference };
50+
/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
51+
export type WithContext<T extends JsonLdObject | string> = T & {
5052
"@context": "https://schema.org";
5153
};
5254
export interface Graph {
5355
"@context": "https://schema.org";
5456
"@graph": readonly Thing[];
5557
}
5658
type SchemaValue<T> = T | readonly T[];
57-
type IdReference = {
58-
/** IRI identifying the canonical address of this object. */
59-
"@id": string;
60-
};
6159
type InputActionConstraints<T extends ActionBase> = Partial<{
6260
[K in Exclude<keyof T, \`@\${string}\`> as \`\${string & K}-input\`]: PropertyValueSpecification | string;
6361
}>;

packages/schema-dts-gen/test/baselines/comments_test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,17 @@ test(`baseline_${basename(import.meta.url)}`, async () => {
6565
);
6666

6767
expect(actual).toMatchInlineSnapshot(`
68-
"/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
69-
export type WithContext<T extends Thing> = T & {
68+
"import type { JsonLdObject, IdReference } from "schema-dts-lib";
69+
export type { JsonLdObject, IdReference };
70+
/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
71+
export type WithContext<T extends JsonLdObject | string> = T & {
7072
"@context": "https://schema.org";
7173
};
7274
export interface Graph {
7375
"@context": "https://schema.org";
7476
"@graph": readonly Thing[];
7577
}
7678
type SchemaValue<T> = T | readonly T[];
77-
type IdReference = {
78-
/** IRI identifying the canonical address of this object. */
79-
"@id": string;
80-
};
8179
type InputActionConstraints<T extends ActionBase> = Partial<{
8280
[K in Exclude<keyof T, \`@\${string}\`> as \`\${string & K}-input\`]: PropertyValueSpecification | string;
8381
}>;

packages/schema-dts-gen/test/baselines/data_type_union_test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,17 @@ test(`baseline_${basename(import.meta.url)}`, async () => {
4343
);
4444

4545
expect(actual).toMatchInlineSnapshot(`
46-
"/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
47-
export type WithContext<T extends Thing> = T & {
46+
"import type { JsonLdObject, IdReference } from "schema-dts-lib";
47+
export type { JsonLdObject, IdReference };
48+
/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
49+
export type WithContext<T extends JsonLdObject | string> = T & {
4850
"@context": "https://schema.org";
4951
};
5052
export interface Graph {
5153
"@context": "https://schema.org";
5254
"@graph": readonly Thing[];
5355
}
5456
type SchemaValue<T> = T | readonly T[];
55-
type IdReference = {
56-
/** IRI identifying the canonical address of this object. */
57-
"@id": string;
58-
};
5957
type InputActionConstraints<T extends ActionBase> = Partial<{
6058
[K in Exclude<keyof T, \`@\${string}\`> as \`\${string & K}-input\`]: PropertyValueSpecification | string;
6159
}>;

packages/schema-dts-gen/test/baselines/default_ontology_test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ test(`baseline_${basename(import.meta.url)}`, async () => {
3030
);
3131

3232
expect(actual).toMatchInlineSnapshot(`
33-
"/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
34-
export type WithContext<T extends Thing> = T & {
33+
"import type { JsonLdObject, IdReference } from "schema-dts-lib";
34+
export type { JsonLdObject, IdReference };
35+
/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
36+
export type WithContext<T extends JsonLdObject | string> = T & {
3537
"@context": "https://schema.org";
3638
};
3739
export interface Graph {
3840
"@context": "https://schema.org";
3941
"@graph": readonly Thing[];
4042
}
4143
type SchemaValue<T> = T | readonly T[];
42-
type IdReference = {
43-
/** IRI identifying the canonical address of this object. */
44-
"@id": string;
45-
};
4644
type InputActionConstraints<T extends ActionBase> = Partial<{
4745
[K in Exclude<keyof T, \`@\${string}\`> as \`\${string & K}-input\`]: PropertyValueSpecification | string;
4846
}>;

packages/schema-dts-gen/test/baselines/deprecated_objects_test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,17 @@ test(`baseline_${basename(import.meta.url)}`, async () => {
6666
);
6767

6868
expect(actual).toMatchInlineSnapshot(`
69-
"/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
70-
export type WithContext<T extends Thing> = T & {
69+
"import type { JsonLdObject, IdReference } from "schema-dts-lib";
70+
export type { JsonLdObject, IdReference };
71+
/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
72+
export type WithContext<T extends JsonLdObject | string> = T & {
7173
"@context": "https://schema.org";
7274
};
7375
export interface Graph {
7476
"@context": "https://schema.org";
7577
"@graph": readonly Thing[];
7678
}
7779
type SchemaValue<T> = T | readonly T[];
78-
type IdReference = {
79-
/** IRI identifying the canonical address of this object. */
80-
"@id": string;
81-
};
8280
type InputActionConstraints<T extends ActionBase> = Partial<{
8381
[K in Exclude<keyof T, \`@\${string}\`> as \`\${string & K}-input\`]: PropertyValueSpecification | string;
8482
}>;

packages/schema-dts-gen/test/baselines/duplicate_comments_test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,17 @@ test(`baseline_${basename(import.meta.url)}`, async () => {
4747
);
4848

4949
expect(actual).toMatchInlineSnapshot(`
50-
"/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
51-
export type WithContext<T extends Thing> = T & {
50+
"import type { JsonLdObject, IdReference } from "schema-dts-lib";
51+
export type { JsonLdObject, IdReference };
52+
/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
53+
export type WithContext<T extends JsonLdObject | string> = T & {
5254
"@context": "https://schema.org";
5355
};
5456
export interface Graph {
5557
"@context": "https://schema.org";
5658
"@graph": readonly Thing[];
5759
}
5860
type SchemaValue<T> = T | readonly T[];
59-
type IdReference = {
60-
/** IRI identifying the canonical address of this object. */
61-
"@id": string;
62-
};
6361
type InputActionConstraints<T extends ActionBase> = Partial<{
6462
[K in Exclude<keyof T, \`@\${string}\`> as \`\${string & K}-input\`]: PropertyValueSpecification | string;
6563
}>;

packages/schema-dts-gen/test/baselines/enum_skipped_test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,17 @@ test(`baseline_${basename(import.meta.url)}`, async () => {
4343
);
4444

4545
expect(actual).toMatchInlineSnapshot(`
46-
"/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
47-
export type WithContext<T extends Thing> = T & {
46+
"import type { JsonLdObject, IdReference } from "schema-dts-lib";
47+
export type { JsonLdObject, IdReference };
48+
/** Used at the top-level node to indicate the context for the JSON-LD objects used. The context provided in this type is compatible with the keys and URLs in the rest of this generated file. */
49+
export type WithContext<T extends JsonLdObject | string> = T & {
4850
"@context": "https://schema.org";
4951
};
5052
export interface Graph {
5153
"@context": "https://schema.org";
5254
"@graph": readonly Thing[];
5355
}
5456
type SchemaValue<T> = T | readonly T[];
55-
type IdReference = {
56-
/** IRI identifying the canonical address of this object. */
57-
"@id": string;
58-
};
5957
type InputActionConstraints<T extends ActionBase> = Partial<{
6058
[K in Exclude<keyof T, \`@\${string}\`> as \`\${string & K}-input\`]: PropertyValueSpecification | string;
6159
}>;

0 commit comments

Comments
 (0)