Skip to content

Commit a31b0e2

Browse files
committed
feat: add jsonld schema support
1 parent 157368a commit a31b0e2

15 files changed

+1693
-0
lines changed

src/configuration.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ export class CodeGenConfig {
141141
httpClient: "",
142142
routeTypes: "",
143143
routeName: "",
144+
jsonldContextDataContract: "",
145+
jsonldEntityDataContract: "",
146+
jsonldUtils: "",
144147
};
145148
schemaParsers: Record<string, (...args: unknown[]) => MonoSchemaParser> = {};
146149
toJS = false;
@@ -180,6 +183,20 @@ export class CodeGenConfig {
180183

181184
successResponseStatusRange = [200, 299];
182185

186+
/** JSON-LD specific configuration options */
187+
jsonLdOptions = {
188+
/** Generate context interfaces */
189+
generateContext: true,
190+
/** Enforce strict JSON-LD typing */
191+
strictTyping: false,
192+
/** Prefix for entity interfaces */
193+
entityPrefix: "",
194+
/** Suffix for context interfaces */
195+
contextSuffix: "Context",
196+
/** Generate utility types for JSON-LD */
197+
generateUtils: true,
198+
};
199+
183200
extractingOptions: Partial<ExtractingOptions> = {
184201
requestBodySuffix: ["Payload", "Body", "Input"],
185202
requestParamsSuffix: ["Params"],
@@ -390,6 +407,18 @@ export class CodeGenConfig {
390407
"relative-json-pointer": () => this.Ts.Keyword.String,
391408
regex: () => this.Ts.Keyword.String,
392409
},
410+
// JSON-LD specific types
411+
"jsonld-iri": () => this.Ts.Keyword.String,
412+
"jsonld-literal": (schema) => this.getJsonLdLiteralType(schema),
413+
"jsonld-node": () => "JsonLdNode",
414+
"jsonld-context": () =>
415+
this.Ts.UnionType([
416+
this.Ts.Keyword.String,
417+
this.Ts.Keyword.Object,
418+
this.Ts.ArrayType(
419+
this.Ts.UnionType([this.Ts.Keyword.String, this.Ts.Keyword.Object]),
420+
),
421+
]),
393422
};
394423

395424
templateInfos = [
@@ -403,6 +432,15 @@ export class CodeGenConfig {
403432
{ name: "httpClient", fileName: "http-client" },
404433
{ name: "routeTypes", fileName: "route-types" },
405434
{ name: "routeName", fileName: "route-name" },
435+
{
436+
name: "jsonldContextDataContract",
437+
fileName: "jsonld-context-data-contract",
438+
},
439+
{
440+
name: "jsonldEntityDataContract",
441+
fileName: "jsonld-entity-data-contract",
442+
},
443+
{ name: "jsonldUtils", fileName: "jsonld-utils" },
406444
];
407445

408446
templateExtensions = [".eta", ".ejs"];
@@ -439,6 +477,51 @@ export class CodeGenConfig {
439477
this.componentTypeNameResolver = new ComponentTypeNameResolver(this, []);
440478
}
441479

480+
/** Helper method to determine JSON-LD literal type */
481+
getJsonLdLiteralType = (schema: any): string => {
482+
if (schema && typeof schema === "object") {
483+
// Check for @type in schema to determine literal type
484+
if (schema["@type"]) {
485+
const type = schema["@type"];
486+
switch (type) {
487+
case "xsd:string":
488+
case "http://www.w3.org/2001/XMLSchema#string":
489+
return this.Ts.Keyword.String;
490+
case "xsd:integer":
491+
case "xsd:int":
492+
case "http://www.w3.org/2001/XMLSchema#integer":
493+
case "http://www.w3.org/2001/XMLSchema#int":
494+
return this.Ts.Keyword.Number;
495+
case "xsd:boolean":
496+
case "http://www.w3.org/2001/XMLSchema#boolean":
497+
return this.Ts.Keyword.Boolean;
498+
case "xsd:dateTime":
499+
case "http://www.w3.org/2001/XMLSchema#dateTime":
500+
return this.Ts.Keyword.String; // or Date if preferred
501+
default:
502+
return this.Ts.Keyword.String;
503+
}
504+
}
505+
506+
// Fallback to primitive type detection
507+
if (schema.type) {
508+
switch (schema.type) {
509+
case "string":
510+
return this.Ts.Keyword.String;
511+
case "number":
512+
case "integer":
513+
return this.Ts.Keyword.Number;
514+
case "boolean":
515+
return this.Ts.Keyword.Boolean;
516+
default:
517+
return this.Ts.Keyword.String;
518+
}
519+
}
520+
}
521+
522+
return this.Ts.Keyword.String;
523+
};
524+
442525
update = (update: Partial<GenerateApiConfiguration["config"]>) => {
443526
objectAssign(this, update);
444527
};

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ export const SCHEMA_TYPES = {
5151
COMPLEX_ALL_OF: "allOf",
5252
COMPLEX_NOT: "not",
5353
COMPLEX_UNKNOWN: "__unknown",
54+
JSONLD_CONTEXT: "jsonld-context",
55+
JSONLD_ENTITY: "jsonld-entity",
56+
JSONLD_TYPE: "jsonld-type",
5457
} as const;

0 commit comments

Comments
 (0)