Skip to content

Commit 045fe31

Browse files
committed
refactor: address comments
1 parent 33086d2 commit 045fe31

File tree

4 files changed

+448
-10
lines changed

4 files changed

+448
-10
lines changed

src/code-gen-process.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,66 @@ export class CodeGenProcess {
441441
}
442442
}
443443

444+
// Add JSON-LD output files if enabled and schemas are present
445+
const jsonldOutputFiles: TranslatorIO[] = [];
446+
447+
// Check if we have JSON-LD schemas and options enabled
448+
const hasJsonLdSchemas = configuration.components?.some?.(
449+
(component) =>
450+
component.schemaType === "jsonld-context" ||
451+
component.schemaType === "jsonld-entity" ||
452+
component.schemaType === "jsonld-type",
453+
);
454+
455+
if (hasJsonLdSchemas) {
456+
const { jsonLdOptions } = configuration.config;
457+
458+
// Generate JSON-LD context interfaces if enabled
459+
if (
460+
jsonLdOptions?.generateContext &&
461+
templatesToRender.jsonldContextDataContract
462+
) {
463+
jsonldOutputFiles.push(
464+
...(await this.createOutputFileInfo(
465+
configuration,
466+
fileNames.jsonldContext,
467+
this.templatesWorker.renderTemplate(
468+
templatesToRender.jsonldContextDataContract,
469+
configuration,
470+
),
471+
)),
472+
);
473+
}
474+
475+
// Generate JSON-LD entity interfaces
476+
if (templatesToRender.jsonldEntityDataContract) {
477+
jsonldOutputFiles.push(
478+
...(await this.createOutputFileInfo(
479+
configuration,
480+
fileNames.jsonldEntity,
481+
this.templatesWorker.renderTemplate(
482+
templatesToRender.jsonldEntityDataContract,
483+
configuration,
484+
),
485+
)),
486+
);
487+
}
488+
489+
// Generate JSON-LD utility types if enabled
490+
if (jsonLdOptions?.generateUtils && templatesToRender.jsonldUtils) {
491+
jsonldOutputFiles.push(
492+
...(await this.createOutputFileInfo(
493+
configuration,
494+
fileNames.jsonldUtils,
495+
this.templatesWorker.renderTemplate(
496+
templatesToRender.jsonldUtils,
497+
configuration,
498+
),
499+
)),
500+
);
501+
}
502+
}
503+
444504
return [
445505
...(await this.createOutputFileInfo(
446506
configuration,
@@ -460,6 +520,7 @@ export class CodeGenProcess {
460520
),
461521
)
462522
: []),
523+
...jsonldOutputFiles,
463524
...modularApiFileInfos,
464525
];
465526
};
@@ -468,7 +529,16 @@ export class CodeGenProcess {
468529
templatesToRender,
469530
configuration,
470531
): Promise<TranslatorIO[]> => {
471-
const { generateRouteTypes, generateClient } = configuration.config;
532+
const { generateRouteTypes, generateClient, jsonLdOptions } =
533+
configuration.config;
534+
535+
// Check if we have JSON-LD schemas
536+
const hasJsonLdSchemas = configuration.components?.some?.(
537+
(component) =>
538+
component.schemaType === "jsonld-context" ||
539+
component.schemaType === "jsonld-entity" ||
540+
component.schemaType === "jsonld-type",
541+
);
472542

473543
return await this.createOutputFileInfo(
474544
configuration,
@@ -479,6 +549,27 @@ export class CodeGenProcess {
479549
templatesToRender.dataContracts,
480550
configuration,
481551
),
552+
// Include JSON-LD templates in single file output if present
553+
hasJsonLdSchemas &&
554+
jsonLdOptions?.generateContext &&
555+
templatesToRender.jsonldContextDataContract &&
556+
this.templatesWorker.renderTemplate(
557+
templatesToRender.jsonldContextDataContract,
558+
configuration,
559+
),
560+
hasJsonLdSchemas &&
561+
templatesToRender.jsonldEntityDataContract &&
562+
this.templatesWorker.renderTemplate(
563+
templatesToRender.jsonldEntityDataContract,
564+
configuration,
565+
),
566+
hasJsonLdSchemas &&
567+
jsonLdOptions?.generateUtils &&
568+
templatesToRender.jsonldUtils &&
569+
this.templatesWorker.renderTemplate(
570+
templatesToRender.jsonldUtils,
571+
configuration,
572+
),
482573
generateRouteTypes &&
483574
this.templatesWorker.renderTemplate(
484575
templatesToRender.routeTypes,

src/configuration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export class CodeGenConfig {
8484
routeTypes: "route-types",
8585
httpClient: "http-client",
8686
outOfModuleApi: "Common",
87+
jsonldContext: "jsonld-context",
88+
jsonldEntity: "jsonld-entity",
89+
jsonldUtils: "jsonld-utils",
8790
};
8891
routeNameDuplicatesMap = new Map();
8992
hooks: Hooks = {

src/schema-parser/schema-formatters.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ export class SchemaFormatters {
5050
$content: parsedSchema.content,
5151
};
5252
},
53+
// JSON-LD schema type formatters
54+
[SCHEMA_TYPES.JSONLD_CONTEXT]: (parsedSchema) => {
55+
// Format JSON-LD context as an object with proper content formatting
56+
return {
57+
...parsedSchema,
58+
$content: parsedSchema.content,
59+
content: this.formatObjectContent(parsedSchema.content),
60+
};
61+
},
62+
[SCHEMA_TYPES.JSONLD_ENTITY]: (parsedSchema) => {
63+
// Format JSON-LD entity as an object with proper content formatting
64+
return {
65+
...parsedSchema,
66+
$content: parsedSchema.content,
67+
content: this.formatObjectContent(parsedSchema.content),
68+
};
69+
},
70+
[SCHEMA_TYPES.JSONLD_TYPE]: (parsedSchema) => {
71+
// Format JSON-LD type as an object with proper content formatting
72+
return {
73+
...parsedSchema,
74+
$content: parsedSchema.content,
75+
content: this.formatObjectContent(parsedSchema.content),
76+
};
77+
},
5378
};
5479
inline = {
5580
[SCHEMA_TYPES.ENUM]: (parsedSchema) => {
@@ -89,6 +114,82 @@ export class SchemaFormatters {
89114
),
90115
};
91116
},
117+
// JSON-LD inline formatters - reuse OBJECT formatter logic
118+
[SCHEMA_TYPES.JSONLD_CONTEXT]: (parsedSchema) => {
119+
// Handle JSON-LD context inline formatting similar to object
120+
if (typeof parsedSchema.content === "string")
121+
return {
122+
...parsedSchema,
123+
typeIdentifier: this.config.Ts.Keyword.Type,
124+
content: this.schemaUtils.safeAddNullToType(parsedSchema.content),
125+
};
126+
127+
return {
128+
...parsedSchema,
129+
typeIdentifier: this.config.Ts.Keyword.Type,
130+
content: this.schemaUtils.safeAddNullToType(
131+
parsedSchema,
132+
parsedSchema.content?.length
133+
? this.config.Ts.ObjectWrapper(
134+
this.formatObjectContent(parsedSchema.content),
135+
)
136+
: this.config.Ts.RecordType(
137+
this.config.Ts.Keyword.String,
138+
this.config.Ts.Keyword.Any,
139+
),
140+
),
141+
};
142+
},
143+
[SCHEMA_TYPES.JSONLD_ENTITY]: (parsedSchema) => {
144+
// Handle JSON-LD entity inline formatting similar to object
145+
if (typeof parsedSchema.content === "string")
146+
return {
147+
...parsedSchema,
148+
typeIdentifier: this.config.Ts.Keyword.Type,
149+
content: this.schemaUtils.safeAddNullToType(parsedSchema.content),
150+
};
151+
152+
return {
153+
...parsedSchema,
154+
typeIdentifier: this.config.Ts.Keyword.Type,
155+
content: this.schemaUtils.safeAddNullToType(
156+
parsedSchema,
157+
parsedSchema.content?.length
158+
? this.config.Ts.ObjectWrapper(
159+
this.formatObjectContent(parsedSchema.content),
160+
)
161+
: this.config.Ts.RecordType(
162+
this.config.Ts.Keyword.String,
163+
this.config.Ts.Keyword.Any,
164+
),
165+
),
166+
};
167+
},
168+
[SCHEMA_TYPES.JSONLD_TYPE]: (parsedSchema) => {
169+
// Handle JSON-LD type inline formatting similar to object
170+
if (typeof parsedSchema.content === "string")
171+
return {
172+
...parsedSchema,
173+
typeIdentifier: this.config.Ts.Keyword.Type,
174+
content: this.schemaUtils.safeAddNullToType(parsedSchema.content),
175+
};
176+
177+
return {
178+
...parsedSchema,
179+
typeIdentifier: this.config.Ts.Keyword.Type,
180+
content: this.schemaUtils.safeAddNullToType(
181+
parsedSchema,
182+
parsedSchema.content?.length
183+
? this.config.Ts.ObjectWrapper(
184+
this.formatObjectContent(parsedSchema.content),
185+
)
186+
: this.config.Ts.RecordType(
187+
this.config.Ts.Keyword.String,
188+
this.config.Ts.Keyword.Any,
189+
),
190+
),
191+
};
192+
},
92193
};
93194

94195
formatSchema = (

0 commit comments

Comments
 (0)