@lssm/lib.contracts-transformers 0.0.0-canary-20251220041653 → 0.0.0-canary-20251221132705
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/common/index.d.ts +2 -2
- package/dist/common/types.d.ts +16 -10
- package/dist/common/types.d.ts.map +1 -1
- package/dist/index.d.ts +15 -6
- package/dist/index.js +13 -4
- package/dist/openapi/differ.d.ts +6 -6
- package/dist/openapi/differ.d.ts.map +1 -1
- package/dist/openapi/differ.js +11 -4
- package/dist/openapi/differ.js.map +1 -1
- package/dist/openapi/exporter/data-views.d.ts +38 -0
- package/dist/openapi/exporter/data-views.d.ts.map +1 -0
- package/dist/openapi/exporter/data-views.js +47 -0
- package/dist/openapi/exporter/data-views.js.map +1 -0
- package/dist/openapi/exporter/events.d.ts +28 -0
- package/dist/openapi/exporter/events.d.ts.map +1 -0
- package/dist/openapi/exporter/events.js +39 -0
- package/dist/openapi/exporter/events.js.map +1 -0
- package/dist/openapi/exporter/features.d.ts +37 -0
- package/dist/openapi/exporter/features.d.ts.map +1 -0
- package/dist/openapi/exporter/features.js +46 -0
- package/dist/openapi/exporter/features.js.map +1 -0
- package/dist/openapi/exporter/forms.d.ts +30 -0
- package/dist/openapi/exporter/forms.d.ts.map +1 -0
- package/dist/openapi/exporter/forms.js +49 -0
- package/dist/openapi/exporter/forms.js.map +1 -0
- package/dist/openapi/exporter/index.js +8 -0
- package/dist/openapi/exporter/operations.d.ts +65 -0
- package/dist/openapi/exporter/operations.d.ts.map +1 -0
- package/dist/openapi/exporter/operations.js +142 -0
- package/dist/openapi/exporter/operations.js.map +1 -0
- package/dist/openapi/exporter/presentations.d.ts +48 -0
- package/dist/openapi/exporter/presentations.d.ts.map +1 -0
- package/dist/openapi/exporter/presentations.js +66 -0
- package/dist/openapi/exporter/presentations.js.map +1 -0
- package/dist/openapi/exporter/registries.d.ts +23 -0
- package/dist/openapi/exporter/registries.d.ts.map +1 -0
- package/dist/openapi/exporter/registries.js +29 -0
- package/dist/openapi/exporter/registries.js.map +1 -0
- package/dist/openapi/exporter/workflows.d.ts +36 -0
- package/dist/openapi/exporter/workflows.d.ts.map +1 -0
- package/dist/openapi/exporter/workflows.js +54 -0
- package/dist/openapi/exporter/workflows.js.map +1 -0
- package/dist/openapi/exporter.d.ts +35 -15
- package/dist/openapi/exporter.d.ts.map +1 -1
- package/dist/openapi/exporter.js +78 -104
- package/dist/openapi/exporter.js.map +1 -1
- package/dist/openapi/importer/analyzer.js +28 -0
- package/dist/openapi/importer/analyzer.js.map +1 -0
- package/dist/openapi/importer/events.js +36 -0
- package/dist/openapi/importer/events.js.map +1 -0
- package/dist/openapi/importer/generator.js +71 -0
- package/dist/openapi/importer/generator.js.map +1 -0
- package/dist/openapi/importer/grouping.js +73 -0
- package/dist/openapi/importer/grouping.js.map +1 -0
- package/dist/openapi/importer/index.d.ts +17 -0
- package/dist/openapi/importer/index.d.ts.map +1 -0
- package/dist/openapi/importer/index.js +161 -0
- package/dist/openapi/importer/index.js.map +1 -0
- package/dist/openapi/importer/models.js +19 -0
- package/dist/openapi/importer/models.js.map +1 -0
- package/dist/openapi/importer/schemas.js +80 -0
- package/dist/openapi/importer/schemas.js.map +1 -0
- package/dist/openapi/index.d.ts +14 -5
- package/dist/openapi/index.js +15 -4
- package/dist/openapi/parser/document.d.ts +20 -0
- package/dist/openapi/parser/document.d.ts.map +1 -0
- package/dist/openapi/parser/document.js +95 -0
- package/dist/openapi/parser/document.js.map +1 -0
- package/dist/openapi/parser/index.js +5 -0
- package/dist/openapi/parser/operation.js +59 -0
- package/dist/openapi/parser/operation.js.map +1 -0
- package/dist/openapi/parser/parameters.js +37 -0
- package/dist/openapi/parser/parameters.js.map +1 -0
- package/dist/openapi/parser/resolvers.js +63 -0
- package/dist/openapi/parser/resolvers.js.map +1 -0
- package/dist/openapi/parser/utils.d.ts +19 -0
- package/dist/openapi/parser/utils.d.ts.map +1 -0
- package/dist/openapi/parser/utils.js +48 -0
- package/dist/openapi/parser/utils.js.map +1 -0
- package/dist/openapi/parser.js +6 -232
- package/dist/openapi/schema-converter.d.ts +7 -1
- package/dist/openapi/schema-converter.d.ts.map +1 -1
- package/dist/openapi/schema-converter.js +117 -20
- package/dist/openapi/schema-converter.js.map +1 -1
- package/dist/openapi/types.d.ts +75 -20
- package/dist/openapi/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/openapi/importer.d.ts +0 -16
- package/dist/openapi/importer.d.ts.map +0 -1
- package/dist/openapi/importer.js +0 -265
- package/dist/openapi/importer.js.map +0 -1
- package/dist/openapi/parser.d.ts +0 -32
- package/dist/openapi/parser.d.ts.map +0 -1
- package/dist/openapi/parser.js.map +0 -1
|
@@ -1,28 +1,48 @@
|
|
|
1
|
-
import { ContractSpecOpenApiDocument, OpenApiExportOptions, OpenApiServer } from "./types.js";
|
|
2
|
-
import {
|
|
1
|
+
import { ContractSpecExportOptions, ContractSpecExportResult, ContractSpecOpenApiDocument, OpenApiExportOptions, OpenApiServer } from "./types.js";
|
|
2
|
+
import { defaultRestPath } from "./exporter/operations.js";
|
|
3
|
+
import { DataViewRegistry, EventSpec, FeatureRegistry, FormRegistry, OperationSpecRegistry, PresentationDescriptorV2, PresentationRegistry, WorkflowRegistry } from "@lssm/lib.contracts";
|
|
4
|
+
import { AnySchemaModel } from "@lssm/lib.schema";
|
|
3
5
|
|
|
4
6
|
//#region src/openapi/exporter.d.ts
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
|
-
*
|
|
9
|
+
* Input registries for unified export.
|
|
8
10
|
*/
|
|
9
|
-
|
|
11
|
+
interface ContractSpecRegistries {
|
|
12
|
+
operations?: OperationSpecRegistry;
|
|
13
|
+
events?: EventSpec<AnySchemaModel>[];
|
|
14
|
+
features?: FeatureRegistry;
|
|
15
|
+
presentations?: PresentationRegistry;
|
|
16
|
+
presentationsV2?: PresentationDescriptorV2[];
|
|
17
|
+
forms?: FormRegistry;
|
|
18
|
+
dataViews?: DataViewRegistry;
|
|
19
|
+
workflows?: WorkflowRegistry;
|
|
20
|
+
}
|
|
10
21
|
/**
|
|
11
|
-
* Export a
|
|
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
|
|
22
|
+
* Export a OperationSpecRegistry to an OpenAPI 3.1 document.
|
|
23
|
+
* @deprecated Use exportContractSpec for full surface support.
|
|
16
24
|
*/
|
|
17
|
-
declare function openApiForRegistry(registry:
|
|
25
|
+
declare function openApiForRegistry(registry: OperationSpecRegistry, options?: OpenApiExportOptions): ContractSpecOpenApiDocument;
|
|
18
26
|
/**
|
|
19
|
-
* Export
|
|
27
|
+
* Export all ContractSpec surfaces to OpenAPI document with extensions.
|
|
20
28
|
*/
|
|
21
|
-
declare function
|
|
29
|
+
declare function exportContractSpec(registries: ContractSpecRegistries, options?: ContractSpecExportOptions): ContractSpecExportResult;
|
|
22
30
|
/**
|
|
23
|
-
* Export a
|
|
31
|
+
* Export a OperationSpecRegistry to OpenAPI JSON string.
|
|
24
32
|
*/
|
|
25
|
-
declare function
|
|
33
|
+
declare function openApiToJson(registry: OperationSpecRegistry, options?: OpenApiExportOptions): string;
|
|
34
|
+
/**
|
|
35
|
+
* Export a OperationSpecRegistry to OpenAPI YAML string.
|
|
36
|
+
*/
|
|
37
|
+
declare function openApiToYaml(registry: OperationSpecRegistry, options?: OpenApiExportOptions): string;
|
|
38
|
+
/**
|
|
39
|
+
* Export ContractSpec to JSON string (all surfaces).
|
|
40
|
+
*/
|
|
41
|
+
declare function contractSpecToJson(registries: ContractSpecRegistries, options?: ContractSpecExportOptions): string;
|
|
42
|
+
/**
|
|
43
|
+
* Export ContractSpec to YAML string (all surfaces).
|
|
44
|
+
*/
|
|
45
|
+
declare function contractSpecToYaml(registries: ContractSpecRegistries, options?: ContractSpecExportOptions): string;
|
|
26
46
|
//#endregion
|
|
27
|
-
export {
|
|
47
|
+
export { ContractSpecRegistries, contractSpecToJson, contractSpecToYaml, exportContractSpec, openApiForRegistry, openApiToJson, openApiToYaml };
|
|
28
48
|
//# sourceMappingURL=exporter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exporter.d.ts","names":[],"sources":["../../src/openapi/exporter.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"exporter.d.ts","names":[],"sources":["../../src/openapi/exporter.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;AAmDkB,UAJD,sBAAA,CAIC;EACE,UAAA,CAAA,EAJL,qBAIK;EACV,MAAA,CAAA,EAJC,SAID,CAJW,cAIX,CAAA,EAAA;EACI,QAAA,CAAA,EAJD,eAIC;EACA,aAAA,CAAA,EAJI,oBAIJ;EAAgB,eAAA,CAAA,EAHV,wBAGU,EAAA;EAUd,KAAA,CAAA,EAZN,YAYM;EACJ,SAAA,CAAA,EAZE,gBAYF;EACD,SAAA,CAAA,EAZG,gBAYH;;AAoBX;;;;AAG2B,iBAzBX,kBAAA,CAyBW,QAAA,EAxBf,qBAwBe,EAAA,OAAA,CAAA,EAvBhB,oBAuBgB,CAAA,EAtBxB,2BAsBwB;AAiI3B;AAWA;AAWA;AAWgB,iBArKA,kBAAA,CAsKF,UAAA,EArKA,sBAsKH,EAAA,OAA8B,CAAA,EArK9B,yBAqK8B,CAAA,EApKtC,wBAoKsC;;;;iBAnCzB,aAAA,WACJ,iCACD;;;;iBASK,aAAA,WACJ,iCACD;;;;iBASK,kBAAA,aACF,kCACH;;;;iBASK,kBAAA,aACF,kCACH"}
|
package/dist/openapi/exporter.js
CHANGED
|
@@ -1,73 +1,43 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defaultRestPath, exportOperations, generateOperationsRegistry } from "./exporter/operations.js";
|
|
2
|
+
import { exportEvents, generateEventsExports } from "./exporter/events.js";
|
|
3
|
+
import { exportFeatures, generateFeaturesRegistry } from "./exporter/features.js";
|
|
4
|
+
import { exportPresentations, generatePresentationsRegistry } from "./exporter/presentations.js";
|
|
5
|
+
import { exportForms, generateFormsRegistry } from "./exporter/forms.js";
|
|
6
|
+
import { exportDataViews, generateDataViewsRegistry } from "./exporter/data-views.js";
|
|
7
|
+
import { exportWorkflows, generateWorkflowsRegistry } from "./exporter/workflows.js";
|
|
8
|
+
import { generateRegistryIndex } from "./exporter/registries.js";
|
|
2
9
|
|
|
3
10
|
//#region src/openapi/exporter.ts
|
|
4
11
|
/**
|
|
5
|
-
*
|
|
12
|
+
* Export a OperationSpecRegistry to an OpenAPI 3.1 document.
|
|
13
|
+
* @deprecated Use exportContractSpec for full surface support.
|
|
6
14
|
*/
|
|
7
|
-
function
|
|
8
|
-
|
|
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) {
|
|
15
|
+
function openApiForRegistry(registry, options = {}) {
|
|
16
|
+
const { paths, schemas } = exportOperations(registry);
|
|
46
17
|
return {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
18
|
+
openapi: "3.1.0",
|
|
19
|
+
info: {
|
|
20
|
+
title: options.title ?? "ContractSpec API",
|
|
21
|
+
version: options.version ?? "0.0.0",
|
|
22
|
+
...options.description ? { description: options.description } : {}
|
|
23
|
+
},
|
|
24
|
+
...options.servers ? { servers: options.servers } : {},
|
|
25
|
+
paths,
|
|
26
|
+
components: { schemas }
|
|
57
27
|
};
|
|
58
28
|
}
|
|
59
29
|
/**
|
|
60
|
-
* Export
|
|
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
|
|
30
|
+
* Export all ContractSpec surfaces to OpenAPI document with extensions.
|
|
65
31
|
*/
|
|
66
|
-
function
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
32
|
+
function exportContractSpec(registries, options = {}) {
|
|
33
|
+
const { operations: includeOps = true, events: includeEvents = true, features: includeFeatures = true, presentations: includePresentations = true, forms: includeForms = true, dataViews: includeDataViews = true, workflows: includeWorkflows = true, generateRegistries = true } = options;
|
|
34
|
+
let paths = {};
|
|
35
|
+
let schemas = {};
|
|
36
|
+
if (includeOps && registries.operations) {
|
|
37
|
+
const opResult = exportOperations(registries.operations);
|
|
38
|
+
paths = opResult.paths;
|
|
39
|
+
schemas = opResult.schemas;
|
|
40
|
+
}
|
|
71
41
|
const doc = {
|
|
72
42
|
openapi: "3.1.0",
|
|
73
43
|
info: {
|
|
@@ -76,63 +46,67 @@ function openApiForRegistry(registry, options = {}) {
|
|
|
76
46
|
...options.description ? { description: options.description } : {}
|
|
77
47
|
},
|
|
78
48
|
...options.servers ? { servers: options.servers } : {},
|
|
79
|
-
paths
|
|
80
|
-
components: { schemas
|
|
49
|
+
paths,
|
|
50
|
+
components: { schemas }
|
|
81
51
|
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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;
|
|
52
|
+
if (includeEvents && registries.events?.length) doc["x-contractspec-events"] = exportEvents(registries.events);
|
|
53
|
+
if (includeFeatures && registries.features) doc["x-contractspec-features"] = exportFeatures(registries.features);
|
|
54
|
+
if (includePresentations && registries.presentations) {
|
|
55
|
+
const presExport = exportPresentations(registries.presentations, registries.presentationsV2);
|
|
56
|
+
doc["x-contractspec-presentations"] = [...presExport.v1, ...presExport.v2];
|
|
57
|
+
}
|
|
58
|
+
if (includeForms && registries.forms) doc["x-contractspec-forms"] = exportForms(registries.forms);
|
|
59
|
+
if (includeDataViews && registries.dataViews) doc["x-contractspec-dataviews"] = exportDataViews(registries.dataViews);
|
|
60
|
+
if (includeWorkflows && registries.workflows) doc["x-contractspec-workflows"] = exportWorkflows(registries.workflows);
|
|
61
|
+
const result = { openApi: doc };
|
|
62
|
+
if (generateRegistries) {
|
|
63
|
+
result.registries = {};
|
|
64
|
+
if (includeOps && registries.operations) result.registries.operations = generateOperationsRegistry(registries.operations);
|
|
65
|
+
if (includeEvents && registries.events?.length) result.registries.events = generateEventsExports(registries.events);
|
|
66
|
+
if (includeFeatures && registries.features) result.registries.features = generateFeaturesRegistry(registries.features);
|
|
67
|
+
if (includePresentations && registries.presentations) result.registries.presentations = generatePresentationsRegistry(registries.presentations);
|
|
68
|
+
if (includeForms && registries.forms) result.registries.forms = generateFormsRegistry(registries.forms);
|
|
69
|
+
if (includeDataViews && registries.dataViews) result.registries.dataViews = generateDataViewsRegistry(registries.dataViews);
|
|
70
|
+
if (includeWorkflows && registries.workflows) result.registries.workflows = generateWorkflowsRegistry(registries.workflows);
|
|
71
|
+
result.registries.index = generateRegistryIndex({
|
|
72
|
+
operations: includeOps && !!registries.operations,
|
|
73
|
+
events: includeEvents && !!registries.events?.length,
|
|
74
|
+
features: includeFeatures && !!registries.features,
|
|
75
|
+
presentations: includePresentations && !!registries.presentations,
|
|
76
|
+
forms: includeForms && !!registries.forms,
|
|
77
|
+
dataViews: includeDataViews && !!registries.dataViews,
|
|
78
|
+
workflows: includeWorkflows && !!registries.workflows
|
|
79
|
+
});
|
|
119
80
|
}
|
|
120
|
-
return
|
|
81
|
+
return result;
|
|
121
82
|
}
|
|
122
83
|
/**
|
|
123
|
-
* Export a
|
|
84
|
+
* Export a OperationSpecRegistry to OpenAPI JSON string.
|
|
124
85
|
*/
|
|
125
86
|
function openApiToJson(registry, options = {}) {
|
|
126
87
|
const doc = openApiForRegistry(registry, options);
|
|
127
88
|
return JSON.stringify(doc, null, 2);
|
|
128
89
|
}
|
|
129
90
|
/**
|
|
130
|
-
* Export a
|
|
91
|
+
* Export a OperationSpecRegistry to OpenAPI YAML string.
|
|
131
92
|
*/
|
|
132
93
|
function openApiToYaml(registry, options = {}) {
|
|
133
94
|
return jsonToYaml(openApiForRegistry(registry, options));
|
|
134
95
|
}
|
|
135
96
|
/**
|
|
97
|
+
* Export ContractSpec to JSON string (all surfaces).
|
|
98
|
+
*/
|
|
99
|
+
function contractSpecToJson(registries, options = {}) {
|
|
100
|
+
const result = exportContractSpec(registries, options);
|
|
101
|
+
return JSON.stringify(result.openApi, null, 2);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Export ContractSpec to YAML string (all surfaces).
|
|
105
|
+
*/
|
|
106
|
+
function contractSpecToYaml(registries, options = {}) {
|
|
107
|
+
return jsonToYaml(exportContractSpec(registries, options).openApi);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
136
110
|
* Simple JSON to YAML conversion.
|
|
137
111
|
*/
|
|
138
112
|
function jsonToYaml(obj, indent = 0) {
|
|
@@ -147,5 +121,5 @@ function jsonToYaml(obj, indent = 0) {
|
|
|
147
121
|
}
|
|
148
122
|
|
|
149
123
|
//#endregion
|
|
150
|
-
export {
|
|
124
|
+
export { contractSpecToJson, contractSpecToYaml, exportContractSpec, openApiForRegistry, openApiToJson, openApiToYaml };
|
|
151
125
|
//# sourceMappingURL=exporter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exporter.js","names":["doc: ContractSpecOpenApiDocument","op: Record<string, unknown>","responses: Record<string, unknown>"],"sources":["../../src/openapi/exporter.ts"],"sourcesContent":["/**\n * Export ContractSpec specs to OpenAPI 3.1 format.\n * Moved from @lssm/lib.contracts/openapi.ts with enhancements.\n */\n\nimport type {\n SpecRegistry,\n AnyContractSpec,\n ContractSpec,\n} from '@lssm/lib.contracts';\nimport type { AnySchemaModel } from '@lssm/lib.schema';\nimport { z } from 'zod';\nimport type {\n OpenApiExportOptions,\n OpenApiServer,\n ContractSpecOpenApiDocument,\n} from './types';\n\ntype OpenApiSchemaObject = Record<string, unknown>;\n\n/**\n * Convert a spec name and version to an operationId.\n */\nfunction toOperationId(name: string, version: number): string {\n return `${name.replace(/\\./g, '_')}_v${version}`;\n}\n\n/**\n * Convert a spec name and version to a schema name.\n */\nfunction toSchemaName(\n prefix: 'Input' | 'Output',\n name: string,\n version: number\n): string {\n return `${prefix}_${toOperationId(name, version)}`;\n}\n\n/**\n * Determine HTTP method from spec kind and override.\n */\nfunction toHttpMethod(\n kind: 'command' | 'query',\n override?: 'GET' | 'POST'\n): string {\n const method = override ?? (kind === 'query' ? 'GET' : 'POST');\n return method.toLowerCase();\n}\n\n/**\n * Generate default REST path from spec name and version.\n */\nexport function defaultRestPath(name: string, version: number): string {\n return `/${name.replace(/\\./g, '/')}/v${version}`;\n}\n\n/**\n * Get REST path from spec, using transport override or default.\n */\nfunction toRestPath(spec: AnyContractSpec): string {\n const path =\n spec.transport?.rest?.path ??\n defaultRestPath(spec.meta.name, spec.meta.version);\n return path.startsWith('/') ? path : `/${path}`;\n}\n\n/**\n * Convert a SchemaModel to JSON Schema using Zod.\n */\nfunction schemaModelToJsonSchema(\n schema: AnySchemaModel | null\n): OpenApiSchemaObject | null {\n if (!schema) return null;\n return z.toJSONSchema(schema.getZod()) as OpenApiSchemaObject;\n}\n\n/**\n * Extract JSON Schema for a spec's input and output.\n */\nfunction jsonSchemaForSpec(\n spec: ContractSpec<AnySchemaModel, AnySchemaModel>\n): {\n input: OpenApiSchemaObject | null;\n output: OpenApiSchemaObject | null;\n meta: {\n name: string;\n version: number;\n kind: 'command' | 'query';\n description: string;\n tags: string[];\n stability: string;\n };\n} {\n return {\n input: schemaModelToJsonSchema(spec.io.input),\n output: schemaModelToJsonSchema(spec.io.output as AnySchemaModel | null),\n meta: {\n name: spec.meta.name,\n version: spec.meta.version,\n kind: spec.meta.kind,\n description: spec.meta.description,\n tags: spec.meta.tags ?? [],\n stability: spec.meta.stability ?? 'stable',\n },\n };\n}\n\n/**\n * Export a SpecRegistry to an OpenAPI 3.1 document.\n *\n * @param registry - The SpecRegistry containing specs to export\n * @param options - Export options (title, version, description, servers)\n * @returns OpenAPI 3.1 document\n */\nexport function openApiForRegistry(\n registry: SpecRegistry,\n options: OpenApiExportOptions = {}\n): ContractSpecOpenApiDocument {\n const specs = registry\n .listSpecs()\n .filter(\n (s): s is AnyContractSpec =>\n s.meta.kind === 'command' || s.meta.kind === 'query'\n )\n .slice()\n .sort((a, b) => {\n const byName = a.meta.name.localeCompare(b.meta.name);\n return byName !== 0 ? byName : a.meta.version - b.meta.version;\n });\n\n const doc: ContractSpecOpenApiDocument = {\n openapi: '3.1.0',\n info: {\n title: options.title ?? 'ContractSpec API',\n version: options.version ?? '0.0.0',\n ...(options.description ? { description: options.description } : {}),\n },\n ...(options.servers ? { servers: options.servers } : {}),\n paths: {},\n components: { schemas: {} },\n };\n\n for (const spec of specs) {\n const schema = jsonSchemaForSpec(\n spec as unknown as ContractSpec<AnySchemaModel, AnySchemaModel>\n );\n const method = toHttpMethod(spec.meta.kind, spec.transport?.rest?.method);\n const path = toRestPath(spec);\n\n const operationId = toOperationId(spec.meta.name, spec.meta.version);\n\n const pathItem = (doc.paths[path] ??= {});\n const op: Record<string, unknown> = {\n operationId,\n summary: spec.meta.description ?? spec.meta.name,\n description: spec.meta.description,\n tags: spec.meta.tags ?? [],\n 'x-contractspec': {\n name: spec.meta.name,\n version: spec.meta.version,\n kind: spec.meta.kind,\n },\n responses: {},\n };\n\n if (schema.input) {\n const inputName = toSchemaName(\n 'Input',\n spec.meta.name,\n spec.meta.version\n );\n doc.components.schemas[inputName] = schema.input;\n op['requestBody'] = {\n required: true,\n content: {\n 'application/json': {\n schema: { $ref: `#/components/schemas/${inputName}` },\n },\n },\n };\n }\n\n const responses: Record<string, unknown> = {};\n if (schema.output) {\n const outputName = toSchemaName(\n 'Output',\n spec.meta.name,\n spec.meta.version\n );\n doc.components.schemas[outputName] = schema.output;\n responses['200'] = {\n description: 'OK',\n content: {\n 'application/json': {\n schema: { $ref: `#/components/schemas/${outputName}` },\n },\n },\n };\n } else {\n responses['200'] = { description: 'OK' };\n }\n op['responses'] = responses;\n\n pathItem[method] = op;\n }\n\n return doc;\n}\n\n/**\n * Export a SpecRegistry to OpenAPI JSON string.\n */\nexport function openApiToJson(\n registry: SpecRegistry,\n options: OpenApiExportOptions = {}\n): string {\n const doc = openApiForRegistry(registry, options);\n return JSON.stringify(doc, null, 2);\n}\n\n/**\n * Export a SpecRegistry to OpenAPI YAML string.\n */\nexport function openApiToYaml(\n registry: SpecRegistry,\n options: OpenApiExportOptions = {}\n): string {\n const doc = openApiForRegistry(registry, options);\n return jsonToYaml(doc);\n}\n\n/**\n * Simple JSON to YAML conversion.\n */\nfunction jsonToYaml(obj: unknown, indent = 0): string {\n const spaces = ' '.repeat(indent);\n let yaml = '';\n\n if (Array.isArray(obj)) {\n for (const item of obj) {\n if (typeof item === 'object' && item !== null) {\n yaml += `${spaces}-\\n${jsonToYaml(item, indent + 1)}`;\n } else {\n yaml += `${spaces}- ${JSON.stringify(item)}\\n`;\n }\n }\n } else if (typeof obj === 'object' && obj !== null) {\n for (const [key, value] of Object.entries(obj)) {\n if (Array.isArray(value)) {\n yaml += `${spaces}${key}:\\n${jsonToYaml(value, indent + 1)}`;\n } else if (typeof value === 'object' && value !== null) {\n yaml += `${spaces}${key}:\\n${jsonToYaml(value, indent + 1)}`;\n } else {\n yaml += `${spaces}${key}: ${JSON.stringify(value)}\\n`;\n }\n }\n }\n\n return yaml;\n}\n\n// Re-export types for convenience\nexport type {\n OpenApiExportOptions,\n OpenApiServer,\n ContractSpecOpenApiDocument,\n};\n"],"mappings":";;;;;;AAuBA,SAAS,cAAc,MAAc,SAAyB;AAC5D,QAAO,GAAG,KAAK,QAAQ,OAAO,IAAI,CAAC,IAAI;;;;;AAMzC,SAAS,aACP,QACA,MACA,SACQ;AACR,QAAO,GAAG,OA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AAO;;;;;AAMT,SAAgB,cACd,UACA,UAAgC,EAAE,EAC1B;CACR,MAAM,MAAM,mBAAmB,UAAU,QAAQ;AACjD,QAAO,KAAK,UAAU,KAAK,MAAM,EAAE;;;;;AAMrC,SAAgB,cACd,UACA,UAAgC,EAAE,EAC1B;AAER,QAAO,WADK,mBAAmB,UAAU,QAAQ,CAC3B;;;;;AAMxB,SAAS,WAAW,KAAc,SAAS,GAAW;CACpD,MAAM,SAAS,KAAK,OAAO,OAAO;CAClC,IAAI,OAAO;AAEX,KAAI,MAAM,QAAQ,IAAI,CACpB,MAAK,MAAM,QAAQ,IACjB,KAAI,OAAO,SAAS,YAAY,SAAS,KACvC,SAAQ,GAAG,OAAO,KAAK,WAAW,MAAM,SAAS,EAAE;KAEnD,SAAQ,GAAG,OAAO,IAAI,KAAK,UAAU,KAAK,CAAC;UAGtC,OAAO,QAAQ,YAAY,QAAQ,KAC5C,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,IAAI,CAC5C,KAAI,MAAM,QAAQ,MAAM,CACtB,SAAQ,GAAG,SAAS,IAAI,KAAK,WAAW,OAAO,SAAS,EAAE;UACjD,OAAO,UAAU,YAAY,UAAU,KAChD,SAAQ,GAAG,SAAS,IAAI,KAAK,WAAW,OAAO,SAAS,EAAE;KAE1D,SAAQ,GAAG,SAAS,IAAI,IAAI,KAAK,UAAU,MAAM,CAAC;AAKxD,QAAO"}
|
|
1
|
+
{"version":3,"file":"exporter.js","names":["paths: Record<string, Record<string, unknown>>","schemas: Record<string, Record<string, unknown>>","doc: ContractSpecOpenApiDocument","result: ContractSpecExportResult"],"sources":["../../src/openapi/exporter.ts"],"sourcesContent":["/**\n * Export ContractSpec specs to OpenAPI 3.1 format.\n * Refactored to use modular exporters for all surfaces.\n */\nimport type {\n OperationSpecRegistry,\n FeatureRegistry,\n PresentationRegistry,\n FormRegistry,\n DataViewRegistry,\n WorkflowRegistry,\n EventSpec,\n PresentationDescriptorV2,\n} from '@lssm/lib.contracts';\nimport type { AnySchemaModel } from '@lssm/lib.schema';\nimport type {\n ContractSpecOpenApiDocument,\n OpenApiExportOptions,\n OpenApiServer,\n ContractSpecExportOptions,\n ContractSpecExportResult,\n} from './types';\nimport {\n exportOperations,\n generateOperationsRegistry,\n defaultRestPath as defaultRestPathFn,\n} from './exporter/operations';\nimport { exportEvents, generateEventsExports } from './exporter/events';\nimport { exportFeatures, generateFeaturesRegistry } from './exporter/features';\nimport {\n exportPresentations,\n generatePresentationsRegistry,\n} from './exporter/presentations';\nimport { exportForms, generateFormsRegistry } from './exporter/forms';\nimport {\n exportDataViews,\n generateDataViewsRegistry,\n} from './exporter/data-views';\nimport {\n exportWorkflows,\n generateWorkflowsRegistry,\n} from './exporter/workflows';\nimport { generateRegistryIndex } from './exporter/registries';\n\n/**\n * Input registries for unified export.\n */\nexport interface ContractSpecRegistries {\n operations?: OperationSpecRegistry;\n events?: EventSpec<AnySchemaModel>[];\n features?: FeatureRegistry;\n presentations?: PresentationRegistry;\n presentationsV2?: PresentationDescriptorV2[];\n forms?: FormRegistry;\n dataViews?: DataViewRegistry;\n workflows?: WorkflowRegistry;\n}\n\n// Re-export for backwards compatibility\nexport { defaultRestPathFn as defaultRestPath };\n\n/**\n * Export a OperationSpecRegistry to an OpenAPI 3.1 document.\n * @deprecated Use exportContractSpec for full surface support.\n */\nexport function openApiForRegistry(\n registry: OperationSpecRegistry,\n options: OpenApiExportOptions = {}\n): ContractSpecOpenApiDocument {\n const { paths, schemas } = exportOperations(registry);\n\n return {\n openapi: '3.1.0',\n info: {\n title: options.title ?? 'ContractSpec API',\n version: options.version ?? '0.0.0',\n ...(options.description ? { description: options.description } : {}),\n },\n ...(options.servers ? { servers: options.servers } : {}),\n paths,\n components: { schemas },\n };\n}\n\n/**\n * Export all ContractSpec surfaces to OpenAPI document with extensions.\n */\nexport function exportContractSpec(\n registries: ContractSpecRegistries,\n options: ContractSpecExportOptions = {}\n): ContractSpecExportResult {\n const {\n operations: includeOps = true,\n events: includeEvents = true,\n features: includeFeatures = true,\n presentations: includePresentations = true,\n forms: includeForms = true,\n dataViews: includeDataViews = true,\n workflows: includeWorkflows = true,\n generateRegistries = true,\n } = options;\n\n // Build OpenAPI document\n let paths: Record<string, Record<string, unknown>> = {};\n let schemas: Record<string, Record<string, unknown>> = {};\n\n // Export operations\n if (includeOps && registries.operations) {\n const opResult = exportOperations(registries.operations);\n paths = opResult.paths;\n schemas = opResult.schemas;\n }\n\n const doc: ContractSpecOpenApiDocument = {\n openapi: '3.1.0',\n info: {\n title: options.title ?? 'ContractSpec API',\n version: options.version ?? '0.0.0',\n ...(options.description ? { description: options.description } : {}),\n },\n ...(options.servers ? { servers: options.servers } : {}),\n paths,\n components: { schemas },\n };\n\n // Add extensions for other surfaces\n if (includeEvents && registries.events?.length) {\n doc['x-contractspec-events'] = exportEvents(registries.events);\n }\n\n if (includeFeatures && registries.features) {\n doc['x-contractspec-features'] = exportFeatures(registries.features);\n }\n\n if (includePresentations && registries.presentations) {\n const presExport = exportPresentations(\n registries.presentations,\n registries.presentationsV2\n );\n doc['x-contractspec-presentations'] = [\n ...presExport.v1,\n ...presExport.v2,\n ];\n }\n\n if (includeForms && registries.forms) {\n doc['x-contractspec-forms'] = exportForms(registries.forms);\n }\n\n if (includeDataViews && registries.dataViews) {\n doc['x-contractspec-dataviews'] = exportDataViews(registries.dataViews);\n }\n\n if (includeWorkflows && registries.workflows) {\n doc['x-contractspec-workflows'] = exportWorkflows(registries.workflows);\n }\n\n const result: ContractSpecExportResult = {\n openApi: doc,\n };\n\n // Generate registry code if requested\n if (generateRegistries) {\n result.registries = {};\n\n if (includeOps && registries.operations) {\n result.registries.operations = generateOperationsRegistry(\n registries.operations\n );\n }\n\n if (includeEvents && registries.events?.length) {\n result.registries.events = generateEventsExports(registries.events);\n }\n\n if (includeFeatures && registries.features) {\n result.registries.features = generateFeaturesRegistry(registries.features);\n }\n\n if (includePresentations && registries.presentations) {\n result.registries.presentations = generatePresentationsRegistry(\n registries.presentations\n );\n }\n\n if (includeForms && registries.forms) {\n result.registries.forms = generateFormsRegistry(registries.forms);\n }\n\n if (includeDataViews && registries.dataViews) {\n result.registries.dataViews = generateDataViewsRegistry(\n registries.dataViews\n );\n }\n\n if (includeWorkflows && registries.workflows) {\n result.registries.workflows = generateWorkflowsRegistry(\n registries.workflows\n );\n }\n\n // Generate index file\n result.registries.index = generateRegistryIndex({\n operations: includeOps && !!registries.operations,\n events: includeEvents && !!registries.events?.length,\n features: includeFeatures && !!registries.features,\n presentations: includePresentations && !!registries.presentations,\n forms: includeForms && !!registries.forms,\n dataViews: includeDataViews && !!registries.dataViews,\n workflows: includeWorkflows && !!registries.workflows,\n });\n }\n\n return result;\n}\n\n/**\n * Export a OperationSpecRegistry to OpenAPI JSON string.\n */\nexport function openApiToJson(\n registry: OperationSpecRegistry,\n options: OpenApiExportOptions = {}\n): string {\n const doc = openApiForRegistry(registry, options);\n return JSON.stringify(doc, null, 2);\n}\n\n/**\n * Export a OperationSpecRegistry to OpenAPI YAML string.\n */\nexport function openApiToYaml(\n registry: OperationSpecRegistry,\n options: OpenApiExportOptions = {}\n): string {\n const doc = openApiForRegistry(registry, options);\n return jsonToYaml(doc);\n}\n\n/**\n * Export ContractSpec to JSON string (all surfaces).\n */\nexport function contractSpecToJson(\n registries: ContractSpecRegistries,\n options: ContractSpecExportOptions = {}\n): string {\n const result = exportContractSpec(registries, options);\n return JSON.stringify(result.openApi, null, 2);\n}\n\n/**\n * Export ContractSpec to YAML string (all surfaces).\n */\nexport function contractSpecToYaml(\n registries: ContractSpecRegistries,\n options: ContractSpecExportOptions = {}\n): string {\n const result = exportContractSpec(registries, options);\n return jsonToYaml(result.openApi);\n}\n\n/**\n * Simple JSON to YAML conversion.\n */\nfunction jsonToYaml(obj: unknown, indent = 0): string {\n const spaces = ' '.repeat(indent);\n let yaml = '';\n\n if (Array.isArray(obj)) {\n for (const item of obj) {\n if (typeof item === 'object' && item !== null) {\n yaml += `${spaces}-\\n${jsonToYaml(item, indent + 1)}`;\n } else {\n yaml += `${spaces}- ${JSON.stringify(item)}\\n`;\n }\n }\n } else if (typeof obj === 'object' && obj !== null) {\n for (const [key, value] of Object.entries(obj)) {\n if (Array.isArray(value)) {\n yaml += `${spaces}${key}:\\n${jsonToYaml(value, indent + 1)}`;\n } else if (typeof value === 'object' && value !== null) {\n yaml += `${spaces}${key}:\\n${jsonToYaml(value, indent + 1)}`;\n } else {\n yaml += `${spaces}${key}: ${JSON.stringify(value)}\\n`;\n }\n }\n }\n\n return yaml;\n}\n\n// Re-export types for convenience\nexport type {\n OpenApiExportOptions,\n OpenApiServer,\n ContractSpecOpenApiDocument,\n ContractSpecExportOptions,\n ContractSpecExportResult,\n};\n"],"mappings":";;;;;;;;;;;;;;AAiEA,SAAgB,mBACd,UACA,UAAgC,EAAE,EACL;CAC7B,MAAM,EAAE,OAAO,YAAY,iBAAiB,SAAS;AAErD,QAAO;EACL,SAAS;EACT,MAAM;GACJ,OAAO,QAAQ,SAAS;GACxB,SAAS,QAAQ,WAAW;GAC5B,GAAI,QAAQ,cAAc,EAAE,aAAa,QAAQ,aAAa,GAAG,EAAE;GACpE;EACD,GAAI,QAAQ,UAAU,EAAE,SAAS,QAAQ,SAAS,GAAG,EAAE;EACvD;EACA,YAAY,EAAE,SAAS;EACxB;;;;;AAMH,SAAgB,mBACd,YACA,UAAqC,EAAE,EACb;CAC1B,MAAM,EACJ,YAAY,aAAa,MACzB,QAAQ,gBAAgB,MACxB,UAAU,kBAAkB,MAC5B,eAAe,uBAAuB,MACtC,OAAO,eAAe,MACtB,WAAW,mBAAmB,MAC9B,WAAW,mBAAmB,MAC9B,qBAAqB,SACnB;CAGJ,IAAIA,QAAiD,EAAE;CACvD,IAAIC,UAAmD,EAAE;AAGzD,KAAI,cAAc,WAAW,YAAY;EACvC,MAAM,WAAW,iBAAiB,WAAW,WAAW;AACxD,UAAQ,SAAS;AACjB,YAAU,SAAS;;CAGrB,MAAMC,MAAmC;EACvC,SAAS;EACT,MAA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AAK,UAAU,OAAO,QAAQ,IAAI,CAC5C,KAAI,MAAM,QAAQ,MAAM,CACtB,SAAQ,GAAG,SAAS,IAAI,KAAK,WAAW,OAAO,SAAS,EAAE;UACjD,OAAO,UAAU,YAAY,UAAU,KAChD,SAAQ,GAAG,SAAS,IAAI,KAAK,WAAW,OAAO,SAAS,EAAE;KAE1D,SAAQ,GAAG,SAAS,IAAI,IAAI,KAAK,UAAU,MAAM,CAAC;AAKxD,QAAO"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/openapi/importer/analyzer.ts
|
|
2
|
+
/**
|
|
3
|
+
* HTTP methods that typically indicate a command (state-changing).
|
|
4
|
+
*/
|
|
5
|
+
const COMMAND_METHODS = [
|
|
6
|
+
"post",
|
|
7
|
+
"put",
|
|
8
|
+
"delete",
|
|
9
|
+
"patch"
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* Determine if an operation is a command or query based on HTTP method.
|
|
13
|
+
*/
|
|
14
|
+
function inferOpKind(method) {
|
|
15
|
+
return COMMAND_METHODS.includes(method.toLowerCase()) ? "command" : "query";
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Determine auth level based on security requirements.
|
|
19
|
+
*/
|
|
20
|
+
function inferAuthLevel(operation, defaultAuth) {
|
|
21
|
+
if (!operation.security || operation.security.length === 0) return defaultAuth;
|
|
22
|
+
for (const sec of operation.security) if (Object.keys(sec).length === 0) return "anonymous";
|
|
23
|
+
return "user";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { COMMAND_METHODS, inferAuthLevel, inferOpKind };
|
|
28
|
+
//# sourceMappingURL=analyzer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analyzer.js","names":[],"sources":["../../../src/openapi/importer/analyzer.ts"],"sourcesContent":["import type { ParsedOperation } from '../types';\n\n/**\n * HTTP methods that typically indicate a command (state-changing).\n */\nexport const COMMAND_METHODS = ['post', 'put', 'delete', 'patch'];\n\n/**\n * Determine if an operation is a command or query based on HTTP method.\n */\nexport function inferOpKind(method: string): 'command' | 'query' {\n return COMMAND_METHODS.includes(method.toLowerCase()) ? 'command' : 'query';\n}\n\n/**\n * Determine auth level based on security requirements.\n */\nexport function inferAuthLevel(\n operation: ParsedOperation,\n defaultAuth: 'anonymous' | 'user' | 'admin'\n): 'anonymous' | 'user' | 'admin' {\n if (!operation.security || operation.security.length === 0) {\n // Check if any security scheme is present\n return defaultAuth;\n }\n\n // If there's an empty security requirement, it's anonymous\n for (const sec of operation.security) {\n if (Object.keys(sec).length === 0) {\n return 'anonymous';\n }\n }\n\n return 'user';\n}\n"],"mappings":";;;;AAKA,MAAa,kBAAkB;CAAC;CAAQ;CAAO;CAAU;CAAQ;;;;AAKjE,SAAgB,YAAY,QAAqC;AAC/D,QAAO,gBAAgB,SAAS,OAAO,aAAa,CAAC,GAAG,YAAY;;;;;AAMtE,SAAgB,eACd,WACA,aACgC;AAChC,KAAI,CAAC,UAAU,YAAY,UAAU,SAAS,WAAW,EAEvD,QAAO;AAIT,MAAK,MAAM,OAAO,UAAU,SAC1B,KAAI,OAAO,KAAK,IAAI,CAAC,WAAW,EAC9B,QAAO;AAIX,QAAO"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { toPascalCase, toValidIdentifier } from "../../common/utils.js";
|
|
2
|
+
import { generateImports, generateSchemaModelCode } from "../schema-converter.js";
|
|
3
|
+
|
|
4
|
+
//#region src/openapi/importer/events.ts
|
|
5
|
+
/**
|
|
6
|
+
* Generate code for an event.
|
|
7
|
+
*/
|
|
8
|
+
function generateEventCode(event, options) {
|
|
9
|
+
const eventName = toValidIdentifier(event.name);
|
|
10
|
+
const modelName = toPascalCase(eventName) + "Payload";
|
|
11
|
+
const payloadModel = generateSchemaModelCode(event.payload, modelName);
|
|
12
|
+
const imports = /* @__PURE__ */ new Set();
|
|
13
|
+
imports.add("import { defineEvent, type EventSpec } from '@lssm/lib.contracts';");
|
|
14
|
+
generateImports(payloadModel.fields, options).split("\n").filter(Boolean).forEach((i) => imports.add(i));
|
|
15
|
+
if (payloadModel.name !== modelName) {
|
|
16
|
+
const modelsDir = `../${options.conventions.models}`;
|
|
17
|
+
const kebabName = payloadModel.name.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
18
|
+
imports.add(`import { ${payloadModel.name} } from '${modelsDir}/${kebabName}';`);
|
|
19
|
+
}
|
|
20
|
+
return `
|
|
21
|
+
${Array.from(imports).join("\n")}
|
|
22
|
+
|
|
23
|
+
${payloadModel.code}
|
|
24
|
+
|
|
25
|
+
export const ${eventName} = defineEvent({
|
|
26
|
+
name: '${event.name}',
|
|
27
|
+
version: 1,
|
|
28
|
+
description: ${JSON.stringify(event.description ?? "")},
|
|
29
|
+
payload: ${payloadModel.name},
|
|
30
|
+
});
|
|
31
|
+
`.trim();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { generateEventCode };
|
|
36
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","names":[],"sources":["../../../src/openapi/importer/events.ts"],"sourcesContent":["import type { ParsedEvent } from '../types';\nimport { generateImports, generateSchemaModelCode } from '../schema-converter';\nimport { toPascalCase, toValidIdentifier } from '../../common/utils';\nimport type { ContractsrcConfig } from '@lssm/lib.contracts';\n\n/**\n * Generate code for an event.\n */\nexport function generateEventCode(\n event: ParsedEvent,\n options: ContractsrcConfig\n): string {\n const eventName = toValidIdentifier(event.name);\n const modelName = toPascalCase(eventName) + 'Payload';\n\n // Generate payload model inline or referenced?\n // Let's generate the payload schema definition first\n const payloadModel = generateSchemaModelCode(event.payload, modelName);\n\n const imports = new Set<string>();\n imports.add(\n \"import { defineEvent, type EventSpec } from '@lssm/lib.contracts';\"\n );\n\n const modelImports = generateImports(payloadModel.fields, options);\n // Merge imports - this is a bit hacky string manipulation but works for now\n modelImports\n .split('\\n')\n .filter(Boolean)\n .forEach((i) => imports.add(i));\n\n // If payloadModel is a reference (empty fields and different name), import it\n if (payloadModel.name !== modelName) {\n const modelsDir = `../${options.conventions.models}`;\n const kebabName = payloadModel.name\n .replace(/([a-z0-9])([A-Z])/g, '$1-$2')\n .toLowerCase();\n imports.add(\n `import { ${payloadModel.name} } from '${modelsDir}/${kebabName}';`\n );\n }\n\n const allImports = Array.from(imports).join('\\n');\n\n return `\n${allImports}\n\n${payloadModel.code}\n\nexport const ${eventName} = defineEvent({\n name: '${event.name}',\n version: 1,\n description: ${JSON.stringify(event.description ?? '')},\n payload: ${payloadModel.name},\n});\n`.trim();\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,kBACd,OACA,SACQ;CACR,MAAM,YAAY,kBAAkB,MAAM,KAAK;CAC/C,MAAM,YAAY,aAAa,UAAU,GAAG;CAI5C,MAAM,eAAe,wBAAwB,MAAM,SAAS,UAAU;CAEtE,MAAM,0BAAU,IAAI,KAAa;AACjC,SAAQ,IACN,qEACD;AAID,CAFqB,gBAAgB,aAAa,QAAQ,QAAQ,CAG/D,MAAM,KAAK,CACX,OAAO,QAAQ,CACf,SAAS,MAAM,QAAQ,IAAI,EAAE,CAAC;AAGjC,KAAI,aAAa,SAAS,WAAW;EACnC,MAAM,YAAY,MAAM,QAAQ,YAAY;EAC5C,MAAM,YAAY,aAAa,KAC5B,QAAQ,sBAAsB,QAAQ,CACtC,aAAa;AAChB,UAAQ,IACN,YAAY,aAAa,KAAK,WAAW,UAAU,GAAG,UAAU,IACjE;;AAKH,QAAO;EAFY,MAAM,KAAK,QAAQ,CAAC,KAAK,KAAK,CAGtC;;EAEX,aAAa,KAAK;;eAEL,UAAU;WACd,MAAM,KAAK;;iBAEL,KAAK,UAAU,MAAM,eAAe,GAAG,CAAC;aAC5C,aAAa,KAAK;;EAE7B,MAAM"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { toPascalCase, toSpecName, toValidIdentifier } from "../../common/utils.js";
|
|
2
|
+
import { generateImports } from "../schema-converter.js";
|
|
3
|
+
import { inferAuthLevel, inferOpKind } from "./analyzer.js";
|
|
4
|
+
|
|
5
|
+
//#region src/openapi/importer/generator.ts
|
|
6
|
+
/**
|
|
7
|
+
* Generate ContractSpec TypeScript code for an operation.
|
|
8
|
+
*/
|
|
9
|
+
function generateSpecCode(operation, contractspecConfig, options = {}, inputModel, outputModel) {
|
|
10
|
+
const specName = toSpecName(operation.operationId, options.prefix);
|
|
11
|
+
const kind = inferOpKind(operation.method);
|
|
12
|
+
const auth = inferAuthLevel(operation, options.defaultAuth ?? "user");
|
|
13
|
+
const lines = [];
|
|
14
|
+
lines.push("import { defineCommand, defineQuery } from '@lssm/lib.contracts';");
|
|
15
|
+
if (inputModel || outputModel) lines.push(generateImports([...inputModel?.fields ?? [], ...outputModel?.fields ?? []], contractspecConfig, false));
|
|
16
|
+
lines.push("");
|
|
17
|
+
if (inputModel && inputModel.code) {
|
|
18
|
+
lines.push("// Input schema");
|
|
19
|
+
lines.push(inputModel.code);
|
|
20
|
+
lines.push("");
|
|
21
|
+
}
|
|
22
|
+
if (outputModel && outputModel.code) {
|
|
23
|
+
lines.push("// Output schema");
|
|
24
|
+
lines.push(outputModel.code);
|
|
25
|
+
lines.push("");
|
|
26
|
+
}
|
|
27
|
+
const defineFunc = kind === "command" ? "defineCommand" : "defineQuery";
|
|
28
|
+
const safeName = toValidIdentifier(toPascalCase(operation.operationId));
|
|
29
|
+
lines.push(`/**`);
|
|
30
|
+
lines.push(` * ${operation.summary ?? operation.operationId}`);
|
|
31
|
+
if (operation.description) {
|
|
32
|
+
lines.push(` *`);
|
|
33
|
+
lines.push(` * ${operation.description}`);
|
|
34
|
+
}
|
|
35
|
+
lines.push(` *`);
|
|
36
|
+
lines.push(` * @source OpenAPI: ${operation.method.toUpperCase()} ${operation.path}`);
|
|
37
|
+
lines.push(` */`);
|
|
38
|
+
lines.push(`export const ${safeName}Spec = ${defineFunc}({`);
|
|
39
|
+
lines.push(" meta: {");
|
|
40
|
+
lines.push(` name: '${specName}',`);
|
|
41
|
+
lines.push(" version: 1,");
|
|
42
|
+
lines.push(` stability: '${options.defaultStability ?? "stable"}',`);
|
|
43
|
+
lines.push(` owners: [${(options.defaultOwners ?? []).map((o) => `'${o}'`).join(", ")}],`);
|
|
44
|
+
lines.push(` tags: [${operation.tags.map((t) => `'${t}'`).join(", ")}],`);
|
|
45
|
+
lines.push(` description: ${JSON.stringify(operation.summary ?? operation.operationId)},`);
|
|
46
|
+
lines.push(` goal: ${JSON.stringify(operation.description ?? "Imported from OpenAPI")},`);
|
|
47
|
+
lines.push(` context: 'Imported from OpenAPI: ${operation.method.toUpperCase()} ${operation.path}',`);
|
|
48
|
+
lines.push(" },");
|
|
49
|
+
lines.push(" io: {");
|
|
50
|
+
if (inputModel) lines.push(` input: ${inputModel.name},`);
|
|
51
|
+
else lines.push(" input: null,");
|
|
52
|
+
if (outputModel) lines.push(` output: ${outputModel.name},`);
|
|
53
|
+
else lines.push(" output: null, // TODO: Define output schema");
|
|
54
|
+
lines.push(" },");
|
|
55
|
+
lines.push(" policy: {");
|
|
56
|
+
lines.push(` auth: '${auth}',`);
|
|
57
|
+
lines.push(" },");
|
|
58
|
+
const restMethod = operation.method.toUpperCase() === "GET" ? "GET" : "POST";
|
|
59
|
+
lines.push(" transport: {");
|
|
60
|
+
lines.push(" rest: {");
|
|
61
|
+
lines.push(` method: '${restMethod}',`);
|
|
62
|
+
lines.push(` path: '${operation.path}',`);
|
|
63
|
+
lines.push(" },");
|
|
64
|
+
lines.push(" },");
|
|
65
|
+
lines.push("});");
|
|
66
|
+
return lines.join("\n");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
export { generateSpecCode };
|
|
71
|
+
//# sourceMappingURL=generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.js","names":["lines: string[]"],"sources":["../../../src/openapi/importer/generator.ts"],"sourcesContent":["import type { ParsedOperation } from '../types';\nimport {\n toPascalCase,\n toSpecName,\n toValidIdentifier,\n} from '../../common/utils';\nimport { type GeneratedModel, generateImports } from '../schema-converter';\nimport { inferAuthLevel, inferOpKind } from './analyzer';\nimport type {\n ContractsrcConfig,\n OpenApiSourceConfig,\n} from '@lssm/lib.contracts';\n\n/**\n * Generate ContractSpec TypeScript code for an operation.\n */\nexport function generateSpecCode(\n operation: ParsedOperation,\n contractspecConfig: ContractsrcConfig,\n options: Partial<OpenApiSourceConfig> = {},\n inputModel: GeneratedModel | null,\n outputModel: GeneratedModel | null\n): string {\n const specName = toSpecName(operation.operationId, options.prefix);\n const kind = inferOpKind(operation.method);\n const auth = inferAuthLevel(operation, options.defaultAuth ?? 'user');\n\n const lines: string[] = [];\n\n // Imports\n lines.push(\n \"import { defineCommand, defineQuery } from '@lssm/lib.contracts';\"\n );\n if (inputModel || outputModel) {\n lines.push(\n generateImports(\n [...(inputModel?.fields ?? []), ...(outputModel?.fields ?? [])],\n contractspecConfig,\n false // operations import from ../models, not same directory\n )\n );\n }\n lines.push('');\n\n // Generate input model if present\n if (inputModel && inputModel.code) {\n lines.push('// Input schema');\n lines.push(inputModel.code);\n lines.push('');\n }\n\n // Generate output model if present\n if (outputModel && outputModel.code) {\n lines.push('// Output schema');\n lines.push(outputModel.code);\n lines.push('');\n }\n\n // Generate spec\n const defineFunc = kind === 'command' ? 'defineCommand' : 'defineQuery';\n const safeName = toValidIdentifier(toPascalCase(operation.operationId));\n\n lines.push(`/**`);\n lines.push(` * ${operation.summary ?? operation.operationId}`);\n if (operation.description) {\n lines.push(` *`);\n lines.push(` * ${operation.description}`);\n }\n lines.push(` *`);\n lines.push(\n ` * @source OpenAPI: ${operation.method.toUpperCase()} ${operation.path}`\n );\n lines.push(` */`);\n lines.push(`export const ${safeName}Spec = ${defineFunc}({`);\n\n // Meta\n lines.push(' meta: {');\n lines.push(` name: '${specName}',`);\n lines.push(' version: 1,');\n lines.push(` stability: '${options.defaultStability ?? 'stable'}',`);\n lines.push(\n ` owners: [${(options.defaultOwners ?? []).map((o) => `'${o}'`).join(', ')}],`\n );\n lines.push(` tags: [${operation.tags.map((t) => `'${t}'`).join(', ')}],`);\n lines.push(\n ` description: ${JSON.stringify(operation.summary ?? operation.operationId)},`\n );\n lines.push(\n ` goal: ${JSON.stringify(operation.description ?? 'Imported from OpenAPI')},`\n );\n lines.push(\n ` context: 'Imported from OpenAPI: ${operation.method.toUpperCase()} ${operation.path}',`\n );\n lines.push(' },');\n\n // IO\n lines.push(' io: {');\n if (inputModel) {\n lines.push(` input: ${inputModel.name},`);\n } else {\n lines.push(' input: null,');\n }\n if (outputModel) {\n lines.push(` output: ${outputModel.name},`);\n } else {\n lines.push(' output: null, // TODO: Define output schema');\n }\n lines.push(' },');\n\n // Policy\n lines.push(' policy: {');\n lines.push(` auth: '${auth}',`);\n lines.push(' },');\n\n // Transport hints\n // ContractSpec only supports GET and POST - map other methods appropriately\n const httpMethod = operation.method.toUpperCase();\n const restMethod = httpMethod === 'GET' ? 'GET' : 'POST'; // GET stays GET, everything else becomes POST\n lines.push(' transport: {');\n lines.push(' rest: {');\n lines.push(` method: '${restMethod}',`);\n lines.push(` path: '${operation.path}',`);\n lines.push(' },');\n lines.push(' },');\n\n lines.push('});');\n\n return lines.join('\\n');\n}\n"],"mappings":";;;;;;;;AAgBA,SAAgB,iBACd,WACA,oBACA,UAAwC,EAAE,EAC1C,YACA,aACQ;CACR,MAAM,WAAW,WAAW,UAAU,aAAa,QAAQ,OAAO;CAClE,MAAM,OAAO,YAAY,UAAU,OAAO;CAC1C,MAAM,OAAO,eAAe,WAAW,QAAQ,eAAe,OAAO;CAErE,MAAMA,QAAkB,EAAE;AAG1B,OAAM,KACJ,oEACD;AACD,KAAI,cAAc,YAChB,OAAM,KACJ,gBACE,CAAC,GAAI,YAAY,UAAU,EAAE,EAAG,GAAI,aAAa,UAAU,EAAE,CAAE,EAC/D,oBACA,MACD,CACF;AAEH,OAAM,KAAK,GAAG;AAG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K,OAAO;AAGlB,OAAM,KAAK,UAAU;AACrB,KAAI,WACF,OAAM,KAAK,cAAc,WAAW,KAAK,GAAG;KAE5C,OAAM,KAAK,mBAAmB;AAEhC,KAAI,YACF,OAAM,KAAK,eAAe,YAAY,KAAK,GAAG;KAE9C,OAAM,KAAK,kDAAkD;AAE/D,OAAM,KAAK,OAAO;AAGlB,OAAM,KAAK,cAAc;AACzB,OAAM,KAAK,cAAc,KAAK,IAAI;AAClC,OAAM,KAAK,OAAO;CAKlB,MAAM,aADa,UAAU,OAAO,aAAa,KACf,QAAQ,QAAQ;AAClD,OAAM,KAAK,iBAAiB;AAC5B,OAAM,KAAK,cAAc;AACzB,OAAM,KAAK,kBAAkB,WAAW,IAAI;AAC5C,OAAM,KAAK,gBAAgB,UAAU,KAAK,IAAI;AAC9C,OAAM,KAAK,SAAS;AACpB,OAAM,KAAK,OAAO;AAElB,OAAM,KAAK,MAAM;AAEjB,QAAO,MAAM,KAAK,KAAK"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//#region src/openapi/importer/grouping.ts
|
|
2
|
+
/**
|
|
3
|
+
* Resolve the group folder for an operation based on grouping config.
|
|
4
|
+
*/
|
|
5
|
+
function resolveOperationGroupFolder(operation, conventions) {
|
|
6
|
+
const groupingRule = conventions.operationsGrouping;
|
|
7
|
+
if (!groupingRule || groupingRule.strategy === "none") return "";
|
|
8
|
+
return applyGroupingStrategy(groupingRule, {
|
|
9
|
+
name: operation.operationId,
|
|
10
|
+
tags: operation.tags,
|
|
11
|
+
path: operation.path
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Resolve the group folder for a model based on grouping config.
|
|
16
|
+
*/
|
|
17
|
+
function resolveModelGroupFolder(modelName, conventions, relatedPath, relatedTags) {
|
|
18
|
+
const groupingRule = conventions.modelsGrouping;
|
|
19
|
+
if (!groupingRule || groupingRule.strategy === "none") return "";
|
|
20
|
+
return applyGroupingStrategy(groupingRule, {
|
|
21
|
+
name: modelName,
|
|
22
|
+
tags: relatedTags ?? [],
|
|
23
|
+
path: relatedPath
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Resolve the group folder for an event based on grouping config.
|
|
28
|
+
*/
|
|
29
|
+
function resolveEventGroupFolder(eventName, conventions, relatedTags) {
|
|
30
|
+
const groupingRule = conventions.eventsGrouping;
|
|
31
|
+
if (!groupingRule || groupingRule.strategy === "none") return "";
|
|
32
|
+
return applyGroupingStrategy(groupingRule, {
|
|
33
|
+
name: eventName,
|
|
34
|
+
tags: relatedTags ?? []
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Apply grouping strategy to extract folder name.
|
|
39
|
+
*/
|
|
40
|
+
function applyGroupingStrategy(rule, context) {
|
|
41
|
+
switch (rule.strategy) {
|
|
42
|
+
case "by-tag": return context.tags?.[0] ?? "untagged";
|
|
43
|
+
case "by-owner": return context.owners?.[0] ?? "unowned";
|
|
44
|
+
case "by-domain": return extractDomain(context.name);
|
|
45
|
+
case "by-url-path-single": return extractUrlPathLevel(context.path, 1);
|
|
46
|
+
case "by-url-path-multi": return extractUrlPathLevel(context.path, rule.urlPathLevel ?? 2);
|
|
47
|
+
case "by-feature": return extractDomain(context.name);
|
|
48
|
+
case "none":
|
|
49
|
+
default: return "";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Extract domain from operation/model name.
|
|
54
|
+
* e.g., "users.create" -> "users"
|
|
55
|
+
*/
|
|
56
|
+
function extractDomain(name) {
|
|
57
|
+
if (name.includes(".")) return name.split(".")[0] ?? "default";
|
|
58
|
+
if (name.includes("_")) return name.split("_")[0] ?? "default";
|
|
59
|
+
return name.match(/^([a-z]+)/i)?.[1]?.toLowerCase() ?? "default";
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Extract URL path segments for grouping.
|
|
63
|
+
*/
|
|
64
|
+
function extractUrlPathLevel(path, level) {
|
|
65
|
+
if (!path) return "root";
|
|
66
|
+
const nonParamSegments = path.split("/").filter(Boolean).filter((s) => !s.startsWith("{"));
|
|
67
|
+
if (nonParamSegments.length === 0) return "root";
|
|
68
|
+
return nonParamSegments.slice(0, level).join("/");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
export { resolveEventGroupFolder, resolveModelGroupFolder, resolveOperationGroupFolder };
|
|
73
|
+
//# sourceMappingURL=grouping.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grouping.js","names":[],"sources":["../../../src/openapi/importer/grouping.ts"],"sourcesContent":["/**\n * Grouping utilities for OpenAPI import/export.\n * Determines output folder structure based on configuration.\n */\nimport type {\n FolderConventions,\n} from '@lssm/lib.contracts';\nimport type { ParsedOperation } from '../types';\n\n/**\n * Grouping strategy type (matches ContractSpec config).\n */\ntype GroupingStrategy =\n | 'by-tag'\n | 'by-owner'\n | 'by-domain'\n | 'by-url-path-single'\n | 'by-url-path-multi'\n | 'by-feature'\n | 'none';\n\n/**\n * Grouping rule configuration.\n */\ninterface GroupingRule {\n strategy: GroupingStrategy;\n urlPathLevel?: number;\n pattern?: string;\n}\n\n/**\n * Resolve the group folder for an operation based on grouping config.\n */\nexport function resolveOperationGroupFolder(\n operation: ParsedOperation,\n conventions: FolderConventions\n): string {\n const groupingRule = conventions.operationsGrouping as GroupingRule | undefined;\n \n if (!groupingRule || groupingRule.strategy === 'none') {\n return '';\n }\n\n return applyGroupingStrategy(groupingRule, {\n name: operation.operationId,\n tags: operation.tags,\n path: operation.path,\n });\n}\n\n/**\n * Resolve the group folder for a model based on grouping config.\n */\nexport function resolveModelGroupFolder(\n modelName: string,\n conventions: FolderConventions,\n relatedPath?: string,\n relatedTags?: string[]\n): string {\n const groupingRule = conventions.modelsGrouping as GroupingRule | undefined;\n \n if (!groupingRule || groupingRule.strategy === 'none') {\n return '';\n }\n\n return applyGroupingStrategy(groupingRule, {\n name: modelName,\n tags: relatedTags ?? [],\n path: relatedPath,\n });\n}\n\n/**\n * Resolve the group folder for an event based on grouping config.\n */\nexport function resolveEventGroupFolder(\n eventName: string,\n conventions: FolderConventions,\n relatedTags?: string[]\n): string {\n const groupingRule = conventions.eventsGrouping as GroupingRule | undefined;\n \n if (!groupingRule || groupingRule.strategy === 'none') {\n return '';\n }\n\n return applyGroupingStrategy(groupingRule, {\n name: eventName,\n tags: relatedTags ?? [],\n });\n}\n\n/**\n * Apply grouping strategy to extract folder name.\n */\nfunction applyGroupingStrategy(\n rule: GroupingRule,\n context: {\n name: string;\n tags?: string[];\n path?: string;\n owners?: string[];\n }\n): string {\n switch (rule.strategy) {\n case 'by-tag':\n return context.tags?.[0] ?? 'untagged';\n\n case 'by-owner':\n return context.owners?.[0] ?? 'unowned';\n\n case 'by-domain':\n return extractDomain(context.name);\n\n case 'by-url-path-single':\n return extractUrlPathLevel(context.path, 1);\n\n case 'by-url-path-multi':\n return extractUrlPathLevel(context.path, rule.urlPathLevel ?? 2);\n\n case 'by-feature':\n // Use domain extraction for feature grouping\n return extractDomain(context.name);\n\n case 'none':\n default:\n return '';\n }\n}\n\n/**\n * Extract domain from operation/model name.\n * e.g., \"users.create\" -> \"users\"\n */\nfunction extractDomain(name: string): string {\n // Handle camelCase/PascalCase names\n if (name.includes('.')) {\n return name.split('.')[0] ?? 'default';\n }\n if (name.includes('_')) {\n return name.split('_')[0] ?? 'default';\n }\n // Extract from camelCase like \"createUser\" -> \"create\"\n const match = name.match(/^([a-z]+)/i);\n return match?.[1]?.toLowerCase() ?? 'default';\n}\n\n/**\n * Extract URL path segments for grouping.\n */\nfunction extractUrlPathLevel(path: string | undefined, level: number): string {\n if (!path) return 'root';\n const segments = path.split('/').filter(Boolean);\n \n // Filter out path parameters like {id}\n const nonParamSegments = segments.filter((s) => !s.startsWith('{'));\n \n if (nonParamSegments.length === 0) return 'root';\n \n return nonParamSegments.slice(0, level).join('/');\n}\n\n/**\n * Build full output path including group folder.\n */\nexport function buildOutputPath(\n baseDir: string,\n surfaceDir: string,\n groupFolder: string,\n fileName: string\n): string {\n const parts = [baseDir, surfaceDir];\n \n if (groupFolder) {\n parts.push(groupFolder);\n }\n \n parts.push(fileName);\n \n return parts.filter(Boolean).join('/');\n}\n\n/**\n * Determine if feature-based grouping should be applied.\n */\nexport function shouldGroupByFeature(conventions: FolderConventions): boolean {\n return conventions.groupByFeature;\n}\n"],"mappings":";;;;AAiCA,SAAgB,4BACd,WACA,aACQ;CACR,MAAM,eAAe,YAAY;AAEjC,KAAI,CAAC,gBAAgB,aAAa,aAAa,OAC7C,QAAO;AAGT,QAAO,sBAAsB,cAAc;EACzC,MAAM,UAAU;EAChB,MAAM,UAAU;EAChB,MAAM,UAAU;EACjB,CAAC;;;;;AAMJ,SAAgB,wBACd,WACA,aACA,aACA,aACQ;CACR,MAAM,eAAe,YAAY;AAEjC,KAAI,CAAC,gBAAgB,aAAa,aAAa,OAC7C,QAAO;AAGT,QAAO,sBAAsB,cAAc;EACzC,MAAM;EACN,MAAM,eAAe,EAAE;EACvB,MAAM;EACP,CAAC;;;;;AAMJ,SAAgB,wBACd,WACA,aACA,aACQ;CACR,MAAM,eAAe,YAAY;AAEjC,KAAI,CAAC,gBAAgB,aAAa,aAAa,OAC7C,QAAO;AAGT,QAAO,sBAAsB,cAAc;EACzC,MAAM;EACN,MAAM,eAAe,EAAE;EACxB,CAAC;;;;;AAMJ,SAAS,sBACP,MACA,SAMQ;AACR,SAAQ,KAAK,UAAb;EACE,KAAK,SACH,QAAO,QAAQ,OAAO,MAAM;EAE9B,KAAK,WACH,QAAO,QAAQ,SAAS,MAAM;EAEhC,KAAK,YACH,QAAO,cAAc,QAAQ,KAAK;EAEpC,KAAK,qBACH,QAAO,oBAAoB,QAAQ,MAAM,EAAE;EAE7C,KAAK,oBACH,QAAO,oBAAoB,QAAQ,MAAM,KAAK,gBAAgB,EAAE;EAElE,KAAK,aAEH,QAAO,cAAc,QAAQ,KAAK;EAEpC,KAAK;EACL,QACE,QAAO;;;;;;;AAQb,SAAS,cAAc,MAAsB;AAE3C,KAAI,KAAK,SAAS,IAAI,CACpB,QAAO,KAAK,MAAM,IAAI,CAAC,MAAM;AAE/B,KAAI,KAAK,SAAS,IAAI,CACpB,QAAO,KAAK,MAAM,IAAI,CAAC,MAAM;AAI/B,QADc,KAAK,MAAM,aAAa,GACvB,IAAI,aAAa,IAAI;;;;;AAMtC,SAAS,oBAAoB,MAA0B,OAAuB;AAC5E,KAAI,CAAC,KAAM,QAAO;CAIlB,MAAM,mBAHW,KAAK,MAAM,IAAI,CAAC,OAAO,QAAQ,CAGd,QAAQ,MAAM,CAAC,EAAE,WAAW,IAAI,CAAC;AAEnE,KAAI,iBAAiB,WAAW,EAAG,QAAO;AAE1C,QAAO,iBAAiB,MAAM,GAAG,MAAM,CAAC,KAAK,IAAI"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ImportResult } from "../../common/types.js";
|
|
2
|
+
import { ParseResult, ParsedOperation } from "../types.js";
|
|
3
|
+
import { ContractsrcConfig, OpenApiSourceConfig } from "@lssm/lib.contracts";
|
|
4
|
+
|
|
5
|
+
//#region src/openapi/importer/index.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Import operations from a parsed OpenAPI document.
|
|
9
|
+
*/
|
|
10
|
+
declare const importFromOpenApi: (parseResult: ParseResult, contractspecOptions: ContractsrcConfig, importOptions?: Partial<OpenApiSourceConfig>) => ImportResult;
|
|
11
|
+
/**
|
|
12
|
+
* Import a single operation to ContractSpec code.
|
|
13
|
+
*/
|
|
14
|
+
declare function importOperation(operation: ParsedOperation, options: Partial<OpenApiSourceConfig> | undefined, contractspecOptions: ContractsrcConfig): string;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { importFromOpenApi, importOperation };
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../src/openapi/importer/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AAyPgB,cAxNH,iBAwNkB,EAAA,CAAA,WAAA,EAvNhB,WAuNgB,EAAA,mBAAA,EAtNR,iBAsNQ,EAAA,aAAA,CAAA,EArNd,OAqNc,CArNN,mBAqNM,CAAA,EAAA,GApN5B,YAoN4B;;;;AAGR,iBAHP,eAAA,CAGO,SAAA,EAFV,eAEU,EAAA,OAAA,EADZ,OACY,CADJ,mBACI,CAAA,GAAA,SAAA,EAAA,mBAAA,EAAA,iBAAA,CAAA,EAAA,MAAA"}
|