@twin.org/tools-core 0.0.3-next.14 → 0.0.3-next.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/es/index.js +20 -11
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/ITypeScriptToSchemaContext.js +2 -0
- package/dist/es/models/ITypeScriptToSchemaContext.js.map +1 -0
- package/dist/es/models/ITypeScriptToSchemaDiagnostics.js +4 -0
- package/dist/es/models/ITypeScriptToSchemaDiagnostics.js.map +1 -0
- package/dist/es/models/ITypeScriptToSchemaOptions.js +4 -0
- package/dist/es/models/ITypeScriptToSchemaOptions.js.map +1 -0
- package/dist/es/utils/constants.js +43 -0
- package/dist/es/utils/constants.js.map +1 -0
- package/dist/es/utils/diagnosticReporter.js +32 -0
- package/dist/es/utils/diagnosticReporter.js.map +1 -0
- package/dist/es/utils/disallowedTypeGuard.js +151 -0
- package/dist/es/utils/disallowedTypeGuard.js.map +1 -0
- package/dist/es/utils/enum.js +152 -0
- package/dist/es/utils/enum.js.map +1 -0
- package/dist/es/utils/fileUtils.js +130 -0
- package/dist/es/utils/fileUtils.js.map +1 -0
- package/dist/es/utils/importTypeQuerySchemaResolver.js +328 -0
- package/dist/es/utils/importTypeQuerySchemaResolver.js.map +1 -0
- package/dist/es/utils/indexSignaturePatternResolver.js +94 -0
- package/dist/es/utils/indexSignaturePatternResolver.js.map +1 -0
- package/dist/es/utils/intersectionSchemaMerger.js +85 -0
- package/dist/es/utils/intersectionSchemaMerger.js.map +1 -0
- package/dist/es/utils/jsonSchemaBuilder.js +3189 -0
- package/dist/es/utils/jsonSchemaBuilder.js.map +1 -0
- package/dist/es/utils/mappedTypeSchemaResolver.js +231 -0
- package/dist/es/utils/mappedTypeSchemaResolver.js.map +1 -0
- package/dist/es/utils/objectTransformer.js +162 -0
- package/dist/es/utils/objectTransformer.js.map +1 -0
- package/dist/es/utils/regEx.js +128 -0
- package/dist/es/utils/regEx.js.map +1 -0
- package/dist/es/utils/resolver.js +164 -0
- package/dist/es/utils/resolver.js.map +1 -0
- package/dist/es/utils/templateLiteralPatternBuilder.js +94 -0
- package/dist/es/utils/templateLiteralPatternBuilder.js.map +1 -0
- package/dist/es/utils/typeScriptToSchema.js +102 -0
- package/dist/es/utils/typeScriptToSchema.js.map +1 -0
- package/dist/es/utils/utility.js +134 -0
- package/dist/es/utils/utility.js.map +1 -0
- package/dist/es/utils/utilityTypeSchemaMapper.js +412 -0
- package/dist/es/utils/utilityTypeSchemaMapper.js.map +1 -0
- package/dist/types/index.d.ts +20 -11
- package/dist/types/models/ITypeScriptToSchemaContext.d.ts +64 -0
- package/dist/types/models/ITypeScriptToSchemaDiagnostics.d.ts +31 -0
- package/dist/types/models/ITypeScriptToSchemaOptions.d.ts +22 -0
- package/dist/types/utils/constants.d.ts +13 -0
- package/dist/types/utils/diagnosticReporter.d.ts +17 -0
- package/dist/types/utils/disallowedTypeGuard.d.ts +16 -0
- package/dist/types/utils/enum.d.ts +42 -0
- package/dist/types/utils/fileUtils.d.ts +66 -0
- package/dist/types/utils/importTypeQuerySchemaResolver.d.ts +52 -0
- package/dist/types/utils/indexSignaturePatternResolver.d.ts +21 -0
- package/dist/types/utils/intersectionSchemaMerger.d.ts +16 -0
- package/dist/types/utils/jsonSchemaBuilder.d.ts +721 -0
- package/dist/types/utils/mappedTypeSchemaResolver.d.ts +46 -0
- package/dist/types/utils/objectTransformer.d.ts +33 -0
- package/dist/types/utils/regEx.d.ts +24 -0
- package/dist/types/utils/resolver.d.ts +16 -0
- package/dist/types/utils/templateLiteralPatternBuilder.d.ts +12 -0
- package/dist/types/utils/typeScriptToSchema.d.ts +31 -0
- package/dist/types/utils/utility.d.ts +58 -0
- package/dist/types/utils/utilityTypeSchemaMapper.d.ts +92 -0
- package/docs/changelog.md +22 -1
- package/docs/examples.md +87 -1
- package/docs/reference/classes/Constants.md +29 -0
- package/docs/reference/classes/DiagnosticReporter.md +49 -0
- package/docs/reference/classes/DisallowedTypeGuard.md +35 -0
- package/docs/reference/classes/Enum.md +93 -0
- package/docs/reference/classes/FileUtils.md +237 -0
- package/docs/reference/classes/ImportTypeQuerySchemaResolver.md +87 -0
- package/docs/reference/classes/IndexSignaturePatternResolver.md +69 -0
- package/docs/reference/classes/IntersectionSchemaMerger.md +48 -0
- package/docs/reference/classes/JsonSchemaBuilder.md +2772 -0
- package/docs/reference/classes/MappedTypeSchemaResolver.md +211 -0
- package/docs/reference/classes/ObjectTransformer.md +119 -0
- package/docs/reference/classes/RegEx.md +99 -0
- package/docs/reference/classes/Resolver.md +41 -0
- package/docs/reference/classes/TemplateLiteralPatternBuilder.md +35 -0
- package/docs/reference/classes/TypeScriptToSchema.md +91 -0
- package/docs/reference/classes/Utility.md +164 -0
- package/docs/reference/classes/UtilityTypeSchemaMapper.md +341 -0
- package/docs/reference/index.md +20 -14
- package/docs/reference/interfaces/ITypeScriptToSchemaContext.md +113 -0
- package/docs/reference/interfaces/ITypeScriptToSchemaDiagnostics.md +55 -0
- package/docs/reference/interfaces/ITypeScriptToSchemaOptions.md +44 -0
- package/locales/en.json +30 -1
- package/package.json +4 -3
- package/dist/es/models/IJsonSchema.js +0 -2
- package/dist/es/models/IJsonSchema.js.map +0 -1
- package/dist/es/models/IOpenApi.js +0 -2
- package/dist/es/models/IOpenApi.js.map +0 -1
- package/dist/es/models/IOpenApiExample.js +0 -4
- package/dist/es/models/IOpenApiExample.js.map +0 -1
- package/dist/es/models/IOpenApiHeader.js +0 -4
- package/dist/es/models/IOpenApiHeader.js.map +0 -1
- package/dist/es/models/IOpenApiPathMethod.js +0 -2
- package/dist/es/models/IOpenApiPathMethod.js.map +0 -1
- package/dist/es/models/IOpenApiResponse.js +0 -2
- package/dist/es/models/IOpenApiResponse.js.map +0 -1
- package/dist/es/models/IOpenApiSecurityScheme.js +0 -4
- package/dist/es/models/IOpenApiSecurityScheme.js.map +0 -1
- package/dist/es/models/IPackageJson.js +0 -4
- package/dist/es/models/IPackageJson.js.map +0 -1
- package/dist/es/models/jsonTypeName.js +0 -2
- package/dist/es/models/jsonTypeName.js.map +0 -1
- package/dist/es/utils/jsonSchemaHelper.js +0 -282
- package/dist/es/utils/jsonSchemaHelper.js.map +0 -1
- package/dist/es/utils/openApiHelper.js +0 -12
- package/dist/es/utils/openApiHelper.js.map +0 -1
- package/dist/types/models/IJsonSchema.d.ts +0 -5
- package/dist/types/models/IOpenApi.d.ts +0 -54
- package/dist/types/models/IOpenApiExample.d.ts +0 -13
- package/dist/types/models/IOpenApiHeader.d.ts +0 -19
- package/dist/types/models/IOpenApiPathMethod.d.ts +0 -65
- package/dist/types/models/IOpenApiResponse.d.ts +0 -32
- package/dist/types/models/IOpenApiSecurityScheme.d.ts +0 -25
- package/dist/types/models/IPackageJson.d.ts +0 -15
- package/dist/types/models/jsonTypeName.d.ts +0 -5
- package/dist/types/utils/jsonSchemaHelper.d.ts +0 -78
- package/dist/types/utils/openApiHelper.d.ts +0 -9
- package/docs/reference/classes/JsonSchemaHelper.md +0 -233
- package/docs/reference/classes/OpenApiHelper.md +0 -21
- package/docs/reference/interfaces/IOpenApi.md +0 -103
- package/docs/reference/interfaces/IOpenApiExample.md +0 -19
- package/docs/reference/interfaces/IOpenApiHeader.md +0 -31
- package/docs/reference/interfaces/IOpenApiPathMethod.md +0 -119
- package/docs/reference/interfaces/IOpenApiResponse.md +0 -35
- package/docs/reference/interfaces/IOpenApiSecurityScheme.md +0 -43
- package/docs/reference/interfaces/IPackageJson.md +0 -23
- package/docs/reference/type-aliases/IJsonSchema.md +0 -5
- package/docs/reference/type-aliases/JsonTypeName.md +0 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexSignaturePatternResolver.js","sourceRoot":"","sources":["../../../src/utils/indexSignaturePatternResolver.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,6BAA6B,EAAE,MAA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AAwC,EAAE,CACnD,EAAE,CAAC,sBAAsB,CAAC,SAAS,CAAC,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,KAAK,QAAQ,CACzE,CAAC;YAEF,IAAI,SAAS,EAAE,CAAC;gBACf,kBAAkB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACjC,MAAM,oBAAoB,GAAG,6BAA6B,CAAC,8BAA8B,CACxF,OAAO,EACP,SAAS,CAAC,IAAI,EACd,kBAAkB,EAClB,uBAAuB,CACvB,CAAC;gBACF,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;gBACpC,OAAO,oBAAoB,CAAC;YAC7B,CAAC;QACF,CAAC;QAED,OAAO,SAAS,CAAC;IAClB,CAAC;CACD","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport * as ts from \"typescript\";\nimport { TemplateLiteralPatternBuilder } from \"./templateLiteralPatternBuilder.js\";\nimport type { ITypeScriptToSchemaContext } from \"../models/ITypeScriptToSchemaContext.js\";\n\n/**\n * Resolves regex patterns for index signature key types.\n */\nexport class IndexSignaturePatternResolver {\n\t/**\n\t * Determine whether an index signature can be represented as JSON object additionalProperties.\n\t * @param member The index signature declaration.\n\t * @returns True if supported.\n\t */\n\tpublic static isSupportedIndexSignature(member: ts.IndexSignatureDeclaration): boolean {\n\t\t// [key: string]: Value (index signature, first parameter is the key)\n\t\tconst indexParameter = member.parameters[0];\n\t\tif (!indexParameter?.type) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// [key: string]: Value or [key: number]: Value (string or numeric key)\n\t\treturn (\n\t\t\tindexParameter.type.kind === ts.SyntaxKind.StringKeyword ||\n\t\t\tindexParameter.type.kind === ts.SyntaxKind.NumberKeyword\n\t\t);\n\t}\n\n\t/**\n\t * Extract a regex pattern for an index signature key type when representable.\n\t * @param context The generation context.\n\t * @param member The index signature declaration.\n\t * @param getTypeParameterBinding Callback for resolving generic bindings.\n\t * @returns The property-name pattern, if derivable.\n\t */\n\tpublic static extractIndexSignaturePattern(\n\t\tcontext: ITypeScriptToSchemaContext,\n\t\tmember: ts.IndexSignatureDeclaration,\n\t\tgetTypeParameterBinding: (\n\t\t\tcontext: ITypeScriptToSchemaContext,\n\t\t\ttypeName: string\n\t\t) => ts.TypeNode | null | undefined\n\t): string | undefined {\n\t\tconst indexParameterType = member.parameters[0]?.type;\n\t\tif (!indexParameterType) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst resolvedTemplateType = IndexSignaturePatternResolver.resolveTemplateLiteralTypeNode(\n\t\t\tcontext,\n\t\t\tindexParameterType,\n\t\t\tnew Set<string>(),\n\t\t\tgetTypeParameterBinding\n\t\t);\n\n\t\treturn resolvedTemplateType\n\t\t\t? TemplateLiteralPatternBuilder.buildTemplateLiteralPattern(resolvedTemplateType)\n\t\t\t: undefined;\n\t}\n\n\t/**\n\t * Resolve a template literal type node from a key-type expression.\n\t * Handles parenthesised, readonly-wrapped, and type-aliased variants by recursing.\n\t * A cycle guard via resolvingTypeNames prevents unbounded recursion on self-referential types.\n\t * @param context The generation context.\n\t * @param typeNode The key type node.\n\t * @param resolvingTypeNames Type names currently being resolved.\n\t * @param getTypeParameterBinding Callback for resolving generic bindings.\n\t * @returns A template literal type node when resolvable.\n\t * @internal\n\t */\n\tprivate static resolveTemplateLiteralTypeNode(\n\t\tcontext: ITypeScriptToSchemaContext,\n\t\ttypeNode: ts.TypeNode,\n\t\tresolvingTypeNames: Set<string>,\n\t\tgetTypeParameterBinding: (\n\t\t\tcontext: ITypeScriptToSchemaContext,\n\t\t\ttypeName: string\n\t\t) => ts.TypeNode | null | undefined\n\t): ts.TemplateLiteralTypeNode | undefined {\n\t\t// `prefix-${T}` or `${K}` (already a template literal type)\n\t\tif (ts.isTemplateLiteralTypeNode(typeNode)) {\n\t\t\treturn typeNode;\n\t\t}\n\n\t\t// (KeyType) (parenthesised type, unwrap and recurse)\n\t\tif (ts.isParenthesizedTypeNode(typeNode)) {\n\t\t\treturn IndexSignaturePatternResolver.resolveTemplateLiteralTypeNode(\n\t\t\t\tcontext,\n\t\t\t\ttypeNode.type,\n\t\t\t\tresolvingTypeNames,\n\t\t\t\tgetTypeParameterBinding\n\t\t\t);\n\t\t}\n\n\t\t// readonly KeyType (readonly-wrapped type operator, strip modifier and recurse)\n\t\tif (ts.isTypeOperatorNode(typeNode) && typeNode.operator === ts.SyntaxKind.ReadonlyKeyword) {\n\t\t\treturn IndexSignaturePatternResolver.resolveTemplateLiteralTypeNode(\n\t\t\t\tcontext,\n\t\t\t\ttypeNode.type,\n\t\t\t\tresolvingTypeNames,\n\t\t\t\tgetTypeParameterBinding\n\t\t\t);\n\t\t}\n\n\t\t// MyKeyType (named type reference, resolve via binding or type alias in source file)\n\t\tif (ts.isTypeReferenceNode(typeNode)) {\n\t\t\t// Namespace.TypeName (qualified name) or TypeName (plain identifier)\n\t\t\tconst typeName = ts.isIdentifier(typeNode.typeName)\n\t\t\t\t? typeNode.typeName.text\n\t\t\t\t: typeNode.typeName.right.text;\n\n\t\t\tif (resolvingTypeNames.has(typeName)) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tconst typeParameterBinding = getTypeParameterBinding(context, typeName);\n\t\t\tif (typeParameterBinding) {\n\t\t\t\treturn IndexSignaturePatternResolver.resolveTemplateLiteralTypeNode(\n\t\t\t\t\tcontext,\n\t\t\t\t\ttypeParameterBinding,\n\t\t\t\t\tresolvingTypeNames,\n\t\t\t\t\tgetTypeParameterBinding\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst sourceFile = context.activeSourceFile;\n\t\t\tif (!sourceFile) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\t// type MyKeyType = `prefix-${string}` (look up a type alias in the active source file)\n\t\t\tconst typeAlias = sourceFile.statements.find(\n\t\t\t\t(statement): statement is ts.TypeAliasDeclaration =>\n\t\t\t\t\tts.isTypeAliasDeclaration(statement) && statement.name.text === typeName\n\t\t\t);\n\n\t\t\tif (typeAlias) {\n\t\t\t\tresolvingTypeNames.add(typeName);\n\t\t\t\tconst resolvedTemplateType = IndexSignaturePatternResolver.resolveTemplateLiteralTypeNode(\n\t\t\t\t\tcontext,\n\t\t\t\t\ttypeAlias.type,\n\t\t\t\t\tresolvingTypeNames,\n\t\t\t\t\tgetTypeParameterBinding\n\t\t\t\t);\n\t\t\t\tresolvingTypeNames.delete(typeName);\n\t\t\t\treturn resolvedTemplateType;\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Copyright 2026 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { Is, ObjectHelper } from "@twin.org/core";
|
|
4
|
+
/**
|
|
5
|
+
* Merges compatible object intersections into a single JSON schema object.
|
|
6
|
+
*/
|
|
7
|
+
export class IntersectionSchemaMerger {
|
|
8
|
+
/**
|
|
9
|
+
* Merge simple object intersection parts into a single object schema.
|
|
10
|
+
* Supports local/known object refs by expanding them before merge.
|
|
11
|
+
* @param context The generation context.
|
|
12
|
+
* @param schemas The mapped intersection schemas.
|
|
13
|
+
* @param toInlineUtilityObjectSchema Callback for converting referenced schemas to inline forms.
|
|
14
|
+
* @returns The merged schema, or undefined when merge is not safe.
|
|
15
|
+
*/
|
|
16
|
+
static mergeIntersectionObjectSchemas(context, schemas, toInlineUtilityObjectSchema) {
|
|
17
|
+
const mergedProperties = {};
|
|
18
|
+
const mergedRequired = new Set();
|
|
19
|
+
const preservedRefs = new Set();
|
|
20
|
+
for (const schema of schemas) {
|
|
21
|
+
const isRefSchema = Is.stringValue(schema.$ref);
|
|
22
|
+
if (isRefSchema && schema.$ref) {
|
|
23
|
+
preservedRefs.add(schema.$ref);
|
|
24
|
+
}
|
|
25
|
+
const objectSchema = IntersectionSchemaMerger.resolveIntersectionObjectSchema(context, schema, toInlineUtilityObjectSchema);
|
|
26
|
+
if (!objectSchema) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
if (objectSchema.$ref ||
|
|
30
|
+
objectSchema.allOf ||
|
|
31
|
+
objectSchema.anyOf ||
|
|
32
|
+
objectSchema.oneOf ||
|
|
33
|
+
objectSchema.items) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
if (!isRefSchema) {
|
|
37
|
+
if (Is.object(objectSchema.properties)) {
|
|
38
|
+
for (const [propertyName, propertySchema] of Object.entries(objectSchema.properties)) {
|
|
39
|
+
mergedProperties[propertyName] = propertySchema;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (Is.array(objectSchema.required)) {
|
|
43
|
+
for (const propertyName of objectSchema.required) {
|
|
44
|
+
mergedRequired.add(propertyName);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
type: "object",
|
|
51
|
+
properties: Object.keys(mergedProperties).length > 0 ? mergedProperties : undefined,
|
|
52
|
+
required: mergedRequired.size > 0 ? [...mergedRequired] : undefined,
|
|
53
|
+
allOf: preservedRefs.size > 0
|
|
54
|
+
? [...preservedRefs].map(schemaRef => ({
|
|
55
|
+
$ref: schemaRef
|
|
56
|
+
}))
|
|
57
|
+
: undefined
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolve an intersection component to a plain object schema when possible.
|
|
62
|
+
* @param context The generation context.
|
|
63
|
+
* @param schema The mapped intersection component schema.
|
|
64
|
+
* @returns The resolved object schema.
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
static resolveIntersectionObjectSchema(context, schema, toInlineUtilityObjectSchema) {
|
|
68
|
+
if (schema.type === "object" && Is.object(schema.properties)) {
|
|
69
|
+
return ObjectHelper.clone(schema);
|
|
70
|
+
}
|
|
71
|
+
if (schema.$ref) {
|
|
72
|
+
for (const packageSchemas of Object.values(context.schemas)) {
|
|
73
|
+
for (const referencedSchema of Object.values(packageSchemas)) {
|
|
74
|
+
if (referencedSchema.$id === schema.$ref &&
|
|
75
|
+
referencedSchema.type === "object" &&
|
|
76
|
+
Is.object(referencedSchema.properties)) {
|
|
77
|
+
return toInlineUtilityObjectSchema(referencedSchema);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=intersectionSchemaMerger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intersectionSchemaMerger.js","sourceRoot":"","sources":["../../../src/utils/intersectionSchemaMerger.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAIlD;;GAEG;AACH,MAAM,OAAO,wBAAwB;IACpC;;;;;;;OAOG;IACI,MAAM,CAAC,8BAA8B,CAC3C,OAAmC,EACnC,OAAsB,EACtB,2BAAiE;QAEjE,MAAM,gBAAgB,GAAkC,EAAE,CAAC;QAC3D,MAAM,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;QACzC,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;QAExC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC9B,MAAM,WAAW,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAChD,IAAI,WAAW,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;gBAChC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAChC,CAAC;YAED,MAAM,YAAY,GAAG,wBAAwB,CAAC,+BAA+B,CAC5E,OAAO,EACP,MAAM,EACN,2BAA2B,CAC3B,CAAC;YACF,IAAI,CAAC,YAAY,EAAE,CAAC;gBACnB,OAAO,SAAS,CAAC;YAClB,CAAC;YACD,IACC,YAAY,CAAC,I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BAA+B,CAC7C,OAAmC,EACnC,MAAmB,EACnB,2BAAiE;QAEjE,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;YAC9D,OAAO,YAAY,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACnC,CAAC;QAED,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;YACjB,KAAK,MAAM,cAAc,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC7D,KAAK,MAAM,gBAAgB,IAAI,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC;oBAC9D,IACC,gBAAgB,CAAC,GAAG,KAAK,MAAM,CAAC,IAAI;wBACpC,gBAAgB,CAAC,IAAI,KAAK,QAAQ;wBAClC,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,UAAU,CAAC,EACrC,CAAC;wBACF,OAAO,2BAA2B,CAAC,gBAAgB,CAAC,CAAC;oBACtD,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;QAED,OAAO,SAAS,CAAC;IAClB,CAAC;CACD","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is, ObjectHelper } from \"@twin.org/core\";\nimport type { IJsonSchema } from \"@twin.org/tools-models\";\nimport type { ITypeScriptToSchemaContext } from \"../models/ITypeScriptToSchemaContext.js\";\n\n/**\n * Merges compatible object intersections into a single JSON schema object.\n */\nexport class IntersectionSchemaMerger {\n\t/**\n\t * Merge simple object intersection parts into a single object schema.\n\t * Supports local/known object refs by expanding them before merge.\n\t * @param context The generation context.\n\t * @param schemas The mapped intersection schemas.\n\t * @param toInlineUtilityObjectSchema Callback for converting referenced schemas to inline forms.\n\t * @returns The merged schema, or undefined when merge is not safe.\n\t */\n\tpublic static mergeIntersectionObjectSchemas(\n\t\tcontext: ITypeScriptToSchemaContext,\n\t\tschemas: IJsonSchema[],\n\t\ttoInlineUtilityObjectSchema: (schema: IJsonSchema) => IJsonSchema\n\t): IJsonSchema | undefined {\n\t\tconst mergedProperties: { [id: string]: IJsonSchema } = {};\n\t\tconst mergedRequired = new Set<string>();\n\t\tconst preservedRefs = new Set<string>();\n\n\t\tfor (const schema of schemas) {\n\t\t\tconst isRefSchema = Is.stringValue(schema.$ref);\n\t\t\tif (isRefSchema && schema.$ref) {\n\t\t\t\tpreservedRefs.add(schema.$ref);\n\t\t\t}\n\n\t\t\tconst objectSchema = IntersectionSchemaMerger.resolveIntersectionObjectSchema(\n\t\t\t\tcontext,\n\t\t\t\tschema,\n\t\t\t\ttoInlineUtilityObjectSchema\n\t\t\t);\n\t\t\tif (!objectSchema) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tif (\n\t\t\t\tobjectSchema.$ref ||\n\t\t\t\tobjectSchema.allOf ||\n\t\t\t\tobjectSchema.anyOf ||\n\t\t\t\tobjectSchema.oneOf ||\n\t\t\t\tobjectSchema.items\n\t\t\t) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\n\t\t\tif (!isRefSchema) {\n\t\t\t\tif (Is.object(objectSchema.properties)) {\n\t\t\t\t\tfor (const [propertyName, propertySchema] of Object.entries(objectSchema.properties)) {\n\t\t\t\t\t\tmergedProperties[propertyName] = propertySchema;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (Is.array(objectSchema.required)) {\n\t\t\t\t\tfor (const propertyName of objectSchema.required) {\n\t\t\t\t\t\tmergedRequired.add(propertyName);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"object\",\n\t\t\tproperties: Object.keys(mergedProperties).length > 0 ? mergedProperties : undefined,\n\t\t\trequired: mergedRequired.size > 0 ? [...mergedRequired] : undefined,\n\t\t\tallOf:\n\t\t\t\tpreservedRefs.size > 0\n\t\t\t\t\t? [...preservedRefs].map(schemaRef => ({\n\t\t\t\t\t\t\t$ref: schemaRef\n\t\t\t\t\t\t}))\n\t\t\t\t\t: undefined\n\t\t};\n\t}\n\n\t/**\n\t * Resolve an intersection component to a plain object schema when possible.\n\t * @param context The generation context.\n\t * @param schema The mapped intersection component schema.\n\t * @returns The resolved object schema.\n\t * @internal\n\t */\n\tprivate static resolveIntersectionObjectSchema(\n\t\tcontext: ITypeScriptToSchemaContext,\n\t\tschema: IJsonSchema,\n\t\ttoInlineUtilityObjectSchema: (schema: IJsonSchema) => IJsonSchema\n\t): IJsonSchema | undefined {\n\t\tif (schema.type === \"object\" && Is.object(schema.properties)) {\n\t\t\treturn ObjectHelper.clone(schema);\n\t\t}\n\n\t\tif (schema.$ref) {\n\t\t\tfor (const packageSchemas of Object.values(context.schemas)) {\n\t\t\t\tfor (const referencedSchema of Object.values(packageSchemas)) {\n\t\t\t\t\tif (\n\t\t\t\t\t\treferencedSchema.$id === schema.$ref &&\n\t\t\t\t\t\treferencedSchema.type === \"object\" &&\n\t\t\t\t\t\tIs.object(referencedSchema.properties)\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn toInlineUtilityObjectSchema(referencedSchema);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n"]}
|