@lssm/lib.contracts-transformers 0.0.0-canary-20251217054315 → 0.0.0-canary-20251217060433
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/dist/common/index.d.ts +3 -0
- package/dist/common/types.d.ts +152 -0
- package/dist/common/utils.d.ts +51 -0
- package/dist/index.d.ts +9 -0
- package/dist/openapi/differ.d.ts +41 -0
- package/dist/openapi/exporter.d.ts +27 -0
- package/dist/openapi/importer.d.ts +15 -0
- package/dist/openapi/index.d.ts +7 -0
- package/dist/openapi/parser.d.ts +31 -0
- package/dist/openapi/schema-converter.d.ts +69 -0
- package/dist/openapi/types.d.ts +221 -0
- package/package.json +8 -8
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { DiffChange, DiffChangeType, ImportResult, ImportedSpec, SpecDiff, SpecSource, SyncResult, TransportHints, ValidationResult } from "./types.js";
|
|
2
|
+
import { deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier } from "./utils.js";
|
|
3
|
+
export { DiffChange, DiffChangeType, ImportResult, ImportedSpec, SpecDiff, SpecSource, SyncResult, TransportHints, ValidationResult, deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { AnyContractSpec } from "@lssm/lib.contracts";
|
|
2
|
+
|
|
3
|
+
//#region src/common/types.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Source information for imported specs.
|
|
7
|
+
*/
|
|
8
|
+
interface SpecSource {
|
|
9
|
+
/** The format the spec was imported from */
|
|
10
|
+
type: 'openapi' | 'asyncapi' | 'graphql' | 'protobuf';
|
|
11
|
+
/** URL if fetched from remote */
|
|
12
|
+
url?: string;
|
|
13
|
+
/** File path if loaded from local file */
|
|
14
|
+
file?: string;
|
|
15
|
+
/** Original identifier in source format */
|
|
16
|
+
sourceId: string;
|
|
17
|
+
/** Timestamp of import */
|
|
18
|
+
importedAt: Date;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Transport hints preserved from external formats.
|
|
22
|
+
* These enable accurate round-trip transformations.
|
|
23
|
+
*/
|
|
24
|
+
interface TransportHints {
|
|
25
|
+
rest?: {
|
|
26
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
27
|
+
path: string;
|
|
28
|
+
params?: {
|
|
29
|
+
path?: string[];
|
|
30
|
+
query?: string[];
|
|
31
|
+
header?: string[];
|
|
32
|
+
cookie?: string[];
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
graphql?: {
|
|
36
|
+
type: 'query' | 'mutation' | 'subscription';
|
|
37
|
+
fieldName: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Result of importing a single spec from an external format.
|
|
42
|
+
*/
|
|
43
|
+
interface ImportedSpec {
|
|
44
|
+
/** The generated ContractSpec */
|
|
45
|
+
spec: AnyContractSpec;
|
|
46
|
+
/** Generated TypeScript code for the spec */
|
|
47
|
+
code: string;
|
|
48
|
+
/** Suggested file name for the spec */
|
|
49
|
+
fileName: string;
|
|
50
|
+
/** Source information for provenance tracking */
|
|
51
|
+
source: SpecSource;
|
|
52
|
+
/** Transport hints for accurate round-trips */
|
|
53
|
+
transportHints: TransportHints;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Result of an import operation.
|
|
57
|
+
*/
|
|
58
|
+
interface ImportResult {
|
|
59
|
+
/** Successfully imported specs */
|
|
60
|
+
specs: ImportedSpec[];
|
|
61
|
+
/** Specs that were skipped (e.g., unsupported features) */
|
|
62
|
+
skipped: Array<{
|
|
63
|
+
sourceId: string;
|
|
64
|
+
reason: string;
|
|
65
|
+
}>;
|
|
66
|
+
/** Errors encountered during import */
|
|
67
|
+
errors: Array<{
|
|
68
|
+
sourceId: string;
|
|
69
|
+
error: string;
|
|
70
|
+
}>;
|
|
71
|
+
/** Summary statistics */
|
|
72
|
+
summary: {
|
|
73
|
+
total: number;
|
|
74
|
+
imported: number;
|
|
75
|
+
skipped: number;
|
|
76
|
+
errors: number;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Type of change detected during diff.
|
|
81
|
+
*/
|
|
82
|
+
type DiffChangeType = 'added' | 'removed' | 'modified' | 'type_changed' | 'required_changed';
|
|
83
|
+
/**
|
|
84
|
+
* A single change detected during diff.
|
|
85
|
+
*/
|
|
86
|
+
interface DiffChange {
|
|
87
|
+
/** JSON path to the changed property */
|
|
88
|
+
path: string;
|
|
89
|
+
/** Type of change */
|
|
90
|
+
type: DiffChangeType;
|
|
91
|
+
/** Previous value (for modified/removed) */
|
|
92
|
+
oldValue?: unknown;
|
|
93
|
+
/** New value (for modified/added) */
|
|
94
|
+
newValue?: unknown;
|
|
95
|
+
/** Human-readable description of the change */
|
|
96
|
+
description: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Result of diffing two specs.
|
|
100
|
+
*/
|
|
101
|
+
interface SpecDiff {
|
|
102
|
+
/** Identifier for the operation */
|
|
103
|
+
operationId: string;
|
|
104
|
+
/** Existing ContractSpec (if any) */
|
|
105
|
+
existing?: AnyContractSpec;
|
|
106
|
+
/** Incoming imported spec */
|
|
107
|
+
incoming: ImportedSpec;
|
|
108
|
+
/** List of detected changes */
|
|
109
|
+
changes: DiffChange[];
|
|
110
|
+
/** Whether specs are semantically equivalent */
|
|
111
|
+
isEquivalent: boolean;
|
|
112
|
+
/** User's resolution choice (for interactive sync) */
|
|
113
|
+
resolution?: 'keep' | 'replace' | 'merge' | 'skip';
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Result of a sync operation.
|
|
117
|
+
*/
|
|
118
|
+
interface SyncResult {
|
|
119
|
+
/** Specs that were added (new imports) */
|
|
120
|
+
added: ImportedSpec[];
|
|
121
|
+
/** Specs that were updated */
|
|
122
|
+
updated: Array<{
|
|
123
|
+
spec: ImportedSpec;
|
|
124
|
+
changes: DiffChange[];
|
|
125
|
+
}>;
|
|
126
|
+
/** Specs that were kept unchanged */
|
|
127
|
+
unchanged: string[];
|
|
128
|
+
/** Specs that had conflicts requiring resolution */
|
|
129
|
+
conflicts: SpecDiff[];
|
|
130
|
+
/** Summary statistics */
|
|
131
|
+
summary: {
|
|
132
|
+
added: number;
|
|
133
|
+
updated: number;
|
|
134
|
+
unchanged: number;
|
|
135
|
+
conflicts: number;
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Validation result for a single spec.
|
|
140
|
+
*/
|
|
141
|
+
interface ValidationResult {
|
|
142
|
+
/** Whether the spec is valid against the source */
|
|
143
|
+
valid: boolean;
|
|
144
|
+
/** Detected differences */
|
|
145
|
+
diffs: DiffChange[];
|
|
146
|
+
/** Validation errors */
|
|
147
|
+
errors: string[];
|
|
148
|
+
/** Validation warnings */
|
|
149
|
+
warnings: string[];
|
|
150
|
+
}
|
|
151
|
+
//#endregion
|
|
152
|
+
export { DiffChange, DiffChangeType, ImportResult, ImportedSpec, SpecDiff, SpecSource, SyncResult, TransportHints, ValidationResult };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/common/utils.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Common utilities for contract transformations.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Convert a string to PascalCase.
|
|
7
|
+
*/
|
|
8
|
+
declare function toPascalCase(str: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Convert a string to camelCase.
|
|
11
|
+
*/
|
|
12
|
+
declare function toCamelCase(str: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Convert a string to kebab-case.
|
|
15
|
+
*/
|
|
16
|
+
declare function toKebabCase(str: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Convert a string to snake_case.
|
|
19
|
+
*/
|
|
20
|
+
declare function toSnakeCase(str: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Sanitize a string to be a valid TypeScript identifier.
|
|
23
|
+
*/
|
|
24
|
+
declare function toValidIdentifier(str: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Generate a ContractSpec name from an operation identifier.
|
|
27
|
+
*/
|
|
28
|
+
declare function toSpecName(operationId: string, prefix?: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* Generate a file name from a spec name.
|
|
31
|
+
*/
|
|
32
|
+
declare function toFileName(specName: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Deep equality check for objects.
|
|
35
|
+
*/
|
|
36
|
+
declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Get a value from an object by JSON path.
|
|
39
|
+
*/
|
|
40
|
+
declare function getByPath(obj: unknown, path: string): unknown;
|
|
41
|
+
/**
|
|
42
|
+
* Extract path parameters from a URL path template.
|
|
43
|
+
* e.g., "/users/{userId}/orders/{orderId}" -> ["userId", "orderId"]
|
|
44
|
+
*/
|
|
45
|
+
declare function extractPathParams(path: string): string[];
|
|
46
|
+
/**
|
|
47
|
+
* Normalize a URL path for comparison.
|
|
48
|
+
*/
|
|
49
|
+
declare function normalizePath(path: string): string;
|
|
50
|
+
//#endregion
|
|
51
|
+
export { deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DiffChange, DiffChangeType, ImportResult, ImportedSpec, SpecDiff, SpecSource, SyncResult, TransportHints, ValidationResult } from "./common/types.js";
|
|
2
|
+
import { deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier } from "./common/utils.js";
|
|
3
|
+
import { ContractSpecOpenApiDocument, HttpMethod, OpenApiDocument, OpenApiExportOptions, OpenApiImportOptions, OpenApiOperation, OpenApiParameter, OpenApiParseOptions, OpenApiSchema, OpenApiServer, OpenApiSource, OpenApiTransportHints, OpenApiVersion, ParameterLocation, ParseResult, ParsedOperation, ParsedParameter } from "./openapi/types.js";
|
|
4
|
+
import { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString } from "./openapi/parser.js";
|
|
5
|
+
import { defaultRestPath, openApiForRegistry, openApiToJson, openApiToYaml } from "./openapi/exporter.js";
|
|
6
|
+
import { importFromOpenApi, importOperation } from "./openapi/importer.js";
|
|
7
|
+
import { GeneratedModel, SchemaField, TypescriptType, generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType } from "./openapi/schema-converter.js";
|
|
8
|
+
import { DiffOptions, createSpecDiff, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges } from "./openapi/differ.js";
|
|
9
|
+
export { ContractSpecOpenApiDocument, DiffChange, DiffChangeType, DiffOptions, GeneratedModel, HttpMethod, ImportResult, ImportedSpec, OpenApiDocument, OpenApiExportOptions, OpenApiImportOptions, OpenApiOperation, OpenApiParameter, OpenApiParseOptions, OpenApiSchema, OpenApiServer, OpenApiSource, OpenApiTransportHints, OpenApiVersion, ParameterLocation, ParseResult, ParsedOperation, ParsedParameter, SchemaField, SpecDiff, SpecSource, SyncResult, TransportHints, TypescriptType, ValidationResult, createSpecDiff, deepEqual, defaultRestPath, detectFormat, detectVersion, diffAll, diffSpecVsOperation, diffSpecs, extractPathParams, formatDiffChanges, generateImports, generateSchemaModelCode, getByPath, getScalarType, importFromOpenApi, importOperation, jsonSchemaToField, jsonSchemaToType, normalizePath, openApiForRegistry, openApiToJson, openApiToYaml, parseOpenApi, parseOpenApiDocument, parseOpenApiString, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DiffChange, ImportedSpec, SpecDiff } from "../common/types.js";
|
|
2
|
+
import { ParsedOperation } from "./types.js";
|
|
3
|
+
import { AnyContractSpec } from "@lssm/lib.contracts";
|
|
4
|
+
|
|
5
|
+
//#region src/openapi/differ.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for diffing specs.
|
|
9
|
+
*/
|
|
10
|
+
interface DiffOptions {
|
|
11
|
+
/** Ignore description changes */
|
|
12
|
+
ignoreDescriptions?: boolean;
|
|
13
|
+
/** Ignore tag changes */
|
|
14
|
+
ignoreTags?: boolean;
|
|
15
|
+
/** Ignore transport changes (path, method) */
|
|
16
|
+
ignoreTransport?: boolean;
|
|
17
|
+
/** Custom paths to ignore */
|
|
18
|
+
ignorePaths?: string[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Diff a ContractSpec against an OpenAPI operation.
|
|
22
|
+
*/
|
|
23
|
+
declare function diffSpecVsOperation(spec: AnyContractSpec, operation: ParsedOperation, options?: DiffOptions): DiffChange[];
|
|
24
|
+
/**
|
|
25
|
+
* Diff two ContractSpecs.
|
|
26
|
+
*/
|
|
27
|
+
declare function diffSpecs(oldSpec: AnyContractSpec, newSpec: AnyContractSpec, options?: DiffOptions): DiffChange[];
|
|
28
|
+
/**
|
|
29
|
+
* Create a SpecDiff from an existing spec and an imported spec.
|
|
30
|
+
*/
|
|
31
|
+
declare function createSpecDiff(operationId: string, existing: AnyContractSpec | undefined, incoming: ImportedSpec, options?: DiffOptions): SpecDiff;
|
|
32
|
+
/**
|
|
33
|
+
* Batch diff multiple specs against OpenAPI operations.
|
|
34
|
+
*/
|
|
35
|
+
declare function diffAll(existingSpecs: Map<string, AnyContractSpec>, importedSpecs: ImportedSpec[], options?: DiffOptions): SpecDiff[];
|
|
36
|
+
/**
|
|
37
|
+
* Format diff changes for display.
|
|
38
|
+
*/
|
|
39
|
+
declare function formatDiffChanges(changes: DiffChange[]): string;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { DiffOptions, createSpecDiff, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ContractSpecOpenApiDocument, OpenApiExportOptions, OpenApiServer } from "./types.js";
|
|
2
|
+
import { SpecRegistry } from "@lssm/lib.contracts";
|
|
3
|
+
|
|
4
|
+
//#region src/openapi/exporter.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generate default REST path from spec name and version.
|
|
8
|
+
*/
|
|
9
|
+
declare function defaultRestPath(name: string, version: number): string;
|
|
10
|
+
/**
|
|
11
|
+
* Export a SpecRegistry to an OpenAPI 3.1 document.
|
|
12
|
+
*
|
|
13
|
+
* @param registry - The SpecRegistry containing specs to export
|
|
14
|
+
* @param options - Export options (title, version, description, servers)
|
|
15
|
+
* @returns OpenAPI 3.1 document
|
|
16
|
+
*/
|
|
17
|
+
declare function openApiForRegistry(registry: SpecRegistry, options?: OpenApiExportOptions): ContractSpecOpenApiDocument;
|
|
18
|
+
/**
|
|
19
|
+
* Export a SpecRegistry to OpenAPI JSON string.
|
|
20
|
+
*/
|
|
21
|
+
declare function openApiToJson(registry: SpecRegistry, options?: OpenApiExportOptions): string;
|
|
22
|
+
/**
|
|
23
|
+
* Export a SpecRegistry to OpenAPI YAML string.
|
|
24
|
+
*/
|
|
25
|
+
declare function openApiToYaml(registry: SpecRegistry, options?: OpenApiExportOptions): string;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { defaultRestPath, openApiForRegistry, openApiToJson, openApiToYaml };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ImportResult } from "../common/types.js";
|
|
2
|
+
import { OpenApiImportOptions, ParseResult, ParsedOperation } from "./types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/openapi/importer.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Import operations from a parsed OpenAPI document.
|
|
8
|
+
*/
|
|
9
|
+
declare function importFromOpenApi(parseResult: ParseResult, options?: OpenApiImportOptions): ImportResult;
|
|
10
|
+
/**
|
|
11
|
+
* Import a single operation to ContractSpec code.
|
|
12
|
+
*/
|
|
13
|
+
declare function importOperation(operation: ParsedOperation, options?: OpenApiImportOptions): string;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { importFromOpenApi, importOperation };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ContractSpecOpenApiDocument, HttpMethod, OpenApiDocument, OpenApiExportOptions, OpenApiImportOptions, OpenApiOperation, OpenApiParameter, OpenApiParseOptions, OpenApiSchema, OpenApiServer, OpenApiSource, OpenApiTransportHints, OpenApiVersion, ParameterLocation, ParseResult, ParsedOperation, ParsedParameter } from "./types.js";
|
|
2
|
+
import { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString } from "./parser.js";
|
|
3
|
+
import { defaultRestPath, openApiForRegistry, openApiToJson, openApiToYaml } from "./exporter.js";
|
|
4
|
+
import { importFromOpenApi, importOperation } from "./importer.js";
|
|
5
|
+
import { GeneratedModel, SchemaField, TypescriptType, generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType } from "./schema-converter.js";
|
|
6
|
+
import { DiffOptions, createSpecDiff, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges } from "./differ.js";
|
|
7
|
+
export { type ContractSpecOpenApiDocument, type DiffOptions, type GeneratedModel, type HttpMethod, type OpenApiDocument, type OpenApiExportOptions, type OpenApiImportOptions, type OpenApiOperation, type OpenApiParameter, type OpenApiParseOptions, type OpenApiSchema, type OpenApiServer, type OpenApiSource, type OpenApiTransportHints, type OpenApiVersion, type ParameterLocation, type ParseResult, type ParsedOperation, type ParsedParameter, type SchemaField, type TypescriptType, createSpecDiff, defaultRestPath, detectFormat, detectVersion, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges, generateImports, generateSchemaModelCode, getScalarType, importFromOpenApi, importOperation, jsonSchemaToField, jsonSchemaToType, openApiForRegistry, openApiToJson, openApiToYaml, parseOpenApi, parseOpenApiDocument, parseOpenApiString };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { OpenApiDocument, OpenApiParseOptions, OpenApiVersion, ParseResult } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/openapi/parser.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parse an OpenAPI document from a string (JSON or YAML).
|
|
7
|
+
*/
|
|
8
|
+
declare function parseOpenApiString(content: string, format?: 'json' | 'yaml'): OpenApiDocument;
|
|
9
|
+
/**
|
|
10
|
+
* Detect the format of content (JSON or YAML).
|
|
11
|
+
*/
|
|
12
|
+
declare function detectFormat(content: string): 'json' | 'yaml';
|
|
13
|
+
/**
|
|
14
|
+
* Detect OpenAPI version from document.
|
|
15
|
+
*/
|
|
16
|
+
declare function detectVersion(doc: OpenApiDocument): OpenApiVersion;
|
|
17
|
+
/**
|
|
18
|
+
* Parse an OpenAPI document into a structured result.
|
|
19
|
+
*/
|
|
20
|
+
declare function parseOpenApiDocument(doc: OpenApiDocument, _options?: OpenApiParseOptions): ParseResult;
|
|
21
|
+
/**
|
|
22
|
+
* Parse OpenAPI from a file path or URL.
|
|
23
|
+
* Note: This is an async function that requires I/O adapters.
|
|
24
|
+
* For pure parsing, use parseOpenApiString or parseOpenApiDocument.
|
|
25
|
+
*/
|
|
26
|
+
declare function parseOpenApi(source: string, options?: OpenApiParseOptions & {
|
|
27
|
+
fetch?: typeof globalThis.fetch;
|
|
28
|
+
readFile?: (path: string) => Promise<string>;
|
|
29
|
+
}): Promise<ParseResult>;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { OpenApiSchema } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/openapi/schema-converter.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* TypeScript type representation for code generation.
|
|
7
|
+
*/
|
|
8
|
+
interface TypescriptType {
|
|
9
|
+
/** The type expression (e.g., "string", "number", "MyModel") */
|
|
10
|
+
type: string;
|
|
11
|
+
/** Whether the type is optional */
|
|
12
|
+
optional: boolean;
|
|
13
|
+
/** Whether the type is an array */
|
|
14
|
+
array: boolean;
|
|
15
|
+
/** Whether this is a primitive type */
|
|
16
|
+
primitive: boolean;
|
|
17
|
+
/** Description for documentation */
|
|
18
|
+
description?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* SchemaModel field representation for code generation.
|
|
22
|
+
*/
|
|
23
|
+
interface SchemaField {
|
|
24
|
+
/** Field name */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Field type */
|
|
27
|
+
type: TypescriptType;
|
|
28
|
+
/** Scalar type enum value (for FieldType) */
|
|
29
|
+
scalarType?: string;
|
|
30
|
+
/** Enum values if this is an enum type */
|
|
31
|
+
enumValues?: string[];
|
|
32
|
+
/** Nested model if this is an object type */
|
|
33
|
+
nestedModel?: GeneratedModel;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Generated model representation.
|
|
37
|
+
*/
|
|
38
|
+
interface GeneratedModel {
|
|
39
|
+
/** Model name (PascalCase) */
|
|
40
|
+
name: string;
|
|
41
|
+
/** Model description */
|
|
42
|
+
description?: string;
|
|
43
|
+
/** Fields */
|
|
44
|
+
fields: SchemaField[];
|
|
45
|
+
/** Generated TypeScript code */
|
|
46
|
+
code: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Convert a JSON Schema to a TypeScript type representation.
|
|
50
|
+
*/
|
|
51
|
+
declare function jsonSchemaToType(schema: OpenApiSchema, name?: string): TypescriptType;
|
|
52
|
+
/**
|
|
53
|
+
* Get the ScalarTypeEnum value for a JSON Schema type.
|
|
54
|
+
*/
|
|
55
|
+
declare function getScalarType(schema: OpenApiSchema): string | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Convert a JSON Schema to a SchemaModel field definition.
|
|
58
|
+
*/
|
|
59
|
+
declare function jsonSchemaToField(schema: OpenApiSchema, fieldName: string, required: boolean): SchemaField;
|
|
60
|
+
/**
|
|
61
|
+
* Generate SchemaModel TypeScript code for a JSON Schema object.
|
|
62
|
+
*/
|
|
63
|
+
declare function generateSchemaModelCode(schema: OpenApiSchema, modelName: string, indent?: number): GeneratedModel;
|
|
64
|
+
/**
|
|
65
|
+
* Generate import statements for a SchemaModel.
|
|
66
|
+
*/
|
|
67
|
+
declare function generateImports(fields: SchemaField[]): string;
|
|
68
|
+
//#endregion
|
|
69
|
+
export { GeneratedModel, SchemaField, TypescriptType, generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType };
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { SpecSource, TransportHints } from "../common/types.js";
|
|
2
|
+
import { OpenAPIV3, OpenAPIV3_1 } from "openapi-types";
|
|
3
|
+
|
|
4
|
+
//#region src/openapi/types.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Supported OpenAPI versions.
|
|
8
|
+
*/
|
|
9
|
+
type OpenApiVersion = '3.0' | '3.1';
|
|
10
|
+
/**
|
|
11
|
+
* OpenAPI document (union of supported versions).
|
|
12
|
+
*/
|
|
13
|
+
type OpenApiDocument = OpenAPIV3.Document | OpenAPIV3_1.Document;
|
|
14
|
+
/**
|
|
15
|
+
* OpenAPI operation object.
|
|
16
|
+
*/
|
|
17
|
+
type OpenApiOperation = OpenAPIV3.OperationObject | OpenAPIV3_1.OperationObject;
|
|
18
|
+
/**
|
|
19
|
+
* OpenAPI schema object.
|
|
20
|
+
*/
|
|
21
|
+
type OpenApiSchema = OpenAPIV3.SchemaObject | OpenAPIV3_1.SchemaObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
|
|
22
|
+
/**
|
|
23
|
+
* OpenAPI parameter object.
|
|
24
|
+
*/
|
|
25
|
+
type OpenApiParameter = OpenAPIV3.ParameterObject | OpenAPIV3_1.ParameterObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
|
|
26
|
+
/**
|
|
27
|
+
* HTTP methods supported by OpenAPI.
|
|
28
|
+
*/
|
|
29
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace';
|
|
30
|
+
/**
|
|
31
|
+
* Parameter location in OpenAPI.
|
|
32
|
+
*/
|
|
33
|
+
type ParameterLocation = 'path' | 'query' | 'header' | 'cookie';
|
|
34
|
+
/**
|
|
35
|
+
* Server configuration for OpenAPI export.
|
|
36
|
+
*/
|
|
37
|
+
interface OpenApiServer {
|
|
38
|
+
url: string;
|
|
39
|
+
description?: string;
|
|
40
|
+
variables?: Record<string, {
|
|
41
|
+
default: string;
|
|
42
|
+
enum?: string[];
|
|
43
|
+
description?: string;
|
|
44
|
+
}>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Options for exporting to OpenAPI.
|
|
48
|
+
*/
|
|
49
|
+
interface OpenApiExportOptions {
|
|
50
|
+
/** API title */
|
|
51
|
+
title?: string;
|
|
52
|
+
/** API version */
|
|
53
|
+
version?: string;
|
|
54
|
+
/** API description */
|
|
55
|
+
description?: string;
|
|
56
|
+
/** Server configurations */
|
|
57
|
+
servers?: OpenApiServer[];
|
|
58
|
+
/** Additional OpenAPI extensions */
|
|
59
|
+
extensions?: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Options for importing from OpenAPI.
|
|
63
|
+
*/
|
|
64
|
+
interface OpenApiImportOptions {
|
|
65
|
+
/** Prefix for generated spec names */
|
|
66
|
+
prefix?: string;
|
|
67
|
+
/** Only import operations with these tags */
|
|
68
|
+
tags?: string[];
|
|
69
|
+
/** Exclude operations with these operationIds */
|
|
70
|
+
exclude?: string[];
|
|
71
|
+
/** Include operations with these operationIds (overrides exclude) */
|
|
72
|
+
include?: string[];
|
|
73
|
+
/** Default stability for imported specs */
|
|
74
|
+
defaultStability?: 'experimental' | 'beta' | 'stable' | 'deprecated';
|
|
75
|
+
/** Default owners for imported specs */
|
|
76
|
+
defaultOwners?: string[];
|
|
77
|
+
/** Default auth level for imported specs */
|
|
78
|
+
defaultAuth?: 'anonymous' | 'user' | 'admin';
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Options for parsing OpenAPI documents.
|
|
82
|
+
*/
|
|
83
|
+
interface OpenApiParseOptions {
|
|
84
|
+
/** Base URL for resolving relative references */
|
|
85
|
+
baseUrl?: string;
|
|
86
|
+
/** Whether to resolve $ref references */
|
|
87
|
+
resolveRefs?: boolean;
|
|
88
|
+
/** Timeout for HTTP requests (ms) */
|
|
89
|
+
timeout?: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Parsed operation with resolved metadata.
|
|
93
|
+
*/
|
|
94
|
+
interface ParsedOperation {
|
|
95
|
+
/** Operation ID (generated if not present) */
|
|
96
|
+
operationId: string;
|
|
97
|
+
/** HTTP method */
|
|
98
|
+
method: HttpMethod;
|
|
99
|
+
/** Path template */
|
|
100
|
+
path: string;
|
|
101
|
+
/** Operation summary */
|
|
102
|
+
summary?: string;
|
|
103
|
+
/** Operation description */
|
|
104
|
+
description?: string;
|
|
105
|
+
/** Operation tags */
|
|
106
|
+
tags: string[];
|
|
107
|
+
/** Path parameters */
|
|
108
|
+
pathParams: ParsedParameter[];
|
|
109
|
+
/** Query parameters */
|
|
110
|
+
queryParams: ParsedParameter[];
|
|
111
|
+
/** Header parameters */
|
|
112
|
+
headerParams: ParsedParameter[];
|
|
113
|
+
/** Cookie parameters */
|
|
114
|
+
cookieParams: ParsedParameter[];
|
|
115
|
+
/** Request body schema */
|
|
116
|
+
requestBody?: {
|
|
117
|
+
required: boolean;
|
|
118
|
+
schema: OpenApiSchema;
|
|
119
|
+
contentType: string;
|
|
120
|
+
};
|
|
121
|
+
/** Response schemas by status code */
|
|
122
|
+
responses: Record<string, {
|
|
123
|
+
description: string;
|
|
124
|
+
schema?: OpenApiSchema;
|
|
125
|
+
contentType?: string;
|
|
126
|
+
}>;
|
|
127
|
+
/** Whether the operation is deprecated */
|
|
128
|
+
deprecated: boolean;
|
|
129
|
+
/** Security requirements */
|
|
130
|
+
security?: Array<Record<string, string[]>>;
|
|
131
|
+
/** ContractSpec extension data if present */
|
|
132
|
+
contractSpecMeta?: {
|
|
133
|
+
name: string;
|
|
134
|
+
version: number;
|
|
135
|
+
kind: 'command' | 'query';
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Parsed parameter with resolved schema.
|
|
140
|
+
*/
|
|
141
|
+
interface ParsedParameter {
|
|
142
|
+
/** Parameter name */
|
|
143
|
+
name: string;
|
|
144
|
+
/** Parameter location */
|
|
145
|
+
in: ParameterLocation;
|
|
146
|
+
/** Whether the parameter is required */
|
|
147
|
+
required: boolean;
|
|
148
|
+
/** Parameter description */
|
|
149
|
+
description?: string;
|
|
150
|
+
/** Parameter schema */
|
|
151
|
+
schema: OpenApiSchema;
|
|
152
|
+
/** Whether the parameter is deprecated */
|
|
153
|
+
deprecated: boolean;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Result of parsing an OpenAPI document.
|
|
157
|
+
*/
|
|
158
|
+
interface ParseResult {
|
|
159
|
+
/** Original document */
|
|
160
|
+
document: OpenApiDocument;
|
|
161
|
+
/** Detected OpenAPI version */
|
|
162
|
+
version: OpenApiVersion;
|
|
163
|
+
/** API info */
|
|
164
|
+
info: {
|
|
165
|
+
title: string;
|
|
166
|
+
version: string;
|
|
167
|
+
description?: string;
|
|
168
|
+
};
|
|
169
|
+
/** Parsed operations */
|
|
170
|
+
operations: ParsedOperation[];
|
|
171
|
+
/** Component schemas (resolved) */
|
|
172
|
+
schemas: Record<string, OpenApiSchema>;
|
|
173
|
+
/** Servers */
|
|
174
|
+
servers: OpenApiServer[];
|
|
175
|
+
/** Parse warnings */
|
|
176
|
+
warnings: string[];
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* OpenAPI-specific transport hints.
|
|
180
|
+
*/
|
|
181
|
+
interface OpenApiTransportHints extends TransportHints {
|
|
182
|
+
rest: {
|
|
183
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
184
|
+
path: string;
|
|
185
|
+
params?: {
|
|
186
|
+
path?: string[];
|
|
187
|
+
query?: string[];
|
|
188
|
+
header?: string[];
|
|
189
|
+
cookie?: string[];
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* OpenAPI-specific source information.
|
|
195
|
+
*/
|
|
196
|
+
interface OpenApiSource extends SpecSource {
|
|
197
|
+
type: 'openapi';
|
|
198
|
+
/** OpenAPI version of source document */
|
|
199
|
+
openApiVersion: OpenApiVersion;
|
|
200
|
+
/** Original operationId from OpenAPI */
|
|
201
|
+
operationId: string;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* ContractSpec-compatible OpenAPI document structure.
|
|
205
|
+
* Used for export to maintain compatibility with existing code.
|
|
206
|
+
*/
|
|
207
|
+
interface ContractSpecOpenApiDocument {
|
|
208
|
+
openapi: '3.1.0';
|
|
209
|
+
info: {
|
|
210
|
+
title: string;
|
|
211
|
+
version: string;
|
|
212
|
+
description?: string;
|
|
213
|
+
};
|
|
214
|
+
servers?: OpenApiServer[];
|
|
215
|
+
paths: Record<string, Record<string, unknown>>;
|
|
216
|
+
components: {
|
|
217
|
+
schemas: Record<string, Record<string, unknown>>;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
//#endregion
|
|
221
|
+
export { ContractSpecOpenApiDocument, HttpMethod, OpenApiDocument, OpenApiExportOptions, OpenApiImportOptions, OpenApiOperation, OpenApiParameter, OpenApiParseOptions, OpenApiSchema, OpenApiServer, OpenApiSource, OpenApiTransportHints, OpenApiVersion, ParameterLocation, ParseResult, ParsedOperation, ParsedParameter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lssm/lib.contracts-transformers",
|
|
3
|
-
"version": "0.0.0-canary-
|
|
3
|
+
"version": "0.0.0-canary-20251217060433",
|
|
4
4
|
"description": "Contract format transformations: import/export between ContractSpec and external formats (OpenAPI, AsyncAPI, etc.)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -17,15 +17,15 @@
|
|
|
17
17
|
"test": "bun test"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@lssm/lib.contracts": "0.0.0-canary-
|
|
21
|
-
"@lssm/lib.schema": "0.0.0-canary-
|
|
20
|
+
"@lssm/lib.contracts": "0.0.0-canary-20251217060433",
|
|
21
|
+
"@lssm/lib.schema": "0.0.0-canary-20251217060433",
|
|
22
22
|
"openapi-types": "^12.1.3",
|
|
23
23
|
"yaml": "^2.7.1",
|
|
24
24
|
"zod": "^4.1.13"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@lssm/tool.tsdown": "0.0.0-canary-
|
|
28
|
-
"@lssm/tool.typescript": "0.0.0-canary-
|
|
27
|
+
"@lssm/tool.tsdown": "0.0.0-canary-20251217060433",
|
|
28
|
+
"@lssm/tool.typescript": "0.0.0-canary-20251217060433",
|
|
29
29
|
"tsdown": "^0.17.4",
|
|
30
30
|
"typescript": "^5.9.3"
|
|
31
31
|
},
|
|
@@ -37,9 +37,9 @@
|
|
|
37
37
|
],
|
|
38
38
|
"module": "./dist/index.js",
|
|
39
39
|
"exports": {
|
|
40
|
-
".": "./
|
|
41
|
-
"./common": "./
|
|
42
|
-
"./openapi": "./
|
|
40
|
+
".": "./dist/index.js",
|
|
41
|
+
"./common": "./dist/common/index.js",
|
|
42
|
+
"./openapi": "./dist/openapi/index.js",
|
|
43
43
|
"./*": "./*"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|