@twin.org/tools-core 0.0.3-next.3 → 0.0.3-next.31
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 +21 -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/models/embeddedSchemaMode.js +17 -0
- package/dist/es/models/embeddedSchemaMode.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 +132 -0
- package/dist/es/utils/fileUtils.js.map +1 -0
- package/dist/es/utils/importTypeQuerySchemaResolver.js +363 -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/jsDoc.js +138 -0
- package/dist/es/utils/jsDoc.js.map +1 -0
- package/dist/es/utils/jsonSchemaBuilder.js +3415 -0
- package/dist/es/utils/jsonSchemaBuilder.js.map +1 -0
- package/dist/es/utils/mappedTypeSchemaResolver.js +265 -0
- package/dist/es/utils/mappedTypeSchemaResolver.js.map +1 -0
- package/dist/es/utils/objectTransformer.js +161 -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 +177 -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 +319 -0
- package/dist/es/utils/typeScriptToSchema.js.map +1 -0
- package/dist/es/utils/utilityTypeSchemaMapper.js +475 -0
- package/dist/es/utils/utilityTypeSchemaMapper.js.map +1 -0
- package/dist/types/index.d.ts +21 -11
- package/dist/types/models/ITypeScriptToSchemaContext.d.ts +71 -0
- package/dist/types/models/ITypeScriptToSchemaDiagnostics.d.ts +31 -0
- package/dist/types/models/ITypeScriptToSchemaOptions.d.ts +22 -0
- package/dist/types/models/embeddedSchemaMode.d.ts +17 -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 +87 -0
- package/dist/types/utils/indexSignaturePatternResolver.d.ts +21 -0
- package/dist/types/utils/intersectionSchemaMerger.d.ts +16 -0
- package/dist/types/utils/jsDoc.d.ts +53 -0
- package/dist/types/utils/jsonSchemaBuilder.d.ts +671 -0
- package/dist/types/utils/mappedTypeSchemaResolver.d.ts +81 -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 +22 -0
- package/dist/types/utils/templateLiteralPatternBuilder.d.ts +12 -0
- package/dist/types/utils/typeScriptToSchema.d.ts +92 -0
- package/dist/types/utils/utilityTypeSchemaMapper.d.ts +141 -0
- package/docs/changelog.md +365 -34
- 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 +109 -0
- package/docs/reference/classes/IndexSignaturePatternResolver.md +69 -0
- package/docs/reference/classes/IntersectionSchemaMerger.md +48 -0
- package/docs/reference/classes/JsDoc.md +169 -0
- package/docs/reference/classes/JsonSchemaBuilder.md +2578 -0
- package/docs/reference/classes/MappedTypeSchemaResolver.md +275 -0
- package/docs/reference/classes/ObjectTransformer.md +119 -0
- package/docs/reference/classes/RegEx.md +99 -0
- package/docs/reference/classes/Resolver.md +52 -0
- package/docs/reference/classes/TemplateLiteralPatternBuilder.md +35 -0
- package/docs/reference/classes/TypeScriptToSchema.md +91 -0
- package/docs/reference/classes/UtilityTypeSchemaMapper.md +343 -0
- package/docs/reference/index.md +25 -11
- package/docs/reference/interfaces/ITypeScriptToSchemaContext.md +125 -0
- package/docs/reference/interfaces/ITypeScriptToSchemaDiagnostics.md +55 -0
- package/docs/reference/interfaces/ITypeScriptToSchemaOptions.md +44 -0
- package/docs/reference/type-aliases/EmbeddedSchemaMode.md +5 -0
- package/docs/reference/variables/EmbeddedSchemaMode.md +19 -0
- package/locales/en.json +32 -1
- package/package.json +6 -5
- 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 -258
- 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,81 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-models";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
import type { ITypeScriptToSchemaContext } from "../models/ITypeScriptToSchemaContext.js";
|
|
4
|
+
/**
|
|
5
|
+
* Static mapped-type schema transformation helpers.
|
|
6
|
+
*/
|
|
7
|
+
export declare class MappedTypeSchemaResolver {
|
|
8
|
+
/**
|
|
9
|
+
* Resolve mapped type output keys, including remapped key names via `as`.
|
|
10
|
+
* @param context The generation context.
|
|
11
|
+
* @param typeNode The mapped type node.
|
|
12
|
+
* @param mappedKeys The resolved source keys from the mapped type constraint.
|
|
13
|
+
* @param mappedTypeParameterName The mapped type parameter identifier.
|
|
14
|
+
* @returns The resolved source-to-output mapped key entries.
|
|
15
|
+
*/
|
|
16
|
+
static resolveMappedTypePropertyEntries(context: ITypeScriptToSchemaContext, typeNode: ts.MappedTypeNode, mappedKeys: string[], mappedTypeParameterName: string): {
|
|
17
|
+
sourceKey: string;
|
|
18
|
+
mappedKey: string;
|
|
19
|
+
}[] | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Resolve a remapped mapped-type key expression for a concrete source key.
|
|
22
|
+
* @param context The generation context.
|
|
23
|
+
* @param nameTypeNode The mapped type name remapping expression node.
|
|
24
|
+
* @param sourceKey The concrete source key currently being evaluated.
|
|
25
|
+
* @param mappedTypeParameterName The mapped type parameter identifier.
|
|
26
|
+
* @returns The remapped key, null when excluded via never, or undefined when unresolved.
|
|
27
|
+
*/
|
|
28
|
+
static resolveMappedTypeRemappedKey(context: ITypeScriptToSchemaContext, nameTypeNode: ts.TypeNode, sourceKey: string, mappedTypeParameterName: string): string | null | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Evaluate whether a concrete mapped key satisfies an `extends` condition.
|
|
31
|
+
* @param context The generation context.
|
|
32
|
+
* @param sourceKey The concrete source key being evaluated.
|
|
33
|
+
* @param extendsTypeNode The extends condition type node.
|
|
34
|
+
* @returns True when the key satisfies the condition, false when it does not, otherwise undefined.
|
|
35
|
+
*/
|
|
36
|
+
static evaluateMappedKeyExtendsCondition(context: ITypeScriptToSchemaContext, sourceKey: string, extendsTypeNode: ts.TypeNode): boolean | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Build a conservative fallback schema for mapped types whose key remapping cannot be resolved.
|
|
39
|
+
* @param context The generation context.
|
|
40
|
+
* @param typeNode The mapped type node.
|
|
41
|
+
* @param mappedKeys The resolved source keys from the mapped type constraint.
|
|
42
|
+
* @param mappedTypeParameterName The mapped type parameter identifier.
|
|
43
|
+
* @param sourceObjectSchema The optional source object schema for property lookups.
|
|
44
|
+
* @returns The fallback mapped type schema.
|
|
45
|
+
*/
|
|
46
|
+
static buildMappedTypeFallbackSchema(context: ITypeScriptToSchemaContext, typeNode: ts.MappedTypeNode, mappedKeys: string[], mappedTypeParameterName: string, sourceObjectSchema?: IJsonSchema): IJsonSchema;
|
|
47
|
+
/**
|
|
48
|
+
* Build fallback additionalProperties for unresolved mapped key remapping.
|
|
49
|
+
* @param context The generation context.
|
|
50
|
+
* @param typeNode The mapped type node.
|
|
51
|
+
* @param mappedKeys The resolved source keys from the mapped type constraint.
|
|
52
|
+
* @param mappedTypeParameterName The mapped type parameter identifier.
|
|
53
|
+
* @param sourceObjectSchema The optional source object schema for property lookups.
|
|
54
|
+
* @returns The fallback additionalProperties schema.
|
|
55
|
+
*/
|
|
56
|
+
static buildMappedTypeFallbackAdditionalProperties(context: ITypeScriptToSchemaContext, typeNode: ts.MappedTypeNode, mappedKeys: string[], mappedTypeParameterName: string, sourceObjectSchema?: IJsonSchema): IJsonSchema | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Merge mapped property schemas when multiple source keys remap to the same output key.
|
|
59
|
+
* @param existingSchema The existing schema already assigned to the mapped key.
|
|
60
|
+
* @param nextSchema The next schema to merge into the mapped key.
|
|
61
|
+
* @returns The merged schema.
|
|
62
|
+
*/
|
|
63
|
+
static mergeMappedTypePropertySchemas(existingSchema: IJsonSchema, nextSchema: IJsonSchema): IJsonSchema;
|
|
64
|
+
/**
|
|
65
|
+
* Resolve required remapped keys from the source object's required key set.
|
|
66
|
+
* @param mappedEntries The source-to-output mapped key entries.
|
|
67
|
+
* @param sourceObjectSchema The source object schema containing required keys.
|
|
68
|
+
* @returns The required output keys.
|
|
69
|
+
*/
|
|
70
|
+
static resolveMappedTypeSourceRequiredPropertyKeys(mappedEntries: {
|
|
71
|
+
sourceKey: string;
|
|
72
|
+
mappedKey: string;
|
|
73
|
+
}[], sourceObjectSchema: IJsonSchema): string[] | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Apply TypeScript intrinsic string remapping helpers to a key.
|
|
76
|
+
* @param intrinsicName The intrinsic helper name.
|
|
77
|
+
* @param value The input key value.
|
|
78
|
+
* @returns The remapped key value.
|
|
79
|
+
*/
|
|
80
|
+
private static applyIntrinsicMappedTypeKeyRemap;
|
|
81
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-models";
|
|
2
|
+
/**
|
|
3
|
+
* Applies common object-schema transformations used by the builder.
|
|
4
|
+
*/
|
|
5
|
+
export declare class ObjectTransformer {
|
|
6
|
+
/**
|
|
7
|
+
* Resolve a property schema from an object schema by property key.
|
|
8
|
+
* @param baseSchema The source object schema.
|
|
9
|
+
* @param propertyKey The property key to resolve.
|
|
10
|
+
* @returns The resolved property schema.
|
|
11
|
+
*/
|
|
12
|
+
static resolvePropertySchemaFromObjectSchema(baseSchema: IJsonSchema, propertyKey: string): IJsonSchema | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Remove keys from an object schema.
|
|
15
|
+
* @param baseSchema The source object schema.
|
|
16
|
+
* @param omittedKeys The keys to remove.
|
|
17
|
+
* @returns The transformed object schema.
|
|
18
|
+
*/
|
|
19
|
+
static omitKeysFromObjectSchema(baseSchema: IJsonSchema, omittedKeys: string[]): IJsonSchema;
|
|
20
|
+
/**
|
|
21
|
+
* Keep only keys from an object schema.
|
|
22
|
+
* @param baseSchema The source object schema.
|
|
23
|
+
* @param pickedKeys The keys to keep.
|
|
24
|
+
* @returns The transformed object schema.
|
|
25
|
+
*/
|
|
26
|
+
static pickKeysFromObjectSchema(baseSchema: IJsonSchema, pickedKeys: string[]): IJsonSchema;
|
|
27
|
+
/**
|
|
28
|
+
* Normalize schema description whitespace while preserving intentional line breaks.
|
|
29
|
+
* @param schema The schema to normalize.
|
|
30
|
+
* @returns The normalized schema.
|
|
31
|
+
*/
|
|
32
|
+
static normalizeSchemaDescriptions(schema: IJsonSchema): IJsonSchema;
|
|
33
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility methods for pattern matching and regex construction.
|
|
3
|
+
*/
|
|
4
|
+
export declare class RegEx {
|
|
5
|
+
/**
|
|
6
|
+
* Determine whether a reference mapping pattern matches an external reference.
|
|
7
|
+
* @param pattern The mapping pattern.
|
|
8
|
+
* @param packageName The external package name.
|
|
9
|
+
* @param typeName The imported type name.
|
|
10
|
+
* @param schemaTitle The derived schema title.
|
|
11
|
+
* @returns True if matched.
|
|
12
|
+
*/
|
|
13
|
+
static isReferencePatternMatch(pattern: string, packageName: string, typeName: string, schemaTitle: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Apply a reference mapping replacement to the first matching candidate.
|
|
16
|
+
* @param pattern The mapping pattern.
|
|
17
|
+
* @param replacement The replacement template.
|
|
18
|
+
* @param packageName The external package name.
|
|
19
|
+
* @param typeName The imported type name.
|
|
20
|
+
* @param schemaTitle The derived schema title.
|
|
21
|
+
* @returns The replaced value if a candidate matches.
|
|
22
|
+
*/
|
|
23
|
+
static applyReferencePatternReplacement(pattern: string, replacement: string, packageName: string, typeName: string, schemaTitle: string): string | undefined;
|
|
24
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
/**
|
|
3
|
+
* Resolve TypeScript type declarations from package names.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Resolver {
|
|
6
|
+
/**
|
|
7
|
+
* Resolve a type declaration AST from a package and type name.
|
|
8
|
+
* @param packageName The package to inspect.
|
|
9
|
+
* @param typeName The type to resolve.
|
|
10
|
+
* @param containingFilePath An optional source file path to use as the starting point for
|
|
11
|
+
* package resolution. When provided, TypeScript module resolution walks up from that file's
|
|
12
|
+
* directory, which allows transitive dependencies installed alongside the source file (e.g.
|
|
13
|
+
* in a sub-directory node_modules) to be found even when they are not reachable from the
|
|
14
|
+
* current working directory. The path is normalised to absolute before use;
|
|
15
|
+
* falls back to process.cwd() when omitted.
|
|
16
|
+
* @returns The resolved declaration AST.
|
|
17
|
+
*/
|
|
18
|
+
static resolveTypeDeclarationAst(packageName: string, typeName: string, containingFilePath?: string): {
|
|
19
|
+
sourceFile: ts.SourceFile;
|
|
20
|
+
declaration: ts.InterfaceDeclaration | ts.TypeAliasDeclaration;
|
|
21
|
+
} | undefined;
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
/**
|
|
3
|
+
* Builds regex patterns for TypeScript template literal types.
|
|
4
|
+
*/
|
|
5
|
+
export declare class TemplateLiteralPatternBuilder {
|
|
6
|
+
/**
|
|
7
|
+
* Build a regular-expression pattern for a template literal type.
|
|
8
|
+
* @param typeNode The template literal type node.
|
|
9
|
+
* @returns The regex pattern, or undefined if a safe pattern cannot be derived.
|
|
10
|
+
*/
|
|
11
|
+
static buildTemplateLiteralPattern(typeNode: ts.TemplateLiteralTypeNode): string | undefined;
|
|
12
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-models";
|
|
2
|
+
import type { ITypeScriptToSchemaOptions } from "../models/ITypeScriptToSchemaOptions.js";
|
|
3
|
+
/**
|
|
4
|
+
* Class for converting TypeScript types to JSON Schema.
|
|
5
|
+
*/
|
|
6
|
+
export declare class TypeScriptToSchema {
|
|
7
|
+
/**
|
|
8
|
+
* Generates a JSON schema from a TypeScript source file or type name.
|
|
9
|
+
* @param namespace The schema namespace.
|
|
10
|
+
* @param packageName The package name.
|
|
11
|
+
* @param schemas The package schema map.
|
|
12
|
+
* @param sourceFileOrTypeName The source file to process or type name to resolve.
|
|
13
|
+
* @param options Additional generation options.
|
|
14
|
+
* @returns The generated JSON schemas indexed by title.
|
|
15
|
+
*/
|
|
16
|
+
generateSchema(namespace: string, packageName: string, schemas: {
|
|
17
|
+
[id: string]: {
|
|
18
|
+
[id: string]: IJsonSchema;
|
|
19
|
+
};
|
|
20
|
+
}, sourceFileOrTypeName: string, options?: ITypeScriptToSchemaOptions): Promise<{
|
|
21
|
+
[id: string]: IJsonSchema;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Determine if a diagnostic originates from a specific package.
|
|
25
|
+
* @param path The schema or source path associated with the diagnostic.
|
|
26
|
+
* @param fileName The source filename associated with the diagnostic.
|
|
27
|
+
* @param packageName The package name to check, e.g. jose.
|
|
28
|
+
* @returns True if the diagnostic originated from the package.
|
|
29
|
+
*/
|
|
30
|
+
isDiagnosticFromPackage(path: string, fileName: string | undefined, packageName: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Rewrite refs to @json-schema embedded local types into the current schema's $defs.
|
|
33
|
+
* @param context The generation context.
|
|
34
|
+
* @param schema The root schema to rewrite.
|
|
35
|
+
* @param rootTitle The title of the root schema.
|
|
36
|
+
*/
|
|
37
|
+
private inlineEmbeddedSchemas;
|
|
38
|
+
/**
|
|
39
|
+
* Traverse a schema tree and move embedded local refs into the root schema's $defs.
|
|
40
|
+
* @param context The generation context.
|
|
41
|
+
* @param rootSchema The root schema document being rewritten.
|
|
42
|
+
* @param currentNode The current schema node.
|
|
43
|
+
* @param rootTitle The title of the root schema.
|
|
44
|
+
* @param ancestry Titles currently being expanded to avoid recursion cycles.
|
|
45
|
+
* @returns True if inline embedding changed this node or a descendant.
|
|
46
|
+
*/
|
|
47
|
+
private inlineEmbeddedSchemasInNode;
|
|
48
|
+
/**
|
|
49
|
+
* Flatten inline-expanded object allOf branches into the containing schema.
|
|
50
|
+
* @param schema The schema to flatten.
|
|
51
|
+
*/
|
|
52
|
+
private flattenInlineAllOfBranches;
|
|
53
|
+
/**
|
|
54
|
+
* Determine whether an allOf branch can be flattened into its parent after inline embedding.
|
|
55
|
+
* @param branch The allOf branch.
|
|
56
|
+
* @returns True if the branch is a plain object schema.
|
|
57
|
+
*/
|
|
58
|
+
private canFlattenInlineAllOfBranch;
|
|
59
|
+
/**
|
|
60
|
+
* Create a clone of an embedded schema suitable for defs or inline expansion.
|
|
61
|
+
* @param context The generation context.
|
|
62
|
+
* @param embeddedSchema The source schema.
|
|
63
|
+
* @param embeddedMode The embedding mode.
|
|
64
|
+
* @returns The cloned schema.
|
|
65
|
+
*/
|
|
66
|
+
private createEmbeddedSchemaClone;
|
|
67
|
+
/**
|
|
68
|
+
* Replace the contents of a schema node while keeping the same object reference.
|
|
69
|
+
* @param target The schema node to mutate.
|
|
70
|
+
* @param replacement The replacement content.
|
|
71
|
+
*/
|
|
72
|
+
private replaceSchemaNode;
|
|
73
|
+
/**
|
|
74
|
+
* Find a generated schema by its canonical id across all loaded packages.
|
|
75
|
+
* @param context The generation context.
|
|
76
|
+
* @param schemaId The schema id to locate.
|
|
77
|
+
* @returns The schema when found.
|
|
78
|
+
*/
|
|
79
|
+
private findSchemaById;
|
|
80
|
+
/**
|
|
81
|
+
* Resolve the local $defs key to use for an embedded schema.
|
|
82
|
+
* @param schema The embedded schema.
|
|
83
|
+
* @param schemaId The canonical schema id.
|
|
84
|
+
* @returns The local definition key.
|
|
85
|
+
*/
|
|
86
|
+
private getEmbeddedDefinitionKey;
|
|
87
|
+
/**
|
|
88
|
+
* Remove $comment fields from a schema tree.
|
|
89
|
+
* @param schema The schema tree to clean.
|
|
90
|
+
*/
|
|
91
|
+
private removeSchemaComments;
|
|
92
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { IJsonSchema } from "@twin.org/tools-models";
|
|
2
|
+
import * as ts from "typescript";
|
|
3
|
+
import type { ITypeScriptToSchemaContext } from "../models/ITypeScriptToSchemaContext.js";
|
|
4
|
+
/**
|
|
5
|
+
* Static utility-type schema mapping helpers.
|
|
6
|
+
*/
|
|
7
|
+
export declare class UtilityTypeSchemaMapper {
|
|
8
|
+
/**
|
|
9
|
+
* Map `Partial<T>` to an object schema with no required properties.
|
|
10
|
+
* @param context The generation context.
|
|
11
|
+
* @param typeNode The `Partial` type reference.
|
|
12
|
+
* @returns The mapped schema.
|
|
13
|
+
*/
|
|
14
|
+
static mapPartialUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Map `Required<T>` to an object schema with all properties required.
|
|
17
|
+
* @param context The generation context.
|
|
18
|
+
* @param typeNode The `Required` type reference.
|
|
19
|
+
* @returns The mapped schema.
|
|
20
|
+
*/
|
|
21
|
+
static mapRequiredUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Map `Pick<T, K>` to an object schema with selected keys preserved.
|
|
24
|
+
* @param context The generation context.
|
|
25
|
+
* @param typeNode The Pick type reference.
|
|
26
|
+
* @returns The mapped schema.
|
|
27
|
+
*/
|
|
28
|
+
static mapPickUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Map `Omit<T, K>` to an object schema with selected keys removed.
|
|
31
|
+
* @param context The generation context.
|
|
32
|
+
* @param typeNode The `Omit` type reference.
|
|
33
|
+
* @returns The mapped schema.
|
|
34
|
+
*/
|
|
35
|
+
static mapOmitUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Map `Exclude<T, U>` to a schema that removes `U` members from `T`.
|
|
38
|
+
* @param context The generation context.
|
|
39
|
+
* @param typeNode The `Exclude` type reference.
|
|
40
|
+
* @returns The mapped schema.
|
|
41
|
+
*/
|
|
42
|
+
static mapExcludeUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Map `Extract<T, U>` to a schema that keeps `U` members from `T`.
|
|
45
|
+
* @param context The generation context.
|
|
46
|
+
* @param typeNode The `Extract` type reference.
|
|
47
|
+
* @returns The mapped schema.
|
|
48
|
+
*/
|
|
49
|
+
static mapExtractUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Map `NonNullable<T>` by removing `null` and `undefined` branches from `T`.
|
|
52
|
+
* @param context The generation context.
|
|
53
|
+
* @param typeNode The `NonNullable` type reference.
|
|
54
|
+
* @returns The mapped schema.
|
|
55
|
+
*/
|
|
56
|
+
static mapNonNullableUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Map `Record<K, V>` to an object schema with key constraints where possible.
|
|
59
|
+
* @param context The generation context.
|
|
60
|
+
* @param typeNode The `Record` type reference.
|
|
61
|
+
* @returns The mapped schema.
|
|
62
|
+
*/
|
|
63
|
+
static mapRecordUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Map `JsonLdObject` utility types using key-removal and optional key-addition rules.
|
|
66
|
+
* @param context The generation context.
|
|
67
|
+
* @param typeNode The `JsonLdObject` utility type reference.
|
|
68
|
+
* @param options The mapping options for key removal and optional key addition.
|
|
69
|
+
* @param options.keysToRemove The property keys to remove from the base schema.
|
|
70
|
+
* @param options.keyToAdd The optional key to add after removal.
|
|
71
|
+
* @param options.isAddedKeyRequired True when the added key must be required.
|
|
72
|
+
* @returns The mapped schema.
|
|
73
|
+
*/
|
|
74
|
+
static mapJsonLdObjectUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode, options: {
|
|
75
|
+
keysToRemove: string[];
|
|
76
|
+
keyToAdd?: "id" | "@id" | "type" | "@type" | "@context";
|
|
77
|
+
isAddedKeyRequired?: boolean;
|
|
78
|
+
}): IJsonSchema | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Map `ObjectOrArray<T>` to a schema accepting `T` or `T[]`.
|
|
81
|
+
* @param context The generation context.
|
|
82
|
+
* @param typeNode The `ObjectOrArray` type reference.
|
|
83
|
+
* @returns The mapped schema.
|
|
84
|
+
*/
|
|
85
|
+
static mapObjectOrArrayUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* Map `SingleOccurrenceArray<T, U>` to a non-empty array containing exactly one `U`.
|
|
88
|
+
* @param context The generation context.
|
|
89
|
+
* @param typeNode The `SingleOccurrenceArray` type reference.
|
|
90
|
+
* @returns The mapped schema.
|
|
91
|
+
*/
|
|
92
|
+
static mapSingleOccurrenceArrayUtilityType(context: ITypeScriptToSchemaContext, typeNode: ts.TypeReferenceNode): IJsonSchema | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Determine whether a type node represents null or undefined.
|
|
95
|
+
* @param typeNode The type node to inspect.
|
|
96
|
+
* @returns True if the node represents null or undefined; otherwise false.
|
|
97
|
+
*/
|
|
98
|
+
private static isNullOrUndefinedTypeNode;
|
|
99
|
+
/**
|
|
100
|
+
* Extract literal keys from a Record key type argument.
|
|
101
|
+
* @param keyTypeNode The Record key type argument to inspect.
|
|
102
|
+
* @returns The extracted literal keys.
|
|
103
|
+
*/
|
|
104
|
+
private static extractRecordLiteralKeys;
|
|
105
|
+
/**
|
|
106
|
+
* Resolve a default schema for JsonLdObject utility key additions when the type argument is omitted.
|
|
107
|
+
* @param context The generation context.
|
|
108
|
+
* @param baseSchema The base object schema being transformed.
|
|
109
|
+
* @param keyToAdd The key to add when no explicit value type argument is provided.
|
|
110
|
+
* @returns The resolved default schema for the added key.
|
|
111
|
+
*/
|
|
112
|
+
private static mapJsonLdObjectDefaultSchemaByKey;
|
|
113
|
+
/**
|
|
114
|
+
* Resolve default id schema for JsonLdObjectWithId when Id type argument is omitted.
|
|
115
|
+
* @param baseSchema The base object schema being transformed.
|
|
116
|
+
* @returns The default id schema.
|
|
117
|
+
*/
|
|
118
|
+
private static mapJsonLdObjectWithIdDefaultIdSchema;
|
|
119
|
+
/**
|
|
120
|
+
* Resolve default type schema for JsonLdObjectWithType when Type argument is omitted.
|
|
121
|
+
* @param baseSchema The base object schema being transformed.
|
|
122
|
+
* @returns The default type schema.
|
|
123
|
+
*/
|
|
124
|
+
private static mapJsonLdObjectWithTypeDefaultTypeSchema;
|
|
125
|
+
/**
|
|
126
|
+
* Resolve default context schema for JsonLdObjectWithContext when Context argument is omitted.
|
|
127
|
+
* @param context The generation context.
|
|
128
|
+
* @param baseSchema The base object schema being transformed.
|
|
129
|
+
* @returns The default context schema.
|
|
130
|
+
*/
|
|
131
|
+
private static mapJsonLdObjectWithContextDefaultContextSchema;
|
|
132
|
+
/**
|
|
133
|
+
* Resolve default schema from either of two source keys, with fallback when both are absent.
|
|
134
|
+
* @param baseSchema The base object schema being transformed.
|
|
135
|
+
* @param firstKey The first property key to check.
|
|
136
|
+
* @param secondKey The second property key to check.
|
|
137
|
+
* @param fallbackSchema The fallback schema when neither key is present.
|
|
138
|
+
* @returns The resolved schema.
|
|
139
|
+
*/
|
|
140
|
+
private static mapJsonLdObjectDefaultEitherSchema;
|
|
141
|
+
}
|