Skip to content

Commit 314f0e3

Browse files
committed
refactor: address comments
1 parent 33086d2 commit 314f0e3

File tree

4 files changed

+442
-10
lines changed

4 files changed

+442
-10
lines changed

src/code-gen-process.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,62 @@ 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 => component.schemaType === "jsonld-context" ||
450+
component.schemaType === "jsonld-entity" ||
451+
component.schemaType === "jsonld-type"
452+
);
453+
454+
if (hasJsonLdSchemas) {
455+
const { jsonLdOptions } = configuration.config;
456+
457+
// Generate JSON-LD context interfaces if enabled
458+
if (jsonLdOptions?.generateContext && templatesToRender.jsonldContextDataContract) {
459+
jsonldOutputFiles.push(
460+
...(await this.createOutputFileInfo(
461+
configuration,
462+
fileNames.jsonldContext,
463+
this.templatesWorker.renderTemplate(
464+
templatesToRender.jsonldContextDataContract,
465+
configuration,
466+
),
467+
))
468+
);
469+
}
470+
471+
// Generate JSON-LD entity interfaces
472+
if (templatesToRender.jsonldEntityDataContract) {
473+
jsonldOutputFiles.push(
474+
...(await this.createOutputFileInfo(
475+
configuration,
476+
fileNames.jsonldEntity,
477+
this.templatesWorker.renderTemplate(
478+
templatesToRender.jsonldEntityDataContract,
479+
configuration,
480+
),
481+
))
482+
);
483+
}
484+
485+
// Generate JSON-LD utility types if enabled
486+
if (jsonLdOptions?.generateUtils && templatesToRender.jsonldUtils) {
487+
jsonldOutputFiles.push(
488+
...(await this.createOutputFileInfo(
489+
configuration,
490+
fileNames.jsonldUtils,
491+
this.templatesWorker.renderTemplate(
492+
templatesToRender.jsonldUtils,
493+
configuration,
494+
),
495+
))
496+
);
497+
}
498+
}
499+
444500
return [
445501
...(await this.createOutputFileInfo(
446502
configuration,
@@ -460,6 +516,7 @@ export class CodeGenProcess {
460516
),
461517
)
462518
: []),
519+
...jsonldOutputFiles,
463520
...modularApiFileInfos,
464521
];
465522
};
@@ -468,7 +525,14 @@ export class CodeGenProcess {
468525
templatesToRender,
469526
configuration,
470527
): Promise<TranslatorIO[]> => {
471-
const { generateRouteTypes, generateClient } = configuration.config;
528+
const { generateRouteTypes, generateClient, jsonLdOptions } = configuration.config;
529+
530+
// Check if we have JSON-LD schemas
531+
const hasJsonLdSchemas = configuration.components?.some?.(
532+
component => component.schemaType === "jsonld-context" ||
533+
component.schemaType === "jsonld-entity" ||
534+
component.schemaType === "jsonld-type"
535+
);
472536

473537
return await this.createOutputFileInfo(
474538
configuration,
@@ -479,6 +543,27 @@ export class CodeGenProcess {
479543
templatesToRender.dataContracts,
480544
configuration,
481545
),
546+
// Include JSON-LD templates in single file output if present
547+
hasJsonLdSchemas &&
548+
jsonLdOptions?.generateContext &&
549+
templatesToRender.jsonldContextDataContract &&
550+
this.templatesWorker.renderTemplate(
551+
templatesToRender.jsonldContextDataContract,
552+
configuration,
553+
),
554+
hasJsonLdSchemas &&
555+
templatesToRender.jsonldEntityDataContract &&
556+
this.templatesWorker.renderTemplate(
557+
templatesToRender.jsonldEntityDataContract,
558+
configuration,
559+
),
560+
hasJsonLdSchemas &&
561+
jsonLdOptions?.generateUtils &&
562+
templatesToRender.jsonldUtils &&
563+
this.templatesWorker.renderTemplate(
564+
templatesToRender.jsonldUtils,
565+
configuration,
566+
),
482567
generateRouteTypes &&
483568
this.templatesWorker.renderTemplate(
484569
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)