@lssm/lib.contracts-transformers 0.0.0-canary-20251217063201 → 0.0.0-canary-20251217073102
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.js +3 -1
- package/dist/common/utils.js +102 -1
- package/dist/index.js +9 -1
- package/dist/openapi/differ.js +214 -2
- package/dist/openapi/exporter.js +150 -1
- package/dist/openapi/importer.js +264 -2
- package/dist/openapi/index.js +7 -1
- package/dist/openapi/parser.js +231 -1
- package/dist/openapi/schema-converter.js +201 -4
- package/package.json +5 -5
package/dist/common/index.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier } from "./utils.js";
|
|
2
|
+
|
|
3
|
+
export { deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier };
|
package/dist/common/utils.js
CHANGED
|
@@ -1 +1,102 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/common/utils.ts
|
|
2
|
+
/**
|
|
3
|
+
* Common utilities for contract transformations.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Convert a string to PascalCase.
|
|
7
|
+
*/
|
|
8
|
+
function toPascalCase(str) {
|
|
9
|
+
return str.replace(/[-_./\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : "").replace(/^./, (c) => c.toUpperCase());
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Convert a string to camelCase.
|
|
13
|
+
*/
|
|
14
|
+
function toCamelCase(str) {
|
|
15
|
+
const pascal = toPascalCase(str);
|
|
16
|
+
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convert a string to kebab-case.
|
|
20
|
+
*/
|
|
21
|
+
function toKebabCase(str) {
|
|
22
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_./]+/g, "-").toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert a string to snake_case.
|
|
26
|
+
*/
|
|
27
|
+
function toSnakeCase(str) {
|
|
28
|
+
return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[\s\-./]+/g, "_").toLowerCase();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Sanitize a string to be a valid TypeScript identifier.
|
|
32
|
+
*/
|
|
33
|
+
function toValidIdentifier(str) {
|
|
34
|
+
let result = str.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
35
|
+
if (/^[0-9]/.test(result)) result = "_" + result;
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Generate a ContractSpec name from an operation identifier.
|
|
40
|
+
*/
|
|
41
|
+
function toSpecName(operationId, prefix) {
|
|
42
|
+
const name = toCamelCase(operationId);
|
|
43
|
+
return prefix ? `${prefix}.${name}` : name;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Generate a file name from a spec name.
|
|
47
|
+
*/
|
|
48
|
+
function toFileName(specName) {
|
|
49
|
+
return toKebabCase(specName.replace(/\./g, "-")) + ".ts";
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Deep equality check for objects.
|
|
53
|
+
*/
|
|
54
|
+
function deepEqual(a, b) {
|
|
55
|
+
if (a === b) return true;
|
|
56
|
+
if (a === null || b === null) return false;
|
|
57
|
+
if (typeof a !== typeof b) return false;
|
|
58
|
+
if (typeof a === "object") {
|
|
59
|
+
const aObj = a;
|
|
60
|
+
const bObj = b;
|
|
61
|
+
const aKeys = Object.keys(aObj);
|
|
62
|
+
const bKeys = Object.keys(bObj);
|
|
63
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
64
|
+
for (const key of aKeys) {
|
|
65
|
+
if (!bKeys.includes(key)) return false;
|
|
66
|
+
if (!deepEqual(aObj[key], bObj[key])) return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get a value from an object by JSON path.
|
|
74
|
+
*/
|
|
75
|
+
function getByPath(obj, path) {
|
|
76
|
+
const parts = path.split(".").filter(Boolean);
|
|
77
|
+
let current = obj;
|
|
78
|
+
for (const part of parts) {
|
|
79
|
+
if (current === null || current === void 0) return void 0;
|
|
80
|
+
if (typeof current !== "object") return void 0;
|
|
81
|
+
current = current[part];
|
|
82
|
+
}
|
|
83
|
+
return current;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Extract path parameters from a URL path template.
|
|
87
|
+
* e.g., "/users/{userId}/orders/{orderId}" -> ["userId", "orderId"]
|
|
88
|
+
*/
|
|
89
|
+
function extractPathParams(path) {
|
|
90
|
+
return (path.match(/\{([^}]+)\}/g) || []).map((m) => m.slice(1, -1));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Normalize a URL path for comparison.
|
|
94
|
+
*/
|
|
95
|
+
function normalizePath(path) {
|
|
96
|
+
let normalized = path.replace(/^\/+|\/+$/g, "");
|
|
97
|
+
normalized = normalized.replace(/\/+/g, "/");
|
|
98
|
+
return "/" + normalized;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
export { deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier };
|
package/dist/index.js
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString } from "./openapi/parser.js";
|
|
2
|
+
import { defaultRestPath, openApiForRegistry, openApiToJson, openApiToYaml } from "./openapi/exporter.js";
|
|
3
|
+
import { deepEqual, extractPathParams, getByPath, normalizePath, toCamelCase, toFileName, toKebabCase, toPascalCase, toSnakeCase, toSpecName, toValidIdentifier } from "./common/utils.js";
|
|
4
|
+
import { generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType } from "./openapi/schema-converter.js";
|
|
5
|
+
import { importFromOpenApi, importOperation } from "./openapi/importer.js";
|
|
6
|
+
import { createSpecDiff, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges } from "./openapi/differ.js";
|
|
7
|
+
import "./openapi/index.js";
|
|
8
|
+
|
|
9
|
+
export { 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 };
|
package/dist/openapi/differ.js
CHANGED
|
@@ -1,2 +1,214 @@
|
|
|
1
|
-
import{deepEqual
|
|
2
|
-
|
|
1
|
+
import { deepEqual } from "../common/utils.js";
|
|
2
|
+
|
|
3
|
+
//#region src/openapi/differ.ts
|
|
4
|
+
/**
|
|
5
|
+
* Compare two values and generate a diff change if different.
|
|
6
|
+
*/
|
|
7
|
+
function compareValues(path, oldValue, newValue, description) {
|
|
8
|
+
if (deepEqual(oldValue, newValue)) return null;
|
|
9
|
+
let changeType = "modified";
|
|
10
|
+
if (oldValue === void 0 || oldValue === null) changeType = "added";
|
|
11
|
+
else if (newValue === void 0 || newValue === null) changeType = "removed";
|
|
12
|
+
else if (typeof oldValue !== typeof newValue) changeType = "type_changed";
|
|
13
|
+
return {
|
|
14
|
+
path,
|
|
15
|
+
type: changeType,
|
|
16
|
+
oldValue,
|
|
17
|
+
newValue,
|
|
18
|
+
description
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Diff two objects recursively.
|
|
23
|
+
*/
|
|
24
|
+
function diffObjects(path, oldObj, newObj, options) {
|
|
25
|
+
const changes = [];
|
|
26
|
+
if (!oldObj && !newObj) return changes;
|
|
27
|
+
if (!oldObj) {
|
|
28
|
+
changes.push({
|
|
29
|
+
path,
|
|
30
|
+
type: "added",
|
|
31
|
+
newValue: newObj,
|
|
32
|
+
description: `Added ${path}`
|
|
33
|
+
});
|
|
34
|
+
return changes;
|
|
35
|
+
}
|
|
36
|
+
if (!newObj) {
|
|
37
|
+
changes.push({
|
|
38
|
+
path,
|
|
39
|
+
type: "removed",
|
|
40
|
+
oldValue: oldObj,
|
|
41
|
+
description: `Removed ${path}`
|
|
42
|
+
});
|
|
43
|
+
return changes;
|
|
44
|
+
}
|
|
45
|
+
const allKeys = new Set([...Object.keys(oldObj), ...Object.keys(newObj)]);
|
|
46
|
+
for (const key of allKeys) {
|
|
47
|
+
const keyPath = path ? `${path}.${key}` : key;
|
|
48
|
+
if (options.ignorePaths?.some((p) => keyPath.startsWith(p))) continue;
|
|
49
|
+
const oldVal = oldObj[key];
|
|
50
|
+
const newVal = newObj[key];
|
|
51
|
+
if (typeof oldVal === "object" && typeof newVal === "object") changes.push(...diffObjects(keyPath, oldVal, newVal, options));
|
|
52
|
+
else {
|
|
53
|
+
const change = compareValues(keyPath, oldVal, newVal, `Changed ${keyPath}`);
|
|
54
|
+
if (change) changes.push(change);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return changes;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Diff a ContractSpec against an OpenAPI operation.
|
|
61
|
+
*/
|
|
62
|
+
function diffSpecVsOperation(spec, operation, options = {}) {
|
|
63
|
+
const changes = [];
|
|
64
|
+
if (!options.ignoreDescriptions) {
|
|
65
|
+
const descChange = compareValues("meta.description", spec.meta.description, operation.summary ?? operation.description, "Description changed");
|
|
66
|
+
if (descChange) changes.push(descChange);
|
|
67
|
+
}
|
|
68
|
+
if (!options.ignoreTags) {
|
|
69
|
+
const oldTags = [...spec.meta.tags ?? []].sort();
|
|
70
|
+
const newTags = [...operation.tags].sort();
|
|
71
|
+
if (!deepEqual(oldTags, newTags)) changes.push({
|
|
72
|
+
path: "meta.tags",
|
|
73
|
+
type: "modified",
|
|
74
|
+
oldValue: oldTags,
|
|
75
|
+
newValue: newTags,
|
|
76
|
+
description: "Tags changed"
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (!options.ignoreTransport) {
|
|
80
|
+
const specMethod = spec.transport?.rest?.method ?? (spec.meta.kind === "query" ? "GET" : "POST");
|
|
81
|
+
const opMethod = operation.method.toUpperCase();
|
|
82
|
+
if (specMethod !== opMethod) changes.push({
|
|
83
|
+
path: "transport.rest.method",
|
|
84
|
+
type: "modified",
|
|
85
|
+
oldValue: specMethod,
|
|
86
|
+
newValue: opMethod,
|
|
87
|
+
description: "HTTP method changed"
|
|
88
|
+
});
|
|
89
|
+
const specPath = spec.transport?.rest?.path;
|
|
90
|
+
if (specPath && specPath !== operation.path) changes.push({
|
|
91
|
+
path: "transport.rest.path",
|
|
92
|
+
type: "modified",
|
|
93
|
+
oldValue: specPath,
|
|
94
|
+
newValue: operation.path,
|
|
95
|
+
description: "Path changed"
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (spec.meta.stability === "deprecated" !== operation.deprecated) changes.push({
|
|
99
|
+
path: "meta.stability",
|
|
100
|
+
type: "modified",
|
|
101
|
+
oldValue: spec.meta.stability,
|
|
102
|
+
newValue: operation.deprecated ? "deprecated" : "stable",
|
|
103
|
+
description: "Deprecation status changed"
|
|
104
|
+
});
|
|
105
|
+
return changes;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Diff two ContractSpecs.
|
|
109
|
+
*/
|
|
110
|
+
function diffSpecs(oldSpec, newSpec, options = {}) {
|
|
111
|
+
const changes = [];
|
|
112
|
+
const metaChanges = diffObjects("meta", oldSpec.meta, newSpec.meta, {
|
|
113
|
+
...options,
|
|
114
|
+
ignorePaths: [
|
|
115
|
+
...options.ignorePaths ?? [],
|
|
116
|
+
...options.ignoreDescriptions ? [
|
|
117
|
+
"meta.description",
|
|
118
|
+
"meta.goal",
|
|
119
|
+
"meta.context"
|
|
120
|
+
] : [],
|
|
121
|
+
...options.ignoreTags ? ["meta.tags"] : []
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
changes.push(...metaChanges);
|
|
125
|
+
if (!options.ignoreTransport) {
|
|
126
|
+
const transportChanges = diffObjects("transport", oldSpec.transport, newSpec.transport, options);
|
|
127
|
+
changes.push(...transportChanges);
|
|
128
|
+
}
|
|
129
|
+
const policyChanges = diffObjects("policy", oldSpec.policy, newSpec.policy, options);
|
|
130
|
+
changes.push(...policyChanges);
|
|
131
|
+
return changes;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Create a SpecDiff from an existing spec and an imported spec.
|
|
135
|
+
*/
|
|
136
|
+
function createSpecDiff(operationId, existing, incoming, options = {}) {
|
|
137
|
+
let changes = [];
|
|
138
|
+
let isEquivalent = false;
|
|
139
|
+
if (existing) {
|
|
140
|
+
changes = diffSpecs(existing, incoming.spec, options);
|
|
141
|
+
isEquivalent = changes.length === 0;
|
|
142
|
+
} else changes = [{
|
|
143
|
+
path: "",
|
|
144
|
+
type: "added",
|
|
145
|
+
newValue: incoming.spec,
|
|
146
|
+
description: "New spec imported from OpenAPI"
|
|
147
|
+
}];
|
|
148
|
+
return {
|
|
149
|
+
operationId,
|
|
150
|
+
existing,
|
|
151
|
+
incoming,
|
|
152
|
+
changes,
|
|
153
|
+
isEquivalent
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Batch diff multiple specs against OpenAPI operations.
|
|
158
|
+
*/
|
|
159
|
+
function diffAll(existingSpecs, importedSpecs, options = {}) {
|
|
160
|
+
const diffs = [];
|
|
161
|
+
const matchedExisting = /* @__PURE__ */ new Set();
|
|
162
|
+
for (const imported of importedSpecs) {
|
|
163
|
+
const operationId = imported.source.sourceId;
|
|
164
|
+
let existing;
|
|
165
|
+
for (const [key, spec] of existingSpecs) {
|
|
166
|
+
const specName = spec.meta.name;
|
|
167
|
+
if (key === operationId || specName.includes(operationId)) {
|
|
168
|
+
existing = spec;
|
|
169
|
+
matchedExisting.add(key);
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
diffs.push(createSpecDiff(operationId, existing, imported, options));
|
|
174
|
+
}
|
|
175
|
+
for (const [key, spec] of existingSpecs) if (!matchedExisting.has(key)) diffs.push({
|
|
176
|
+
operationId: key,
|
|
177
|
+
existing: spec,
|
|
178
|
+
incoming: void 0,
|
|
179
|
+
changes: [{
|
|
180
|
+
path: "",
|
|
181
|
+
type: "removed",
|
|
182
|
+
oldValue: spec,
|
|
183
|
+
description: "Spec no longer exists in OpenAPI source"
|
|
184
|
+
}],
|
|
185
|
+
isEquivalent: false
|
|
186
|
+
});
|
|
187
|
+
return diffs;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Format diff changes for display.
|
|
191
|
+
*/
|
|
192
|
+
function formatDiffChanges(changes) {
|
|
193
|
+
if (changes.length === 0) return "No changes detected";
|
|
194
|
+
const lines = [];
|
|
195
|
+
for (const change of changes) {
|
|
196
|
+
const prefix = {
|
|
197
|
+
added: "+",
|
|
198
|
+
removed: "-",
|
|
199
|
+
modified: "~",
|
|
200
|
+
type_changed: "!",
|
|
201
|
+
required_changed: "?"
|
|
202
|
+
}[change.type];
|
|
203
|
+
lines.push(`${prefix} ${change.path}: ${change.description}`);
|
|
204
|
+
if (change.type === "modified" || change.type === "type_changed") {
|
|
205
|
+
lines.push(` old: ${JSON.stringify(change.oldValue)}`);
|
|
206
|
+
lines.push(` new: ${JSON.stringify(change.newValue)}`);
|
|
207
|
+
} else if (change.type === "added") lines.push(` value: ${JSON.stringify(change.newValue)}`);
|
|
208
|
+
else if (change.type === "removed") lines.push(` was: ${JSON.stringify(change.oldValue)}`);
|
|
209
|
+
}
|
|
210
|
+
return lines.join("\n");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
//#endregion
|
|
214
|
+
export { createSpecDiff, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges };
|
package/dist/openapi/exporter.js
CHANGED
|
@@ -1 +1,150 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/openapi/exporter.ts
|
|
4
|
+
/**
|
|
5
|
+
* Convert a spec name and version to an operationId.
|
|
6
|
+
*/
|
|
7
|
+
function toOperationId(name, version) {
|
|
8
|
+
return `${name.replace(/\./g, "_")}_v${version}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Convert a spec name and version to a schema name.
|
|
12
|
+
*/
|
|
13
|
+
function toSchemaName(prefix, name, version) {
|
|
14
|
+
return `${prefix}_${toOperationId(name, version)}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Determine HTTP method from spec kind and override.
|
|
18
|
+
*/
|
|
19
|
+
function toHttpMethod(kind, override) {
|
|
20
|
+
return (override ?? (kind === "query" ? "GET" : "POST")).toLowerCase();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Generate default REST path from spec name and version.
|
|
24
|
+
*/
|
|
25
|
+
function defaultRestPath(name, version) {
|
|
26
|
+
return `/${name.replace(/\./g, "/")}/v${version}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get REST path from spec, using transport override or default.
|
|
30
|
+
*/
|
|
31
|
+
function toRestPath(spec) {
|
|
32
|
+
const path = spec.transport?.rest?.path ?? defaultRestPath(spec.meta.name, spec.meta.version);
|
|
33
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Convert a SchemaModel to JSON Schema using Zod.
|
|
37
|
+
*/
|
|
38
|
+
function schemaModelToJsonSchema(schema) {
|
|
39
|
+
if (!schema) return null;
|
|
40
|
+
return z.toJSONSchema(schema.getZod());
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Extract JSON Schema for a spec's input and output.
|
|
44
|
+
*/
|
|
45
|
+
function jsonSchemaForSpec(spec) {
|
|
46
|
+
return {
|
|
47
|
+
input: schemaModelToJsonSchema(spec.io.input),
|
|
48
|
+
output: schemaModelToJsonSchema(spec.io.output),
|
|
49
|
+
meta: {
|
|
50
|
+
name: spec.meta.name,
|
|
51
|
+
version: spec.meta.version,
|
|
52
|
+
kind: spec.meta.kind,
|
|
53
|
+
description: spec.meta.description,
|
|
54
|
+
tags: spec.meta.tags ?? [],
|
|
55
|
+
stability: spec.meta.stability ?? "stable"
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Export a SpecRegistry to an OpenAPI 3.1 document.
|
|
61
|
+
*
|
|
62
|
+
* @param registry - The SpecRegistry containing specs to export
|
|
63
|
+
* @param options - Export options (title, version, description, servers)
|
|
64
|
+
* @returns OpenAPI 3.1 document
|
|
65
|
+
*/
|
|
66
|
+
function openApiForRegistry(registry, options = {}) {
|
|
67
|
+
const specs = registry.listSpecs().filter((s) => s.meta.kind === "command" || s.meta.kind === "query").slice().sort((a, b) => {
|
|
68
|
+
const byName = a.meta.name.localeCompare(b.meta.name);
|
|
69
|
+
return byName !== 0 ? byName : a.meta.version - b.meta.version;
|
|
70
|
+
});
|
|
71
|
+
const doc = {
|
|
72
|
+
openapi: "3.1.0",
|
|
73
|
+
info: {
|
|
74
|
+
title: options.title ?? "ContractSpec API",
|
|
75
|
+
version: options.version ?? "0.0.0",
|
|
76
|
+
...options.description ? { description: options.description } : {}
|
|
77
|
+
},
|
|
78
|
+
...options.servers ? { servers: options.servers } : {},
|
|
79
|
+
paths: {},
|
|
80
|
+
components: { schemas: {} }
|
|
81
|
+
};
|
|
82
|
+
for (const spec of specs) {
|
|
83
|
+
const schema = jsonSchemaForSpec(spec);
|
|
84
|
+
const method = toHttpMethod(spec.meta.kind, spec.transport?.rest?.method);
|
|
85
|
+
const path = toRestPath(spec);
|
|
86
|
+
const operationId = toOperationId(spec.meta.name, spec.meta.version);
|
|
87
|
+
const pathItem = doc.paths[path] ??= {};
|
|
88
|
+
const op = {
|
|
89
|
+
operationId,
|
|
90
|
+
summary: spec.meta.description ?? spec.meta.name,
|
|
91
|
+
description: spec.meta.description,
|
|
92
|
+
tags: spec.meta.tags ?? [],
|
|
93
|
+
"x-contractspec": {
|
|
94
|
+
name: spec.meta.name,
|
|
95
|
+
version: spec.meta.version,
|
|
96
|
+
kind: spec.meta.kind
|
|
97
|
+
},
|
|
98
|
+
responses: {}
|
|
99
|
+
};
|
|
100
|
+
if (schema.input) {
|
|
101
|
+
const inputName = toSchemaName("Input", spec.meta.name, spec.meta.version);
|
|
102
|
+
doc.components.schemas[inputName] = schema.input;
|
|
103
|
+
op["requestBody"] = {
|
|
104
|
+
required: true,
|
|
105
|
+
content: { "application/json": { schema: { $ref: `#/components/schemas/${inputName}` } } }
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const responses = {};
|
|
109
|
+
if (schema.output) {
|
|
110
|
+
const outputName = toSchemaName("Output", spec.meta.name, spec.meta.version);
|
|
111
|
+
doc.components.schemas[outputName] = schema.output;
|
|
112
|
+
responses["200"] = {
|
|
113
|
+
description: "OK",
|
|
114
|
+
content: { "application/json": { schema: { $ref: `#/components/schemas/${outputName}` } } }
|
|
115
|
+
};
|
|
116
|
+
} else responses["200"] = { description: "OK" };
|
|
117
|
+
op["responses"] = responses;
|
|
118
|
+
pathItem[method] = op;
|
|
119
|
+
}
|
|
120
|
+
return doc;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Export a SpecRegistry to OpenAPI JSON string.
|
|
124
|
+
*/
|
|
125
|
+
function openApiToJson(registry, options = {}) {
|
|
126
|
+
const doc = openApiForRegistry(registry, options);
|
|
127
|
+
return JSON.stringify(doc, null, 2);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Export a SpecRegistry to OpenAPI YAML string.
|
|
131
|
+
*/
|
|
132
|
+
function openApiToYaml(registry, options = {}) {
|
|
133
|
+
return jsonToYaml(openApiForRegistry(registry, options));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Simple JSON to YAML conversion.
|
|
137
|
+
*/
|
|
138
|
+
function jsonToYaml(obj, indent = 0) {
|
|
139
|
+
const spaces = " ".repeat(indent);
|
|
140
|
+
let yaml = "";
|
|
141
|
+
if (Array.isArray(obj)) for (const item of obj) if (typeof item === "object" && item !== null) yaml += `${spaces}-\n${jsonToYaml(item, indent + 1)}`;
|
|
142
|
+
else yaml += `${spaces}- ${JSON.stringify(item)}\n`;
|
|
143
|
+
else if (typeof obj === "object" && obj !== null) for (const [key, value] of Object.entries(obj)) if (Array.isArray(value)) yaml += `${spaces}${key}:\n${jsonToYaml(value, indent + 1)}`;
|
|
144
|
+
else if (typeof value === "object" && value !== null) yaml += `${spaces}${key}:\n${jsonToYaml(value, indent + 1)}`;
|
|
145
|
+
else yaml += `${spaces}${key}: ${JSON.stringify(value)}\n`;
|
|
146
|
+
return yaml;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
export { defaultRestPath, openApiForRegistry, openApiToJson, openApiToYaml };
|
package/dist/openapi/importer.js
CHANGED
|
@@ -1,2 +1,264 @@
|
|
|
1
|
-
import{toFileName
|
|
2
|
-
|
|
1
|
+
import { toFileName, toPascalCase, toSpecName, toValidIdentifier } from "../common/utils.js";
|
|
2
|
+
import { generateImports, generateSchemaModelCode } from "./schema-converter.js";
|
|
3
|
+
|
|
4
|
+
//#region src/openapi/importer.ts
|
|
5
|
+
/**
|
|
6
|
+
* HTTP methods that typically indicate a command (state-changing).
|
|
7
|
+
*/
|
|
8
|
+
const COMMAND_METHODS = [
|
|
9
|
+
"post",
|
|
10
|
+
"put",
|
|
11
|
+
"delete",
|
|
12
|
+
"patch"
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Determine if an operation is a command or query based on HTTP method.
|
|
16
|
+
*/
|
|
17
|
+
function inferOpKind(method) {
|
|
18
|
+
return COMMAND_METHODS.includes(method.toLowerCase()) ? "command" : "query";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Determine auth level based on security requirements.
|
|
22
|
+
*/
|
|
23
|
+
function inferAuthLevel(operation, defaultAuth) {
|
|
24
|
+
if (!operation.security || operation.security.length === 0) return defaultAuth;
|
|
25
|
+
for (const sec of operation.security) if (Object.keys(sec).length === 0) return "anonymous";
|
|
26
|
+
return "user";
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Build a merged input schema from all parameter sources.
|
|
30
|
+
*/
|
|
31
|
+
function buildInputSchema(operation) {
|
|
32
|
+
const fields = [];
|
|
33
|
+
for (const param of operation.pathParams) fields.push({
|
|
34
|
+
name: param.name,
|
|
35
|
+
schema: param.schema,
|
|
36
|
+
required: true
|
|
37
|
+
});
|
|
38
|
+
for (const param of operation.queryParams) fields.push({
|
|
39
|
+
name: param.name,
|
|
40
|
+
schema: param.schema,
|
|
41
|
+
required: param.required
|
|
42
|
+
});
|
|
43
|
+
const excludedHeaders = [
|
|
44
|
+
"authorization",
|
|
45
|
+
"content-type",
|
|
46
|
+
"accept",
|
|
47
|
+
"user-agent"
|
|
48
|
+
];
|
|
49
|
+
for (const param of operation.headerParams) if (!excludedHeaders.includes(param.name.toLowerCase())) fields.push({
|
|
50
|
+
name: param.name,
|
|
51
|
+
schema: param.schema,
|
|
52
|
+
required: param.required
|
|
53
|
+
});
|
|
54
|
+
if (operation.requestBody?.schema) {
|
|
55
|
+
const bodySchema = operation.requestBody.schema;
|
|
56
|
+
if (!("$ref" in bodySchema)) {
|
|
57
|
+
const schemaObj = bodySchema;
|
|
58
|
+
const properties = schemaObj["properties"];
|
|
59
|
+
const required = schemaObj["required"] ?? [];
|
|
60
|
+
if (properties) for (const [propName, propSchema] of Object.entries(properties)) fields.push({
|
|
61
|
+
name: propName,
|
|
62
|
+
schema: propSchema,
|
|
63
|
+
required: required.includes(propName)
|
|
64
|
+
});
|
|
65
|
+
} else fields.push({
|
|
66
|
+
name: "body",
|
|
67
|
+
schema: bodySchema,
|
|
68
|
+
required: operation.requestBody.required
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (fields.length === 0) return {
|
|
72
|
+
schema: null,
|
|
73
|
+
fields: []
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
schema: {
|
|
77
|
+
type: "object",
|
|
78
|
+
properties: fields.reduce((acc, f) => {
|
|
79
|
+
acc[f.name] = f.schema;
|
|
80
|
+
return acc;
|
|
81
|
+
}, {}),
|
|
82
|
+
required: fields.filter((f) => f.required).map((f) => f.name)
|
|
83
|
+
},
|
|
84
|
+
fields
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get the output schema from the operation responses.
|
|
89
|
+
*/
|
|
90
|
+
function getOutputSchema(operation) {
|
|
91
|
+
for (const code of [
|
|
92
|
+
"200",
|
|
93
|
+
"201",
|
|
94
|
+
"202",
|
|
95
|
+
"204"
|
|
96
|
+
]) {
|
|
97
|
+
const response = operation.responses[code];
|
|
98
|
+
if (response?.schema) return response.schema;
|
|
99
|
+
}
|
|
100
|
+
for (const [code, response] of Object.entries(operation.responses)) if (code.startsWith("2") && response.schema) return response.schema;
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Generate ContractSpec TypeScript code for an operation.
|
|
105
|
+
*/
|
|
106
|
+
function generateSpecCode(operation, options, inputModel, outputModel) {
|
|
107
|
+
const specName = toSpecName(operation.operationId, options.prefix);
|
|
108
|
+
const kind = inferOpKind(operation.method);
|
|
109
|
+
const auth = inferAuthLevel(operation, options.defaultAuth ?? "user");
|
|
110
|
+
const lines = [];
|
|
111
|
+
lines.push("import { defineCommand, defineQuery } from '@lssm/lib.contracts';");
|
|
112
|
+
if (inputModel || outputModel) lines.push(generateImports([...inputModel?.fields ?? [], ...outputModel?.fields ?? []]));
|
|
113
|
+
lines.push("");
|
|
114
|
+
if (inputModel && inputModel.code) {
|
|
115
|
+
lines.push("// Input schema");
|
|
116
|
+
lines.push(inputModel.code);
|
|
117
|
+
lines.push("");
|
|
118
|
+
}
|
|
119
|
+
if (outputModel && outputModel.code) {
|
|
120
|
+
lines.push("// Output schema");
|
|
121
|
+
lines.push(outputModel.code);
|
|
122
|
+
lines.push("");
|
|
123
|
+
}
|
|
124
|
+
const defineFunc = kind === "command" ? "defineCommand" : "defineQuery";
|
|
125
|
+
const safeName = toValidIdentifier(toPascalCase(operation.operationId));
|
|
126
|
+
lines.push(`/**`);
|
|
127
|
+
lines.push(` * ${operation.summary ?? operation.operationId}`);
|
|
128
|
+
if (operation.description) {
|
|
129
|
+
lines.push(` *`);
|
|
130
|
+
lines.push(` * ${operation.description}`);
|
|
131
|
+
}
|
|
132
|
+
lines.push(` *`);
|
|
133
|
+
lines.push(` * @source OpenAPI: ${operation.method.toUpperCase()} ${operation.path}`);
|
|
134
|
+
lines.push(` */`);
|
|
135
|
+
lines.push(`export const ${safeName}Spec = ${defineFunc}({`);
|
|
136
|
+
lines.push(" meta: {");
|
|
137
|
+
lines.push(` name: '${specName}',`);
|
|
138
|
+
lines.push(" version: 1,");
|
|
139
|
+
lines.push(` stability: '${options.defaultStability ?? "stable"}',`);
|
|
140
|
+
lines.push(` owners: [${(options.defaultOwners ?? []).map((o) => `'${o}'`).join(", ")}],`);
|
|
141
|
+
lines.push(` tags: [${operation.tags.map((t) => `'${t}'`).join(", ")}],`);
|
|
142
|
+
lines.push(` description: ${JSON.stringify(operation.summary ?? operation.operationId)},`);
|
|
143
|
+
lines.push(` goal: ${JSON.stringify(operation.description ?? "Imported from OpenAPI")},`);
|
|
144
|
+
lines.push(` context: 'Imported from OpenAPI: ${operation.method.toUpperCase()} ${operation.path}',`);
|
|
145
|
+
lines.push(" },");
|
|
146
|
+
lines.push(" io: {");
|
|
147
|
+
if (inputModel) lines.push(` input: ${inputModel.name},`);
|
|
148
|
+
else lines.push(" input: null,");
|
|
149
|
+
if (outputModel) lines.push(` output: ${outputModel.name},`);
|
|
150
|
+
else lines.push(" output: null, // TODO: Define output schema");
|
|
151
|
+
lines.push(" },");
|
|
152
|
+
lines.push(" policy: {");
|
|
153
|
+
lines.push(` auth: '${auth}',`);
|
|
154
|
+
lines.push(" },");
|
|
155
|
+
lines.push(" transport: {");
|
|
156
|
+
lines.push(" rest: {");
|
|
157
|
+
lines.push(` method: '${operation.method.toUpperCase()}',`);
|
|
158
|
+
lines.push(` path: '${operation.path}',`);
|
|
159
|
+
lines.push(" },");
|
|
160
|
+
lines.push(" },");
|
|
161
|
+
lines.push("});");
|
|
162
|
+
return lines.join("\n");
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Import operations from a parsed OpenAPI document.
|
|
166
|
+
*/
|
|
167
|
+
function importFromOpenApi(parseResult, options = {}) {
|
|
168
|
+
const { tags, exclude = [], include } = options;
|
|
169
|
+
const specs = [];
|
|
170
|
+
const skipped = [];
|
|
171
|
+
const errors = [];
|
|
172
|
+
for (const operation of parseResult.operations) {
|
|
173
|
+
if (tags && tags.length > 0) {
|
|
174
|
+
if (!operation.tags.some((t) => tags.includes(t))) {
|
|
175
|
+
skipped.push({
|
|
176
|
+
sourceId: operation.operationId,
|
|
177
|
+
reason: `No matching tags (has: ${operation.tags.join(", ")})`
|
|
178
|
+
});
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (include && include.length > 0) {
|
|
183
|
+
if (!include.includes(operation.operationId)) {
|
|
184
|
+
skipped.push({
|
|
185
|
+
sourceId: operation.operationId,
|
|
186
|
+
reason: "Not in include list"
|
|
187
|
+
});
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
} else if (exclude.includes(operation.operationId)) {
|
|
191
|
+
skipped.push({
|
|
192
|
+
sourceId: operation.operationId,
|
|
193
|
+
reason: "In exclude list"
|
|
194
|
+
});
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (operation.deprecated && options.defaultStability !== "deprecated") {
|
|
198
|
+
skipped.push({
|
|
199
|
+
sourceId: operation.operationId,
|
|
200
|
+
reason: "Deprecated operation"
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
const { schema: inputSchema } = buildInputSchema(operation);
|
|
206
|
+
const inputModel = inputSchema ? generateSchemaModelCode(inputSchema, `${operation.operationId}Input`) : null;
|
|
207
|
+
const outputSchema = getOutputSchema(operation);
|
|
208
|
+
const code = generateSpecCode(operation, options, inputModel, outputSchema ? generateSchemaModelCode(outputSchema, `${operation.operationId}Output`) : null);
|
|
209
|
+
const fileName = toFileName(toSpecName(operation.operationId, options.prefix));
|
|
210
|
+
const transportHints = { rest: {
|
|
211
|
+
method: operation.method.toUpperCase(),
|
|
212
|
+
path: operation.path,
|
|
213
|
+
params: {
|
|
214
|
+
path: operation.pathParams.map((p) => p.name),
|
|
215
|
+
query: operation.queryParams.map((p) => p.name),
|
|
216
|
+
header: operation.headerParams.map((p) => p.name),
|
|
217
|
+
cookie: operation.cookieParams.map((p) => p.name)
|
|
218
|
+
}
|
|
219
|
+
} };
|
|
220
|
+
const source = {
|
|
221
|
+
type: "openapi",
|
|
222
|
+
sourceId: operation.operationId,
|
|
223
|
+
operationId: operation.operationId,
|
|
224
|
+
openApiVersion: parseResult.version,
|
|
225
|
+
importedAt: /* @__PURE__ */ new Date()
|
|
226
|
+
};
|
|
227
|
+
specs.push({
|
|
228
|
+
spec: {},
|
|
229
|
+
code,
|
|
230
|
+
fileName,
|
|
231
|
+
source,
|
|
232
|
+
transportHints
|
|
233
|
+
});
|
|
234
|
+
} catch (error) {
|
|
235
|
+
errors.push({
|
|
236
|
+
sourceId: operation.operationId,
|
|
237
|
+
error: error instanceof Error ? error.message : String(error)
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
specs,
|
|
243
|
+
skipped,
|
|
244
|
+
errors,
|
|
245
|
+
summary: {
|
|
246
|
+
total: parseResult.operations.length,
|
|
247
|
+
imported: specs.length,
|
|
248
|
+
skipped: skipped.length,
|
|
249
|
+
errors: errors.length
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Import a single operation to ContractSpec code.
|
|
255
|
+
*/
|
|
256
|
+
function importOperation(operation, options = {}) {
|
|
257
|
+
const { schema: inputSchema } = buildInputSchema(operation);
|
|
258
|
+
const inputModel = inputSchema ? generateSchemaModelCode(inputSchema, `${operation.operationId}Input`) : null;
|
|
259
|
+
const outputSchema = getOutputSchema(operation);
|
|
260
|
+
return generateSpecCode(operation, options, inputModel, outputSchema ? generateSchemaModelCode(outputSchema, `${operation.operationId}Output`) : null);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
//#endregion
|
|
264
|
+
export { importFromOpenApi, importOperation };
|
package/dist/openapi/index.js
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString } from "./parser.js";
|
|
2
|
+
import { defaultRestPath, openApiForRegistry, openApiToJson, openApiToYaml } from "./exporter.js";
|
|
3
|
+
import { generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType } from "./schema-converter.js";
|
|
4
|
+
import { importFromOpenApi, importOperation } from "./importer.js";
|
|
5
|
+
import { createSpecDiff, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges } from "./differ.js";
|
|
6
|
+
|
|
7
|
+
export { createSpecDiff, defaultRestPath, detectFormat, detectVersion, diffAll, diffSpecVsOperation, diffSpecs, formatDiffChanges, generateImports, generateSchemaModelCode, getScalarType, importFromOpenApi, importOperation, jsonSchemaToField, jsonSchemaToType, openApiForRegistry, openApiToJson, openApiToYaml, parseOpenApi, parseOpenApiDocument, parseOpenApiString };
|
package/dist/openapi/parser.js
CHANGED
|
@@ -1 +1,231 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { parse } from "yaml";
|
|
2
|
+
|
|
3
|
+
//#region src/openapi/parser.ts
|
|
4
|
+
/**
|
|
5
|
+
* OpenAPI document parser.
|
|
6
|
+
* Parses OpenAPI 3.x documents from JSON/YAML files or URLs.
|
|
7
|
+
*/
|
|
8
|
+
const HTTP_METHODS = [
|
|
9
|
+
"get",
|
|
10
|
+
"post",
|
|
11
|
+
"put",
|
|
12
|
+
"delete",
|
|
13
|
+
"patch",
|
|
14
|
+
"head",
|
|
15
|
+
"options",
|
|
16
|
+
"trace"
|
|
17
|
+
];
|
|
18
|
+
/**
|
|
19
|
+
* Parse an OpenAPI document from a string (JSON or YAML).
|
|
20
|
+
*/
|
|
21
|
+
function parseOpenApiString(content, format = "json") {
|
|
22
|
+
if (format === "yaml") return parse(content);
|
|
23
|
+
return JSON.parse(content);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Detect the format of content (JSON or YAML).
|
|
27
|
+
*/
|
|
28
|
+
function detectFormat(content) {
|
|
29
|
+
const trimmed = content.trim();
|
|
30
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) return "json";
|
|
31
|
+
return "yaml";
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Detect OpenAPI version from document.
|
|
35
|
+
*/
|
|
36
|
+
function detectVersion(doc) {
|
|
37
|
+
if (doc.openapi.startsWith("3.1")) return "3.1";
|
|
38
|
+
return "3.0";
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check if a value is a reference object.
|
|
42
|
+
*/
|
|
43
|
+
function isReference(obj) {
|
|
44
|
+
return typeof obj === "object" && obj !== null && "$ref" in obj;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolve a $ref reference in the document.
|
|
48
|
+
*/
|
|
49
|
+
function resolveRef(doc, ref) {
|
|
50
|
+
if (!ref.startsWith("#/")) return;
|
|
51
|
+
const path = ref.slice(2).split("/");
|
|
52
|
+
let current = doc;
|
|
53
|
+
for (const part of path) {
|
|
54
|
+
if (current === null || current === void 0) return void 0;
|
|
55
|
+
if (typeof current !== "object") return void 0;
|
|
56
|
+
current = current[part];
|
|
57
|
+
}
|
|
58
|
+
return current;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolve a schema, following $ref if needed.
|
|
62
|
+
*/
|
|
63
|
+
function resolveSchema(doc, schema) {
|
|
64
|
+
if (!schema) return void 0;
|
|
65
|
+
if (isReference(schema)) return resolveRef(doc, schema.$ref) ?? schema;
|
|
66
|
+
return schema;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Parse parameters from an operation.
|
|
70
|
+
*/
|
|
71
|
+
function parseParameters(doc, params) {
|
|
72
|
+
const result = {
|
|
73
|
+
path: [],
|
|
74
|
+
query: [],
|
|
75
|
+
header: [],
|
|
76
|
+
cookie: []
|
|
77
|
+
};
|
|
78
|
+
if (!params) return result;
|
|
79
|
+
for (const param of params) {
|
|
80
|
+
let resolved;
|
|
81
|
+
if (isReference(param)) {
|
|
82
|
+
const ref = resolveRef(doc, param.$ref);
|
|
83
|
+
if (!ref) continue;
|
|
84
|
+
resolved = ref;
|
|
85
|
+
} else resolved = param;
|
|
86
|
+
const parsed = {
|
|
87
|
+
name: resolved.name,
|
|
88
|
+
in: resolved.in,
|
|
89
|
+
required: resolved.required ?? resolved.in === "path",
|
|
90
|
+
description: resolved.description,
|
|
91
|
+
schema: resolved.schema,
|
|
92
|
+
deprecated: resolved.deprecated ?? false
|
|
93
|
+
};
|
|
94
|
+
result[resolved.in]?.push(parsed);
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Generate an operationId if not present.
|
|
100
|
+
*/
|
|
101
|
+
function generateOperationId(method, path) {
|
|
102
|
+
return method + path.split("/").filter(Boolean).map((part) => {
|
|
103
|
+
if (part.startsWith("{") && part.endsWith("}")) return "By" + part.slice(1, -1).charAt(0).toUpperCase() + part.slice(2, -1);
|
|
104
|
+
return part.charAt(0).toUpperCase() + part.slice(1);
|
|
105
|
+
}).join("");
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Parse a single operation.
|
|
109
|
+
*/
|
|
110
|
+
function parseOperation(doc, method, path, operation, pathParams) {
|
|
111
|
+
const params = parseParameters(doc, [...pathParams ?? [], ...operation.parameters ?? []]);
|
|
112
|
+
let requestBody;
|
|
113
|
+
if (operation.requestBody) {
|
|
114
|
+
const body = isReference(operation.requestBody) ? resolveRef(doc, operation.requestBody.$ref) : operation.requestBody;
|
|
115
|
+
if (body) {
|
|
116
|
+
const contentType = Object.keys(body.content ?? {})[0] ?? "application/json";
|
|
117
|
+
const content = body.content?.[contentType];
|
|
118
|
+
if (content?.schema) requestBody = {
|
|
119
|
+
required: body.required ?? false,
|
|
120
|
+
schema: resolveSchema(doc, content.schema),
|
|
121
|
+
contentType
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const responses = {};
|
|
126
|
+
for (const [status, response] of Object.entries(operation.responses ?? {})) {
|
|
127
|
+
const resolved = isReference(response) ? resolveRef(doc, response.$ref) : response;
|
|
128
|
+
if (resolved) {
|
|
129
|
+
const contentType = Object.keys(resolved.content ?? {})[0];
|
|
130
|
+
const content = contentType ? resolved.content?.[contentType] : void 0;
|
|
131
|
+
responses[status] = {
|
|
132
|
+
description: resolved.description,
|
|
133
|
+
schema: content?.schema ? resolveSchema(doc, content.schema) : void 0,
|
|
134
|
+
contentType
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const contractSpecMeta = operation?.["x-contractspec"];
|
|
139
|
+
return {
|
|
140
|
+
operationId: operation.operationId ?? generateOperationId(method, path),
|
|
141
|
+
method,
|
|
142
|
+
path,
|
|
143
|
+
summary: operation.summary,
|
|
144
|
+
description: operation.description,
|
|
145
|
+
tags: operation.tags ?? [],
|
|
146
|
+
pathParams: params.path,
|
|
147
|
+
queryParams: params.query,
|
|
148
|
+
headerParams: params.header,
|
|
149
|
+
cookieParams: params.cookie,
|
|
150
|
+
requestBody,
|
|
151
|
+
responses,
|
|
152
|
+
deprecated: operation.deprecated ?? false,
|
|
153
|
+
security: operation.security,
|
|
154
|
+
contractSpecMeta
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Parse an OpenAPI document into a structured result.
|
|
159
|
+
*/
|
|
160
|
+
function parseOpenApiDocument(doc, _options = {}) {
|
|
161
|
+
const version = detectVersion(doc);
|
|
162
|
+
const warnings = [];
|
|
163
|
+
const operations = [];
|
|
164
|
+
for (const [path, pathItem] of Object.entries(doc.paths ?? {})) {
|
|
165
|
+
if (!pathItem) continue;
|
|
166
|
+
const pathParams = pathItem.parameters;
|
|
167
|
+
for (const method of HTTP_METHODS) {
|
|
168
|
+
const operation = pathItem[method];
|
|
169
|
+
if (operation) try {
|
|
170
|
+
operations.push(parseOperation(doc, method, path, operation, pathParams));
|
|
171
|
+
} catch (error) {
|
|
172
|
+
warnings.push(`Failed to parse ${method.toUpperCase()} ${path}: ${error}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const schemas = {};
|
|
177
|
+
const components = doc.components;
|
|
178
|
+
if (components?.schemas) for (const [name, schema] of Object.entries(components.schemas)) schemas[name] = schema;
|
|
179
|
+
const servers = (doc.servers ?? []).map((s) => ({
|
|
180
|
+
url: s.url,
|
|
181
|
+
description: s.description,
|
|
182
|
+
variables: s.variables
|
|
183
|
+
}));
|
|
184
|
+
return {
|
|
185
|
+
document: doc,
|
|
186
|
+
version,
|
|
187
|
+
info: {
|
|
188
|
+
title: doc.info.title,
|
|
189
|
+
version: doc.info.version,
|
|
190
|
+
description: doc.info.description
|
|
191
|
+
},
|
|
192
|
+
operations,
|
|
193
|
+
schemas,
|
|
194
|
+
servers,
|
|
195
|
+
warnings
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Parse OpenAPI from a file path or URL.
|
|
200
|
+
* Note: This is an async function that requires I/O adapters.
|
|
201
|
+
* For pure parsing, use parseOpenApiString or parseOpenApiDocument.
|
|
202
|
+
*/
|
|
203
|
+
async function parseOpenApi(source, options = {}) {
|
|
204
|
+
const { fetch: fetchFn = globalThis.fetch, readFile, timeout = 3e4 } = options;
|
|
205
|
+
let content;
|
|
206
|
+
let format;
|
|
207
|
+
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
208
|
+
const controller = new AbortController();
|
|
209
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
210
|
+
try {
|
|
211
|
+
const response = await fetchFn(source, { signal: controller.signal });
|
|
212
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
213
|
+
content = await response.text();
|
|
214
|
+
} finally {
|
|
215
|
+
clearTimeout(timeoutId);
|
|
216
|
+
}
|
|
217
|
+
if (source.endsWith(".yaml") || source.endsWith(".yml")) format = "yaml";
|
|
218
|
+
else if (source.endsWith(".json")) format = "json";
|
|
219
|
+
else format = detectFormat(content);
|
|
220
|
+
} else {
|
|
221
|
+
if (!readFile) throw new Error("readFile adapter required for file paths");
|
|
222
|
+
content = await readFile(source);
|
|
223
|
+
if (source.endsWith(".yaml") || source.endsWith(".yml")) format = "yaml";
|
|
224
|
+
else if (source.endsWith(".json")) format = "json";
|
|
225
|
+
else format = detectFormat(content);
|
|
226
|
+
}
|
|
227
|
+
return parseOpenApiDocument(parseOpenApiString(content, format), options);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
//#endregion
|
|
231
|
+
export { detectFormat, detectVersion, parseOpenApi, parseOpenApiDocument, parseOpenApiString };
|
|
@@ -1,4 +1,201 @@
|
|
|
1
|
-
import{toCamelCase
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { toCamelCase, toPascalCase, toValidIdentifier } from "../common/utils.js";
|
|
2
|
+
|
|
3
|
+
//#region src/openapi/schema-converter.ts
|
|
4
|
+
/**
|
|
5
|
+
* Map JSON Schema types to ContractSpec ScalarTypeEnum values.
|
|
6
|
+
*/
|
|
7
|
+
const JSON_SCHEMA_TO_SCALAR = {
|
|
8
|
+
string: "ScalarTypeEnum.STRING",
|
|
9
|
+
integer: "ScalarTypeEnum.INT",
|
|
10
|
+
number: "ScalarTypeEnum.FLOAT",
|
|
11
|
+
boolean: "ScalarTypeEnum.BOOLEAN",
|
|
12
|
+
"string:date": "ScalarTypeEnum.DATE",
|
|
13
|
+
"string:date-time": "ScalarTypeEnum.DATE_TIME",
|
|
14
|
+
"string:email": "ScalarTypeEnum.EMAIL",
|
|
15
|
+
"string:uri": "ScalarTypeEnum.URL",
|
|
16
|
+
"string:uuid": "ScalarTypeEnum.ID"
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Check if a schema is a reference object.
|
|
20
|
+
*/
|
|
21
|
+
function isReference(schema) {
|
|
22
|
+
return "$ref" in schema;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Extract type name from a $ref.
|
|
26
|
+
*/
|
|
27
|
+
function typeNameFromRef(ref) {
|
|
28
|
+
const parts = ref.split("/");
|
|
29
|
+
return parts[parts.length - 1] ?? "Unknown";
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Convert a JSON Schema to a TypeScript type representation.
|
|
33
|
+
*/
|
|
34
|
+
function jsonSchemaToType(schema, name) {
|
|
35
|
+
if (isReference(schema)) return {
|
|
36
|
+
type: toPascalCase(typeNameFromRef(schema.$ref)),
|
|
37
|
+
optional: false,
|
|
38
|
+
array: false,
|
|
39
|
+
primitive: false
|
|
40
|
+
};
|
|
41
|
+
const schemaObj = schema;
|
|
42
|
+
const type = schemaObj["type"];
|
|
43
|
+
const format = schemaObj["format"];
|
|
44
|
+
const nullable = schemaObj["nullable"];
|
|
45
|
+
if (type === "array") {
|
|
46
|
+
const items = schemaObj["items"];
|
|
47
|
+
if (items) return {
|
|
48
|
+
...jsonSchemaToType(items, name),
|
|
49
|
+
array: true,
|
|
50
|
+
optional: nullable ?? false
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
type: "unknown",
|
|
54
|
+
optional: nullable ?? false,
|
|
55
|
+
array: true,
|
|
56
|
+
primitive: false
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (type === "object" || schemaObj["properties"]) return {
|
|
60
|
+
type: name ? toPascalCase(name) : "Record<string, unknown>",
|
|
61
|
+
optional: nullable ?? false,
|
|
62
|
+
array: false,
|
|
63
|
+
primitive: false
|
|
64
|
+
};
|
|
65
|
+
if (schemaObj["enum"]) return {
|
|
66
|
+
type: name ? toPascalCase(name) : "string",
|
|
67
|
+
optional: nullable ?? false,
|
|
68
|
+
array: false,
|
|
69
|
+
primitive: false
|
|
70
|
+
};
|
|
71
|
+
format && `${type}${format}`;
|
|
72
|
+
if (type === "string") return {
|
|
73
|
+
type: "string",
|
|
74
|
+
optional: nullable ?? false,
|
|
75
|
+
array: false,
|
|
76
|
+
primitive: true
|
|
77
|
+
};
|
|
78
|
+
if (type === "integer" || type === "number") return {
|
|
79
|
+
type: "number",
|
|
80
|
+
optional: nullable ?? false,
|
|
81
|
+
array: false,
|
|
82
|
+
primitive: true
|
|
83
|
+
};
|
|
84
|
+
if (type === "boolean") return {
|
|
85
|
+
type: "boolean",
|
|
86
|
+
optional: nullable ?? false,
|
|
87
|
+
array: false,
|
|
88
|
+
primitive: true
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
type: "unknown",
|
|
92
|
+
optional: nullable ?? false,
|
|
93
|
+
array: false,
|
|
94
|
+
primitive: false
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get the ScalarTypeEnum value for a JSON Schema type.
|
|
99
|
+
*/
|
|
100
|
+
function getScalarType(schema) {
|
|
101
|
+
if (isReference(schema)) return;
|
|
102
|
+
const schemaObj = schema;
|
|
103
|
+
const type = schemaObj["type"];
|
|
104
|
+
const format = schemaObj["format"];
|
|
105
|
+
if (!type) return void 0;
|
|
106
|
+
return JSON_SCHEMA_TO_SCALAR[format ? `${type}:${format}` : type] ?? JSON_SCHEMA_TO_SCALAR[type];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Convert a JSON Schema to a SchemaModel field definition.
|
|
110
|
+
*/
|
|
111
|
+
function jsonSchemaToField(schema, fieldName, required) {
|
|
112
|
+
const type = jsonSchemaToType(schema, fieldName);
|
|
113
|
+
const scalarType = getScalarType(schema);
|
|
114
|
+
let enumValues;
|
|
115
|
+
if (!isReference(schema)) {
|
|
116
|
+
const enumArr = schema["enum"];
|
|
117
|
+
if (enumArr) enumValues = enumArr.map(String);
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
name: toValidIdentifier(toCamelCase(fieldName)),
|
|
121
|
+
type: {
|
|
122
|
+
...type,
|
|
123
|
+
optional: !required || type.optional,
|
|
124
|
+
description: !isReference(schema) ? schema["description"] : void 0
|
|
125
|
+
},
|
|
126
|
+
scalarType,
|
|
127
|
+
enumValues
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Generate SchemaModel TypeScript code for a JSON Schema object.
|
|
132
|
+
*/
|
|
133
|
+
function generateSchemaModelCode(schema, modelName, indent = 0) {
|
|
134
|
+
const spaces = " ".repeat(indent);
|
|
135
|
+
const fields = [];
|
|
136
|
+
let description;
|
|
137
|
+
if (isReference(schema)) return {
|
|
138
|
+
name: toPascalCase(typeNameFromRef(schema.$ref)),
|
|
139
|
+
fields: [],
|
|
140
|
+
code: `// Reference to ${schema.$ref}`
|
|
141
|
+
};
|
|
142
|
+
const schemaObj = schema;
|
|
143
|
+
description = schemaObj["description"];
|
|
144
|
+
const properties = schemaObj["properties"];
|
|
145
|
+
const required = schemaObj["required"] ?? [];
|
|
146
|
+
if (!properties) return {
|
|
147
|
+
name: toPascalCase(modelName),
|
|
148
|
+
description,
|
|
149
|
+
fields: [],
|
|
150
|
+
code: `${spaces}// Empty schema for ${modelName}`
|
|
151
|
+
};
|
|
152
|
+
for (const [propName, propSchema] of Object.entries(properties)) {
|
|
153
|
+
const isRequired = required.includes(propName);
|
|
154
|
+
fields.push(jsonSchemaToField(propSchema, propName, isRequired));
|
|
155
|
+
}
|
|
156
|
+
const lines = [];
|
|
157
|
+
const safeModelName = toPascalCase(toValidIdentifier(modelName));
|
|
158
|
+
lines.push(`${spaces}export const ${safeModelName} = defineSchemaModel({`);
|
|
159
|
+
lines.push(`${spaces} name: '${safeModelName}',`);
|
|
160
|
+
if (description) lines.push(`${spaces} description: ${JSON.stringify(description)},`);
|
|
161
|
+
lines.push(`${spaces} fields: {`);
|
|
162
|
+
for (const field of fields) {
|
|
163
|
+
const fieldLines = generateFieldCode(field, indent + 2);
|
|
164
|
+
lines.push(fieldLines);
|
|
165
|
+
}
|
|
166
|
+
lines.push(`${spaces} },`);
|
|
167
|
+
lines.push(`${spaces}});`);
|
|
168
|
+
return {
|
|
169
|
+
name: safeModelName,
|
|
170
|
+
description,
|
|
171
|
+
fields,
|
|
172
|
+
code: lines.join("\n")
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Generate TypeScript code for a single field.
|
|
177
|
+
*/
|
|
178
|
+
function generateFieldCode(field, indent) {
|
|
179
|
+
const spaces = " ".repeat(indent);
|
|
180
|
+
const lines = [];
|
|
181
|
+
lines.push(`${spaces}${field.name}: {`);
|
|
182
|
+
if (field.enumValues) lines.push(`${spaces} type: new EnumType([${field.enumValues.map((v) => `'${v}'`).join(", ")}]),`);
|
|
183
|
+
else if (field.scalarType) lines.push(`${spaces} type: ${field.scalarType},`);
|
|
184
|
+
else lines.push(`${spaces} type: ${field.type.type}, // TODO: Define or import this type`);
|
|
185
|
+
if (field.type.optional) lines.push(`${spaces} isOptional: true,`);
|
|
186
|
+
if (field.type.array) lines.push(`${spaces} isArray: true,`);
|
|
187
|
+
lines.push(`${spaces}},`);
|
|
188
|
+
return lines.join("\n");
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Generate import statements for a SchemaModel.
|
|
192
|
+
*/
|
|
193
|
+
function generateImports(fields) {
|
|
194
|
+
const imports = /* @__PURE__ */ new Set();
|
|
195
|
+
imports.add("import { defineSchemaModel, ScalarTypeEnum, EnumType } from '@lssm/lib.schema';");
|
|
196
|
+
for (const field of fields) if (!field.type.primitive && !field.enumValues && !field.scalarType) {}
|
|
197
|
+
return Array.from(imports).join("\n");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
export { generateImports, generateSchemaModelCode, getScalarType, jsonSchemaToField, jsonSchemaToType };
|
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-20251217073102",
|
|
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-20251217073102",
|
|
21
|
+
"@lssm/lib.schema": "0.0.0-canary-20251217073102",
|
|
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-20251217073102",
|
|
28
|
+
"@lssm/tool.typescript": "0.0.0-canary-20251217073102",
|
|
29
29
|
"tsdown": "^0.17.4",
|
|
30
30
|
"typescript": "^5.9.3"
|
|
31
31
|
},
|