@atolis-hq/corum 0.1.17 → 0.1.19
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 +52 -0
- package/dist/src/adapters/asyncapi/index.js +12 -0
- package/dist/src/adapters/asyncapi/mapper.js +510 -0
- package/dist/src/adapters/asyncapi/parser.js +35 -0
- package/dist/src/adapters/index.js +2 -0
- package/dist/src/adapters/openapi/index.js +1 -1
- package/dist/src/adapters/openapi/mapper.js +12 -8
- package/dist/src/bin/corum.js +40 -2
- package/dist/src/import/config.js +34 -1
- package/dist/src/import/runner.js +5 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -116,6 +116,39 @@ Import specifications into the graph.
|
|
|
116
116
|
corum import --config <path> Import using a config YAML file
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
+
#### Import config file
|
|
120
|
+
|
|
121
|
+
The `--config` flag accepts a YAML file that can combine multiple imports in a single run and configure cross-adapter options:
|
|
122
|
+
|
|
123
|
+
```yaml
|
|
124
|
+
# Optional: remap extracted component names before node IDs are built.
|
|
125
|
+
# Useful when the same component appears under different names in different specs
|
|
126
|
+
# (e.g. "order-shipping" from a URI segment vs "ordershipping" from a topic name).
|
|
127
|
+
componentNameReplacements:
|
|
128
|
+
- from: ordershipping
|
|
129
|
+
to: order-shipping
|
|
130
|
+
|
|
131
|
+
imports:
|
|
132
|
+
- adapter: openapi
|
|
133
|
+
spec: ./specs/order-shipping.openapi.yaml
|
|
134
|
+
componentMapping:
|
|
135
|
+
strategy: uri-segment
|
|
136
|
+
segment: 0
|
|
137
|
+
|
|
138
|
+
- adapter: asyncapi
|
|
139
|
+
spec: ./specs/order-shipping.asyncapi.yaml
|
|
140
|
+
componentMapping:
|
|
141
|
+
strategy: channel-segment
|
|
142
|
+
separator: "."
|
|
143
|
+
segment: 0
|
|
144
|
+
messageNaming:
|
|
145
|
+
strategy: name-segment
|
|
146
|
+
separator: "."
|
|
147
|
+
segment: -1
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Replacements are applied in order; the first matching `from` wins. Unmatched names are passed through unchanged.
|
|
151
|
+
|
|
119
152
|
#### `corum import openapi <spec>`
|
|
120
153
|
|
|
121
154
|
Import an OpenAPI spec directly.
|
|
@@ -131,6 +164,25 @@ Options:
|
|
|
131
164
|
--graph <path> Override the graph directory
|
|
132
165
|
```
|
|
133
166
|
|
|
167
|
+
#### `corum import asyncapi <spec>`
|
|
168
|
+
|
|
169
|
+
Import an AsyncAPI spec directly.
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
corum import asyncapi <spec> [options]
|
|
173
|
+
|
|
174
|
+
Options:
|
|
175
|
+
--component-strategy <strategy> Component mapping: channel-segment (default), channel-pattern,
|
|
176
|
+
name-segment, name-pattern, tag, hardcoded
|
|
177
|
+
--separator <char> Separator for segment strategies (default: .)
|
|
178
|
+
--segment <n> Segment index (negative counts from end)
|
|
179
|
+
--pattern <regex> Regex pattern for pattern strategies
|
|
180
|
+
--component <name> Component name (hardcoded strategy)
|
|
181
|
+
--event-classification <mode> always-integration (default) or always-domain
|
|
182
|
+
--include-consumed Also import receive (consumed) operations
|
|
183
|
+
--graph <path> Override the graph directory
|
|
184
|
+
```
|
|
185
|
+
|
|
134
186
|
## Configuration
|
|
135
187
|
|
|
136
188
|
Run `corum init` to generate a `.corum/config.yaml` with all available options. Corum walks up from the current directory to find it, so you can place it at your project root.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { parseSpec } from './parser.js';
|
|
2
|
+
import { mapDocument } from './mapper.js';
|
|
3
|
+
export class AsyncAPIAdapter {
|
|
4
|
+
adapterId = 'asyncapi';
|
|
5
|
+
async import(entry, context) {
|
|
6
|
+
const { document, diagnostics } = await parseSpec(entry.spec);
|
|
7
|
+
if (!document)
|
|
8
|
+
return { nodes: [], edges: [], diagnostics };
|
|
9
|
+
const { nodes, edges, diagnostics: mapDiagnostics } = mapDocument(document, entry, context.packConfig, context.componentNameReplacements);
|
|
10
|
+
return { nodes, edges, diagnostics: [...diagnostics, ...mapDiagnostics] };
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import { applyComponentNameReplacements } from '../../import/config.js';
|
|
2
|
+
export function extractValue(strategy, operation, message) {
|
|
3
|
+
switch (strategy.strategy) {
|
|
4
|
+
case 'channel-segment': {
|
|
5
|
+
const address = operation.channels().all()[0]?.address() ?? '';
|
|
6
|
+
const parts = address.split(strategy.separator).filter(s => s.length > 0);
|
|
7
|
+
const idx = strategy.segment < 0 ? parts.length + strategy.segment : strategy.segment;
|
|
8
|
+
return parts[idx];
|
|
9
|
+
}
|
|
10
|
+
case 'channel-pattern': {
|
|
11
|
+
const address = operation.channels().all()[0]?.address() ?? '';
|
|
12
|
+
const match = address.match(strategy.pattern);
|
|
13
|
+
return match?.[1] ?? match?.[0];
|
|
14
|
+
}
|
|
15
|
+
case 'name-segment': {
|
|
16
|
+
const source = message.name() ?? message.id();
|
|
17
|
+
if (!source)
|
|
18
|
+
return undefined;
|
|
19
|
+
const parts = source.split(strategy.separator).filter(s => s.length > 0);
|
|
20
|
+
const idx = strategy.segment < 0 ? parts.length + strategy.segment : strategy.segment;
|
|
21
|
+
return parts[idx];
|
|
22
|
+
}
|
|
23
|
+
case 'name-pattern': {
|
|
24
|
+
const source = message.name() ?? message.id();
|
|
25
|
+
if (!source)
|
|
26
|
+
return undefined;
|
|
27
|
+
const match = source.match(strategy.pattern);
|
|
28
|
+
return match?.[1] ?? match?.[0];
|
|
29
|
+
}
|
|
30
|
+
case 'tag': {
|
|
31
|
+
const msgTags = message.tags().all();
|
|
32
|
+
if (msgTags.length > 0)
|
|
33
|
+
return msgTags[0].name();
|
|
34
|
+
const opTagsFn = operation.tags;
|
|
35
|
+
const opTags = opTagsFn?.()?.all?.() ?? [];
|
|
36
|
+
return opTags[0]?.name();
|
|
37
|
+
}
|
|
38
|
+
case 'hardcoded':
|
|
39
|
+
return strategy.value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function unwrapPayload(payload) {
|
|
43
|
+
if (!payload || typeof payload !== 'object')
|
|
44
|
+
return payload;
|
|
45
|
+
const p = payload;
|
|
46
|
+
if (p.schemaFormat !== undefined && p.schema !== undefined && typeof p.schema === 'object')
|
|
47
|
+
return p.schema;
|
|
48
|
+
return payload;
|
|
49
|
+
}
|
|
50
|
+
export function deriveScalarType(type, format, scalarTypes) {
|
|
51
|
+
if (format && scalarTypes[`${type}/${format}`])
|
|
52
|
+
return scalarTypes[`${type}/${format}`];
|
|
53
|
+
return scalarTypes[type];
|
|
54
|
+
}
|
|
55
|
+
export function deriveMessageName(message, namingCtx, _specPath) {
|
|
56
|
+
const rawName = message.name();
|
|
57
|
+
const rawId = message.id();
|
|
58
|
+
if (!rawName && (!rawId || rawId.startsWith('<anonymous')))
|
|
59
|
+
return null;
|
|
60
|
+
if (!rawName && !namingCtx)
|
|
61
|
+
return null;
|
|
62
|
+
const extracted = namingCtx
|
|
63
|
+
? extractValue(namingCtx.strategy, namingCtx.operation, message)
|
|
64
|
+
: rawName;
|
|
65
|
+
if (!extracted)
|
|
66
|
+
return null;
|
|
67
|
+
return { name: extracted };
|
|
68
|
+
}
|
|
69
|
+
export function classifyEvent(classification, operation, message) {
|
|
70
|
+
if (!classification)
|
|
71
|
+
return 'IntegrationEvent';
|
|
72
|
+
if (!('from' in classification)) {
|
|
73
|
+
return classification.strategy === 'always-domain' ? 'DomainEvent' : 'IntegrationEvent';
|
|
74
|
+
}
|
|
75
|
+
const value = extractValue(classification.from, operation, message);
|
|
76
|
+
return value === classification.domainValue ? 'DomainEvent' : 'IntegrationEvent';
|
|
77
|
+
}
|
|
78
|
+
export function deriveNodeId(kind, component, name, opts) {
|
|
79
|
+
if (kind === 'event')
|
|
80
|
+
return `${component}.${opts?.template ?? 'IntegrationEvent'}.${name}`;
|
|
81
|
+
return `${opts.parentId}.${opts.section}.${name}`;
|
|
82
|
+
}
|
|
83
|
+
// Extract the original component schema name from a resolved schema object.
|
|
84
|
+
// @asyncapi/parser resolves $refs inline but tags each resolved object with
|
|
85
|
+
// x-parser-schema-id equal to the component schema name (non-anonymous schemas
|
|
86
|
+
// have a plain name; anonymous ones get "<anonymous-schema-N>").
|
|
87
|
+
// Falls back to parsing a literal $ref string for test mocks that don't resolve.
|
|
88
|
+
function componentSchemaIdOf(obj) {
|
|
89
|
+
if (!obj || typeof obj !== 'object')
|
|
90
|
+
return undefined;
|
|
91
|
+
const o = obj;
|
|
92
|
+
const xId = o['x-parser-schema-id'];
|
|
93
|
+
if (typeof xId === 'string' && !xId.startsWith('<'))
|
|
94
|
+
return xId;
|
|
95
|
+
const ref = o['$ref'];
|
|
96
|
+
if (typeof ref === 'string' && ref.startsWith('#/components/schemas/'))
|
|
97
|
+
return ref.slice('#/components/schemas/'.length);
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
export function countMessageSchemaUsage(document) {
|
|
101
|
+
const rawDoc = document.json();
|
|
102
|
+
const componentSchemaNames = new Set(Object.keys(rawDoc.components?.schemas ?? {}));
|
|
103
|
+
const schemaToMessages = new Map();
|
|
104
|
+
function register(schemaName, msgName) {
|
|
105
|
+
if (!componentSchemaNames.has(schemaName))
|
|
106
|
+
return;
|
|
107
|
+
if (!schemaToMessages.has(schemaName))
|
|
108
|
+
schemaToMessages.set(schemaName, new Set());
|
|
109
|
+
schemaToMessages.get(schemaName).add(msgName);
|
|
110
|
+
}
|
|
111
|
+
function collectRefs(obj, msgName) {
|
|
112
|
+
if (!obj || typeof obj !== 'object')
|
|
113
|
+
return;
|
|
114
|
+
const id = componentSchemaIdOf(obj);
|
|
115
|
+
if (id)
|
|
116
|
+
register(id, msgName);
|
|
117
|
+
const o = obj;
|
|
118
|
+
if (o.properties)
|
|
119
|
+
for (const v of Object.values(o.properties))
|
|
120
|
+
collectRefs(v, msgName);
|
|
121
|
+
if (o.items)
|
|
122
|
+
collectRefs(o.items, msgName);
|
|
123
|
+
}
|
|
124
|
+
for (const operation of document.allOperations()) {
|
|
125
|
+
for (const message of operation.messages().all()) {
|
|
126
|
+
const msgName = message.name() ?? message.id();
|
|
127
|
+
if (!msgName)
|
|
128
|
+
continue;
|
|
129
|
+
const msgJson = message.json();
|
|
130
|
+
if (msgJson.payload)
|
|
131
|
+
collectRefs(unwrapPayload(msgJson.payload), msgName);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const counts = new Map();
|
|
135
|
+
for (const [name, names] of schemaToMessages)
|
|
136
|
+
counts.set(name, names.size);
|
|
137
|
+
return counts;
|
|
138
|
+
}
|
|
139
|
+
export function collectSharedSchemaNames(document, counts) {
|
|
140
|
+
const rawDoc = document.json();
|
|
141
|
+
const shared = new Set();
|
|
142
|
+
for (const [name, count] of counts) {
|
|
143
|
+
if (count >= 2)
|
|
144
|
+
shared.add(name);
|
|
145
|
+
}
|
|
146
|
+
function schemaReferences(schema, names) {
|
|
147
|
+
const refs = new Set();
|
|
148
|
+
function walk(obj) {
|
|
149
|
+
if (!obj || typeof obj !== 'object')
|
|
150
|
+
return;
|
|
151
|
+
const id = componentSchemaIdOf(obj);
|
|
152
|
+
if (id && names.has(id))
|
|
153
|
+
refs.add(id);
|
|
154
|
+
const o = obj;
|
|
155
|
+
if (o.properties)
|
|
156
|
+
for (const v of Object.values(o.properties))
|
|
157
|
+
walk(v);
|
|
158
|
+
if (o.items)
|
|
159
|
+
walk(o.items);
|
|
160
|
+
}
|
|
161
|
+
walk(schema);
|
|
162
|
+
return refs;
|
|
163
|
+
}
|
|
164
|
+
const candidateNames = new Set(Object.keys(rawDoc.components?.schemas ?? {}));
|
|
165
|
+
let changed = true;
|
|
166
|
+
while (changed) {
|
|
167
|
+
changed = false;
|
|
168
|
+
for (const [candidateName] of Object.entries(rawDoc.components?.schemas ?? {})) {
|
|
169
|
+
if (shared.has(candidateName))
|
|
170
|
+
continue;
|
|
171
|
+
for (const sharedName of shared) {
|
|
172
|
+
const sharedSchema = rawDoc.components?.schemas?.[sharedName];
|
|
173
|
+
const refs = schemaReferences(sharedSchema, candidateNames);
|
|
174
|
+
if (refs.has(candidateName)) {
|
|
175
|
+
shared.add(candidateName);
|
|
176
|
+
changed = true;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return shared;
|
|
183
|
+
}
|
|
184
|
+
export function extractHeaders(rawHeaders, scalarTypes, specPath) {
|
|
185
|
+
if (!rawHeaders || typeof rawHeaders !== 'object')
|
|
186
|
+
return null;
|
|
187
|
+
const h = rawHeaders;
|
|
188
|
+
if (!h.properties || Object.keys(h.properties).length === 0)
|
|
189
|
+
return null;
|
|
190
|
+
const headers = {};
|
|
191
|
+
const diagnostics = [];
|
|
192
|
+
for (const [name, rawDef] of Object.entries(h.properties)) {
|
|
193
|
+
const def = rawDef;
|
|
194
|
+
const rawType = Array.isArray(def.type) ? (def.type.find(t => t !== 'null') ?? 'string') : (def.type ?? 'string');
|
|
195
|
+
if (rawType === 'object') {
|
|
196
|
+
diagnostics.push({ severity: 'warning', file: specPath, message: `Header "${name}" is a nested object — skipping` });
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const scalarType = deriveScalarType(rawType, def.format, scalarTypes) ?? 'string';
|
|
200
|
+
const required = Array.isArray(h.required) && h.required.includes(name);
|
|
201
|
+
const entry = { type: scalarType, required };
|
|
202
|
+
if (def.description)
|
|
203
|
+
entry.description = def.description;
|
|
204
|
+
headers[name] = entry;
|
|
205
|
+
}
|
|
206
|
+
return { headers, diagnostics };
|
|
207
|
+
}
|
|
208
|
+
export function mapDocument(document, entry, packConfig, componentNameReplacements = []) {
|
|
209
|
+
const nodes = [];
|
|
210
|
+
const edges = [];
|
|
211
|
+
const diagnostics = [];
|
|
212
|
+
const sharedSchemas = new Map();
|
|
213
|
+
const sourceSchemas = new Map();
|
|
214
|
+
const rawDoc = document.json();
|
|
215
|
+
for (const [name, schema] of Object.entries(rawDoc.components?.schemas ?? {})) {
|
|
216
|
+
sourceSchemas.set(name, schema);
|
|
217
|
+
}
|
|
218
|
+
const schemaCounts = countMessageSchemaUsage(document);
|
|
219
|
+
const sharedSchemaNames = collectSharedSchemaNames(document, schemaCounts);
|
|
220
|
+
const schemaComponents = new Map();
|
|
221
|
+
for (const operation of document.allOperations()) {
|
|
222
|
+
for (const message of operation.messages().all()) {
|
|
223
|
+
const rawComponent = extractValue(entry.componentMapping, operation, message);
|
|
224
|
+
if (!rawComponent)
|
|
225
|
+
continue;
|
|
226
|
+
const component = applyComponentNameReplacements(rawComponent, componentNameReplacements);
|
|
227
|
+
const msgName = message.name() ?? message.id();
|
|
228
|
+
if (!msgName)
|
|
229
|
+
continue;
|
|
230
|
+
const msgJson = message.json();
|
|
231
|
+
const resolvedComponent = component;
|
|
232
|
+
function walkForComponent(obj) {
|
|
233
|
+
if (!obj || typeof obj !== 'object')
|
|
234
|
+
return;
|
|
235
|
+
const id = componentSchemaIdOf(obj);
|
|
236
|
+
if (id && sourceSchemas.has(id)) {
|
|
237
|
+
const existing = schemaComponents.get(id);
|
|
238
|
+
if (!existing)
|
|
239
|
+
schemaComponents.set(id, resolvedComponent);
|
|
240
|
+
else if (existing !== resolvedComponent)
|
|
241
|
+
schemaComponents.set(id, 'shared');
|
|
242
|
+
}
|
|
243
|
+
const o = obj;
|
|
244
|
+
if (o.properties)
|
|
245
|
+
for (const v of Object.values(o.properties))
|
|
246
|
+
walkForComponent(v);
|
|
247
|
+
if (o.items)
|
|
248
|
+
walkForComponent(o.items);
|
|
249
|
+
}
|
|
250
|
+
if (msgJson.payload)
|
|
251
|
+
walkForComponent(unwrapPayload(msgJson.payload));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (const [name, schema] of Object.entries(rawDoc.components?.schemas ?? {})) {
|
|
255
|
+
const s = schema;
|
|
256
|
+
const component = schemaComponents.get(name) ?? 'shared';
|
|
257
|
+
if (s.enum) {
|
|
258
|
+
sharedSchemas.set(name, `${component}.EnumDefinition.${name}`);
|
|
259
|
+
}
|
|
260
|
+
else if (sharedSchemaNames.has(name)) {
|
|
261
|
+
sharedSchemas.set(name, `${component}.Schema.${name}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
for (const [name, schema] of Object.entries(rawDoc.components?.schemas ?? {})) {
|
|
265
|
+
const s = schema;
|
|
266
|
+
const registeredId = sharedSchemas.get(name);
|
|
267
|
+
if (!registeredId)
|
|
268
|
+
continue;
|
|
269
|
+
if (s.enum) {
|
|
270
|
+
const [component] = registeredId.split('.');
|
|
271
|
+
nodes.push(makeNode(packConfig.constructs['enumDefinition']?.template ?? 'EnumDefinition', component, entry.spec, registeredId));
|
|
272
|
+
for (const value of s.enum) {
|
|
273
|
+
const valueId = deriveNodeId('enumValue', component, String(value), { parentId: registeredId, section: 'values' });
|
|
274
|
+
const valueNode = makeNode(packConfig.constructs['enumValue']?.template ?? 'EnumValue', component, entry.spec, valueId);
|
|
275
|
+
valueNode.properties = { name: String(value) };
|
|
276
|
+
nodes.push(valueNode);
|
|
277
|
+
edges.push({ id: `${registeredId}__has-value__${valueId}`, from: registeredId, to: valueId, type: 'has-value', state: 'implemented', stability: 'unstable' });
|
|
278
|
+
}
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (!sharedSchemaNames.has(name))
|
|
282
|
+
continue;
|
|
283
|
+
const [component] = registeredId.split('.');
|
|
284
|
+
nodes.push(makeNode(packConfig.constructs['payloadSchema']?.template ?? 'Schema', component, entry.spec, registeredId));
|
|
285
|
+
emitFields(s, registeredId, 'fields', undefined, packConfig, entry.spec, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, new Map());
|
|
286
|
+
}
|
|
287
|
+
const seenMessages = new Map();
|
|
288
|
+
const allOperations = document.allOperations().all();
|
|
289
|
+
const operations = entry.includeConsumed ? allOperations : allOperations.filter(op => op.action() === 'send');
|
|
290
|
+
for (const operation of operations) {
|
|
291
|
+
const channelAddress = operation.channels().all()[0]?.address() ?? '';
|
|
292
|
+
for (const message of operation.messages().all()) {
|
|
293
|
+
const rawComponent = extractValue(entry.componentMapping, operation, message);
|
|
294
|
+
if (!rawComponent) {
|
|
295
|
+
diagnostics.push({ severity: 'warning', file: entry.spec, message: `Cannot derive component for message on channel "${channelAddress}" — skipping` });
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const component = applyComponentNameReplacements(rawComponent, componentNameReplacements);
|
|
299
|
+
const namingCtx = entry.messageNaming ? { strategy: entry.messageNaming, operation } : undefined;
|
|
300
|
+
const nameResult = deriveMessageName(message, namingCtx, entry.spec);
|
|
301
|
+
if (!nameResult) {
|
|
302
|
+
const hasId = message.id() && !message.id().startsWith('<anonymous');
|
|
303
|
+
diagnostics.push({
|
|
304
|
+
severity: hasId ? 'warning' : 'error',
|
|
305
|
+
file: entry.spec,
|
|
306
|
+
message: hasId
|
|
307
|
+
? `Cannot derive name for message on channel "${channelAddress}" — skipping. Set message.name or configure messageNaming.`
|
|
308
|
+
: `Anonymous message on channel "${channelAddress}" — skipping (no name or id)`,
|
|
309
|
+
});
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
const messageName = nameResult.name;
|
|
313
|
+
if (seenMessages.has(messageName)) {
|
|
314
|
+
diagnostics.push({ severity: 'warning', file: entry.spec, message: `[INFO] Message "${messageName}" also on channel "${channelAddress}" — using first channel "${seenMessages.get(messageName)}"` });
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
seenMessages.set(messageName, channelAddress);
|
|
318
|
+
const eventTemplate = classifyEvent(entry.eventClassification, operation, message);
|
|
319
|
+
const eventId = deriveNodeId('event', component, messageName, { template: eventTemplate });
|
|
320
|
+
const eventNode = makeNode(eventTemplate, component, entry.spec, eventId);
|
|
321
|
+
const msgRaw = message.json();
|
|
322
|
+
const properties = { topic: channelAddress };
|
|
323
|
+
if (msgRaw.description)
|
|
324
|
+
properties.description = String(msgRaw.description);
|
|
325
|
+
const rawPayload = unwrapPayload(msgRaw.payload);
|
|
326
|
+
if (rawPayload) {
|
|
327
|
+
const payloadSchemaName = componentSchemaIdOf(rawPayload);
|
|
328
|
+
if (payloadSchemaName) {
|
|
329
|
+
const globalId = sharedSchemas.get(payloadSchemaName);
|
|
330
|
+
if (globalId) {
|
|
331
|
+
emitReadsEdge(eventId, globalId, edges);
|
|
332
|
+
properties.payload = globalId;
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
const sourceSchema = sourceSchemas.get(payloadSchemaName);
|
|
336
|
+
if (sourceSchema) {
|
|
337
|
+
const schemaKey = payloadSchemaName;
|
|
338
|
+
const schemaId = deriveNodeId('schema', component, schemaKey, { parentId: eventId, section: 'schemas' });
|
|
339
|
+
nodes.push(makeNode(packConfig.constructs['payloadSchema']?.template ?? 'Schema', component, entry.spec, schemaId));
|
|
340
|
+
edges.push({ id: `${eventId}__has-field__${schemaId}`, from: eventId, to: schemaId, type: 'has-field', state: 'implemented', stability: 'unstable' });
|
|
341
|
+
emitFields(sourceSchema, schemaId, 'fields', eventId, packConfig, entry.spec, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, new Map());
|
|
342
|
+
properties.payload = `#/schemas/${schemaKey}`;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
else if (rawPayload.type === 'object' && rawPayload.properties) {
|
|
347
|
+
const schemaId = deriveNodeId('schema', component, messageName, { parentId: eventId, section: 'schemas' });
|
|
348
|
+
nodes.push(makeNode(packConfig.constructs['payloadSchema']?.template ?? 'Schema', component, entry.spec, schemaId));
|
|
349
|
+
edges.push({ id: `${eventId}__has-field__${schemaId}`, from: eventId, to: schemaId, type: 'has-field', state: 'implemented', stability: 'unstable' });
|
|
350
|
+
emitFields(rawPayload, schemaId, 'fields', eventId, packConfig, entry.spec, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, new Map());
|
|
351
|
+
properties.payload = `#/schemas/${messageName}`;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
const primitiveType = Array.isArray(rawPayload.type) ? rawPayload.type.find(t => t !== 'null') : rawPayload.type;
|
|
355
|
+
if (primitiveType && primitiveType !== 'object') {
|
|
356
|
+
diagnostics.push({ severity: 'warning', file: entry.spec, message: `Message "${messageName}" has scalar payload (type: ${primitiveType}) — Event created with no schemas section` });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (message.hasHeaders()) {
|
|
361
|
+
const rawHeaders = msgRaw.headers ?? null;
|
|
362
|
+
const headerResult = extractHeaders(rawHeaders, packConfig.scalarTypes, entry.spec);
|
|
363
|
+
if (headerResult) {
|
|
364
|
+
diagnostics.push(...headerResult.diagnostics);
|
|
365
|
+
if (Object.keys(headerResult.headers).length > 0)
|
|
366
|
+
properties.headers = headerResult.headers;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
eventNode.properties = properties;
|
|
370
|
+
nodes.push(eventNode);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return { nodes, edges, diagnostics };
|
|
374
|
+
}
|
|
375
|
+
function makeNode(template, component, specPath, id) {
|
|
376
|
+
return {
|
|
377
|
+
id,
|
|
378
|
+
template,
|
|
379
|
+
component,
|
|
380
|
+
state: 'implemented',
|
|
381
|
+
stability: 'unstable',
|
|
382
|
+
schemaVersion: '1',
|
|
383
|
+
lastModifiedAt: new Date().toISOString().split('T')[0],
|
|
384
|
+
extractedFrom: specPath,
|
|
385
|
+
derivation: 'determined',
|
|
386
|
+
derivedBy: 'adapter:asyncapi',
|
|
387
|
+
properties: {},
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function emitReadsEdge(from, to, edges) {
|
|
391
|
+
const id = `${from}__reads__${to}`;
|
|
392
|
+
if (!edges.some(e => e.id === id)) {
|
|
393
|
+
edges.push({ id, from, to, type: 'reads', state: 'implemented', stability: 'unstable' });
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function refName(ref) {
|
|
397
|
+
return ref.split('/').pop() ?? ref;
|
|
398
|
+
}
|
|
399
|
+
function isRefSchema(schema) {
|
|
400
|
+
return typeof schema === 'object' && schema !== null && '$ref' in schema;
|
|
401
|
+
}
|
|
402
|
+
function resolveAllOfRef(schema) {
|
|
403
|
+
if (isRefSchema(schema))
|
|
404
|
+
return schema;
|
|
405
|
+
const s = schema;
|
|
406
|
+
if (Array.isArray(s.allOf) && s.allOf.length === 1 && isRefSchema(s.allOf[0]))
|
|
407
|
+
return s.allOf[0];
|
|
408
|
+
return schema;
|
|
409
|
+
}
|
|
410
|
+
function resolveSchemaRef(schemaName, nullable, collection, readsSource, rootId, component, packConfig, specPath, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, localSchemas) {
|
|
411
|
+
const extra = { nullable };
|
|
412
|
+
if (collection)
|
|
413
|
+
extra.collection = collection;
|
|
414
|
+
const globalId = sharedSchemas.get(schemaName);
|
|
415
|
+
if (globalId) {
|
|
416
|
+
emitReadsEdge(readsSource, globalId, edges);
|
|
417
|
+
return { $ref: globalId, ...extra };
|
|
418
|
+
}
|
|
419
|
+
if (localSchemas.has(schemaName))
|
|
420
|
+
return { $ref: localSchemas.get(schemaName), ...extra };
|
|
421
|
+
if (rootId) {
|
|
422
|
+
const src = sourceSchemas.get(schemaName);
|
|
423
|
+
if (src) {
|
|
424
|
+
const inlineId = deriveNodeId('schema', component, schemaName, { parentId: rootId, section: 'schemas' });
|
|
425
|
+
if (!nodes.some(n => n.id === inlineId)) {
|
|
426
|
+
nodes.push(makeNode(packConfig.constructs['payloadSchema']?.template ?? 'Schema', component, specPath, inlineId));
|
|
427
|
+
edges.push({ id: `${rootId}__has-field__${inlineId}`, from: rootId, to: inlineId, type: 'has-field', state: 'implemented', stability: 'unstable' });
|
|
428
|
+
const localRef = `#/schemas/${schemaName}`;
|
|
429
|
+
localSchemas.set(schemaName, localRef);
|
|
430
|
+
emitFields(src, inlineId, 'fields', rootId, packConfig, specPath, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, localSchemas);
|
|
431
|
+
}
|
|
432
|
+
return { $ref: localSchemas.get(schemaName) ?? `#/schemas/${schemaName}`, ...extra };
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return { $ref: schemaName, ...extra };
|
|
436
|
+
}
|
|
437
|
+
function emitFields(schema, parentId, section, rootId, packConfig, specPath, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, localSchemas) {
|
|
438
|
+
const readsSource = rootId ?? parentId;
|
|
439
|
+
const [component] = parentId.split('.');
|
|
440
|
+
for (const [fieldName, rawFieldSchema] of Object.entries(schema.properties ?? {})) {
|
|
441
|
+
const resolvedSchemaId = componentSchemaIdOf(rawFieldSchema);
|
|
442
|
+
const fieldSchema = resolveAllOfRef(rawFieldSchema);
|
|
443
|
+
const fieldId = deriveNodeId('field', component, fieldName, { parentId, section });
|
|
444
|
+
const fieldNode = makeNode(packConfig.constructs['payloadField']?.template ?? 'Field', component, specPath, fieldId);
|
|
445
|
+
const required = Array.isArray(schema.required) && schema.required.includes(fieldName);
|
|
446
|
+
// Resolve reference: prefer x-parser-schema-id (resolved by @asyncapi/parser), fall back to literal $ref
|
|
447
|
+
const schemaName = (resolvedSchemaId !== undefined && (sharedSchemas.has(resolvedSchemaId) || sourceSchemas.has(resolvedSchemaId)))
|
|
448
|
+
? resolvedSchemaId
|
|
449
|
+
: (isRefSchema(fieldSchema) ? refName(fieldSchema.$ref) : undefined);
|
|
450
|
+
if (schemaName !== undefined) {
|
|
451
|
+
fieldNode.properties = resolveSchemaRef(schemaName, !required, undefined, readsSource, rootId, component, packConfig, specPath, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, localSchemas);
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
const fs = fieldSchema;
|
|
455
|
+
const rawType = Array.isArray(fs.type) ? (fs.type.find(t => t !== 'null') ?? 'string') : (fs.type ?? 'string');
|
|
456
|
+
const isNullableArray = Array.isArray(fs.type) && fs.type.includes('null');
|
|
457
|
+
const nullable = !required || isNullableArray;
|
|
458
|
+
if (fs.enum) {
|
|
459
|
+
const enumRef = sharedSchemas.get(fieldName);
|
|
460
|
+
fieldNode.properties = enumRef ? { $ref: enumRef, nullable } : { type: 'string', nullable };
|
|
461
|
+
if (!enumRef)
|
|
462
|
+
diagnostics.push({ severity: 'warning', file: specPath, message: `Inline enum for field "${fieldId}" — treating as string` });
|
|
463
|
+
}
|
|
464
|
+
else if (rawType === 'array') {
|
|
465
|
+
const items = fs.items ? resolveAllOfRef(fs.items) : undefined;
|
|
466
|
+
if (!items) {
|
|
467
|
+
fieldNode.properties = { type: 'string', nullable, collection: 'array' };
|
|
468
|
+
}
|
|
469
|
+
else if (isRefSchema(items)) {
|
|
470
|
+
fieldNode.properties = resolveSchemaRef(refName(items.$ref), nullable, 'array', readsSource, rootId, component, packConfig, specPath, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, localSchemas);
|
|
471
|
+
if (!fieldNode.properties.$ref)
|
|
472
|
+
fieldNode.properties = { type: 'string', nullable, collection: 'array' };
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
const it = items;
|
|
476
|
+
fieldNode.properties = { type: deriveScalarType(it.type ?? 'string', it.format, packConfig.scalarTypes) ?? 'string', nullable, collection: 'array' };
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
else if (rawType === 'object' && fs.properties) {
|
|
480
|
+
if (rootId) {
|
|
481
|
+
const inlineId = deriveNodeId('schema', component, fieldName, { parentId: rootId, section: 'schemas' });
|
|
482
|
+
if (!nodes.some(n => n.id === inlineId)) {
|
|
483
|
+
nodes.push(makeNode(packConfig.constructs['payloadSchema']?.template ?? 'Schema', component, specPath, inlineId));
|
|
484
|
+
edges.push({ id: `${rootId}__has-field__${inlineId}`, from: rootId, to: inlineId, type: 'has-field', state: 'implemented', stability: 'unstable' });
|
|
485
|
+
const localRef = `#/schemas/${fieldName}`;
|
|
486
|
+
localSchemas.set(fieldName, localRef);
|
|
487
|
+
emitFields(fs, inlineId, 'fields', rootId, packConfig, specPath, nodes, edges, diagnostics, sharedSchemas, sourceSchemas, localSchemas);
|
|
488
|
+
}
|
|
489
|
+
fieldNode.properties = { $ref: localSchemas.get(fieldName) ?? `#/schemas/${fieldName}`, nullable };
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
diagnostics.push({ severity: 'warning', file: specPath, message: `Inline object field "${fieldId}" has no event context — treating as string` });
|
|
493
|
+
fieldNode.properties = { type: 'string', nullable };
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
const scalarType = deriveScalarType(rawType, fs.format, packConfig.scalarTypes);
|
|
498
|
+
if (scalarType) {
|
|
499
|
+
fieldNode.properties = { type: scalarType, nullable };
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
diagnostics.push({ severity: 'warning', file: specPath, message: `Unknown type for field "${fieldId}": ${rawType}/${fs.format} — defaulting to string` });
|
|
503
|
+
fieldNode.properties = { type: 'string', nullable };
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
nodes.push(fieldNode);
|
|
508
|
+
edges.push({ id: `${parentId}__has-field__${fieldId}`, from: parentId, to: fieldId, type: 'has-field', state: 'implemented', stability: 'unstable' });
|
|
509
|
+
}
|
|
510
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { Parser } from '@asyncapi/parser';
|
|
4
|
+
const parser = new Parser();
|
|
5
|
+
export async function parseSpec(specPath) {
|
|
6
|
+
const diagnostics = [];
|
|
7
|
+
try {
|
|
8
|
+
const content = readFileSync(specPath, 'utf-8');
|
|
9
|
+
const source = `file://${path.resolve(specPath)}`;
|
|
10
|
+
const result = await parser.parse(content, { source });
|
|
11
|
+
for (const d of result.diagnostics) {
|
|
12
|
+
if (d.severity === 0) {
|
|
13
|
+
diagnostics.push({ severity: 'error', file: specPath, message: d.message });
|
|
14
|
+
}
|
|
15
|
+
else if (d.severity === 1) {
|
|
16
|
+
diagnostics.push({ severity: 'warning', file: specPath, message: d.message });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (!result.document) {
|
|
20
|
+
if (!diagnostics.some(d => d.severity === 'error')) {
|
|
21
|
+
diagnostics.push({ severity: 'error', file: specPath, message: 'AsyncAPI parser returned no document' });
|
|
22
|
+
}
|
|
23
|
+
return { document: null, diagnostics };
|
|
24
|
+
}
|
|
25
|
+
return { document: result.document, diagnostics };
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
diagnostics.push({
|
|
29
|
+
severity: 'error',
|
|
30
|
+
file: specPath,
|
|
31
|
+
message: `Failed to parse AsyncAPI spec: ${err instanceof Error ? err.message : String(err)}`,
|
|
32
|
+
});
|
|
33
|
+
return { document: null, diagnostics };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -6,7 +6,7 @@ export class OpenAPIAdapter {
|
|
|
6
6
|
const { document, diagnostics } = await parseSpec(entry.spec);
|
|
7
7
|
if (!document)
|
|
8
8
|
return { nodes: [], edges: [], diagnostics };
|
|
9
|
-
const { nodes, edges, diagnostics: mapDiagnostics } = mapDocument(document, entry, context.packConfig);
|
|
9
|
+
const { nodes, edges, diagnostics: mapDiagnostics } = mapDocument(document, entry, context.packConfig, context.componentNameReplacements);
|
|
10
10
|
return { nodes, edges, diagnostics: [...diagnostics, ...mapDiagnostics] };
|
|
11
11
|
}
|
|
12
12
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { applyComponentNameReplacements } from '../../import/config.js';
|
|
1
2
|
export function deriveComponent(path, mapping) {
|
|
2
3
|
if (mapping.strategy === 'hardcoded')
|
|
3
4
|
return mapping.component;
|
|
@@ -42,7 +43,7 @@ function emitReadsEdge(from, to, edges) {
|
|
|
42
43
|
edges.push({ id, from, to, type: 'reads', state: 'implemented', stability: 'unstable' });
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
|
-
export function mapDocument(document, entry, packConfig) {
|
|
46
|
+
export function mapDocument(document, entry, packConfig, componentNameReplacements = []) {
|
|
46
47
|
const nodes = [];
|
|
47
48
|
const edges = [];
|
|
48
49
|
const diagnostics = [];
|
|
@@ -62,7 +63,7 @@ export function mapDocument(document, entry, packConfig) {
|
|
|
62
63
|
if (isRefSchema(schema))
|
|
63
64
|
continue;
|
|
64
65
|
const s = schema;
|
|
65
|
-
const component = deriveComponentForSchema(name, document, entry);
|
|
66
|
+
const component = deriveComponentForSchema(name, document, entry, componentNameReplacements);
|
|
66
67
|
if (!component)
|
|
67
68
|
continue;
|
|
68
69
|
if (s.type !== 'object' && s.enum) {
|
|
@@ -117,9 +118,12 @@ export function mapDocument(document, entry, packConfig) {
|
|
|
117
118
|
if (!operation)
|
|
118
119
|
continue;
|
|
119
120
|
const operationId = operation.operationId ?? `${method}-${urlPath.replace(/\//g, '-').replace(/^-/, '')}`;
|
|
120
|
-
const
|
|
121
|
+
const rawComponent = entry.componentMapping.strategy === 'tag'
|
|
121
122
|
? operation.tags?.[0]
|
|
122
123
|
: deriveComponent(urlPath, entry.componentMapping);
|
|
124
|
+
const component = rawComponent !== undefined
|
|
125
|
+
? applyComponentNameReplacements(rawComponent, componentNameReplacements)
|
|
126
|
+
: undefined;
|
|
123
127
|
if (!component) {
|
|
124
128
|
diagnostics.push({ severity: 'warning', file: entry.spec, message: `Cannot derive component for ${method.toUpperCase()} ${urlPath}, skipping` });
|
|
125
129
|
continue;
|
|
@@ -438,7 +442,7 @@ function emitFields(schema, parentId, section, rootId, packConfig, specPath, nod
|
|
|
438
442
|
edges.push({ id: `${parentId}__has-field__${fieldId}`, from: parentId, to: fieldId, type: 'has-field', state: 'implemented', stability: 'unstable' });
|
|
439
443
|
}
|
|
440
444
|
}
|
|
441
|
-
function deriveComponentForSchema(name, document, entry, visited = new Set()) {
|
|
445
|
+
function deriveComponentForSchema(name, document, entry, componentNameReplacements, visited = new Set()) {
|
|
442
446
|
if (visited.has(name))
|
|
443
447
|
return undefined;
|
|
444
448
|
visited.add(name);
|
|
@@ -452,11 +456,11 @@ function deriveComponentForSchema(name, document, entry, visited = new Set()) {
|
|
|
452
456
|
if (!operation)
|
|
453
457
|
continue;
|
|
454
458
|
if (referencesSchema(operation, name)) {
|
|
455
|
-
const
|
|
459
|
+
const rawComponent = entry.componentMapping.strategy === 'tag'
|
|
456
460
|
? operation.tags?.[0]
|
|
457
461
|
: deriveComponent(urlPath, entry.componentMapping);
|
|
458
|
-
if (
|
|
459
|
-
directComponents.add(
|
|
462
|
+
if (rawComponent)
|
|
463
|
+
directComponents.add(applyComponentNameReplacements(rawComponent, componentNameReplacements));
|
|
460
464
|
}
|
|
461
465
|
}
|
|
462
466
|
}
|
|
@@ -468,7 +472,7 @@ function deriveComponentForSchema(name, document, entry, visited = new Set()) {
|
|
|
468
472
|
if (schemaName === name || isRefSchema(schema))
|
|
469
473
|
continue;
|
|
470
474
|
if (JSON.stringify(schema).includes(`"#/components/schemas/${name}"`)) {
|
|
471
|
-
const comp = deriveComponentForSchema(schemaName, document, entry, visited);
|
|
475
|
+
const comp = deriveComponentForSchema(schemaName, document, entry, componentNameReplacements, visited);
|
|
472
476
|
if (comp)
|
|
473
477
|
return comp;
|
|
474
478
|
}
|
package/dist/src/bin/corum.js
CHANGED
|
@@ -5,7 +5,7 @@ import { readFile as fsReadFile } from 'node:fs/promises';
|
|
|
5
5
|
import { createRequire } from 'node:module';
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import { parse as parseYaml } from 'yaml';
|
|
8
|
-
import { buildOpenAPIConfig, loadImportConfig } from '../import/config.js';
|
|
8
|
+
import { buildAsyncAPIConfig, buildOpenAPIConfig, loadImportConfig } from '../import/config.js';
|
|
9
9
|
import { runImport } from '../import/runner.js';
|
|
10
10
|
import { loadGraph } from '../loader/index.js';
|
|
11
11
|
import { startMcpServer } from '../mcp/index.js';
|
|
@@ -22,7 +22,8 @@ const program = new Command();
|
|
|
22
22
|
program
|
|
23
23
|
.name('corum')
|
|
24
24
|
.description('Corum graph CLI')
|
|
25
|
-
.version(version)
|
|
25
|
+
.version(version)
|
|
26
|
+
.addHelpText('after', '\nFull documentation: https://github.com/atolis-hq/corum#readme');
|
|
26
27
|
program
|
|
27
28
|
.command('mcp')
|
|
28
29
|
.description('Start the MCP stdio server (+ web UI by default)')
|
|
@@ -145,6 +146,7 @@ const importCmd = program.command('import')
|
|
|
145
146
|
.description('Import specifications into the graph')
|
|
146
147
|
.option('--config <path>', 'Path to import config YAML')
|
|
147
148
|
.option('--graph <path>', 'Override CORUM_GRAPH_PATH')
|
|
149
|
+
.addHelpText('after', '\nFor import config file format and options, see: https://github.com/atolis-hq/corum#readme')
|
|
148
150
|
.action(async (opts) => {
|
|
149
151
|
if (!opts.config) {
|
|
150
152
|
importCmd.help();
|
|
@@ -185,6 +187,42 @@ importCmd
|
|
|
185
187
|
process.exit(2);
|
|
186
188
|
}
|
|
187
189
|
});
|
|
190
|
+
importCmd
|
|
191
|
+
.command('asyncapi <spec>')
|
|
192
|
+
.description('Import an AsyncAPI spec into the graph')
|
|
193
|
+
.option('--component-strategy <strategy>', 'Component mapping: channel-segment, channel-pattern, name-segment, name-pattern, tag, hardcoded', 'channel-segment')
|
|
194
|
+
.option('--separator <char>', 'Separator for segment strategies', '.')
|
|
195
|
+
.option('--segment <n>', 'Segment index for segment strategies', parseInt)
|
|
196
|
+
.option('--pattern <regex>', 'Regex pattern for pattern strategies')
|
|
197
|
+
.option('--component <name>', 'Component name (hardcoded strategy)')
|
|
198
|
+
.option('--event-classification <mode>', 'Event classification: always-integration, always-domain', 'always-integration')
|
|
199
|
+
.option('--include-consumed', 'Also import receive (consumed) operations', false)
|
|
200
|
+
.option('--graph <path>', 'Override CORUM_GRAPH_PATH')
|
|
201
|
+
.action(async (spec, opts) => {
|
|
202
|
+
try {
|
|
203
|
+
const runtimeConfig = buildRuntimeConfig(opts.graph);
|
|
204
|
+
const entry = buildAsyncAPIConfig(spec, opts.componentStrategy, {
|
|
205
|
+
separator: opts.separator,
|
|
206
|
+
segment: opts.segment,
|
|
207
|
+
pattern: opts.pattern,
|
|
208
|
+
value: opts.component,
|
|
209
|
+
});
|
|
210
|
+
if (opts.eventClassification === 'always-domain') {
|
|
211
|
+
entry.eventClassification = { strategy: 'always-domain' };
|
|
212
|
+
}
|
|
213
|
+
if (opts.includeConsumed) {
|
|
214
|
+
entry.includeConsumed = true;
|
|
215
|
+
}
|
|
216
|
+
const result = await runImport({ imports: [entry] }, runtimeConfig);
|
|
217
|
+
reportDiagnostics(result.diagnostics);
|
|
218
|
+
if (result.diagnostics.some(d => d.severity === 'error'))
|
|
219
|
+
process.exit(1);
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
process.stderr.write(`[ERROR] ${err instanceof Error ? err.message : String(err)}\n`);
|
|
223
|
+
process.exit(2);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
188
226
|
const packCmd = program.command('pack').description('Manage template packs');
|
|
189
227
|
packCmd
|
|
190
228
|
.command('install <name>')
|
|
@@ -11,7 +11,16 @@ export function loadImportConfig(filePath) {
|
|
|
11
11
|
if (!isImportConfig(raw)) {
|
|
12
12
|
throw new Error(`Invalid import config: must have an "imports" array`);
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
const cfg = raw;
|
|
15
|
+
for (const replacement of cfg.componentNameReplacements ?? []) {
|
|
16
|
+
if (!replacement.from || !replacement.to) {
|
|
17
|
+
throw new Error(`Invalid import config: componentNameReplacements entries must have non-empty "from" and "to"`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return cfg;
|
|
21
|
+
}
|
|
22
|
+
export function applyComponentNameReplacements(name, replacements) {
|
|
23
|
+
return replacements.find(r => r.from === name)?.to ?? name;
|
|
15
24
|
}
|
|
16
25
|
function isImportConfig(value) {
|
|
17
26
|
return (typeof value === 'object' &&
|
|
@@ -37,3 +46,27 @@ export function buildOpenAPIConfig(spec, strategy, segment, pattern, component)
|
|
|
37
46
|
}
|
|
38
47
|
return { adapter: 'openapi', spec, componentMapping };
|
|
39
48
|
}
|
|
49
|
+
export function buildAsyncAPIConfig(spec, strategy, opts = {}) {
|
|
50
|
+
let componentMapping;
|
|
51
|
+
if (strategy === 'hardcoded') {
|
|
52
|
+
if (!opts.value)
|
|
53
|
+
throw new Error('--component required for hardcoded strategy');
|
|
54
|
+
componentMapping = { strategy: 'hardcoded', value: opts.value };
|
|
55
|
+
}
|
|
56
|
+
else if (strategy === 'tag') {
|
|
57
|
+
componentMapping = { strategy: 'tag' };
|
|
58
|
+
}
|
|
59
|
+
else if (strategy === 'channel-pattern') {
|
|
60
|
+
componentMapping = { strategy: 'channel-pattern', pattern: opts.pattern ?? '' };
|
|
61
|
+
}
|
|
62
|
+
else if (strategy === 'name-pattern') {
|
|
63
|
+
componentMapping = { strategy: 'name-pattern', pattern: opts.pattern ?? '' };
|
|
64
|
+
}
|
|
65
|
+
else if (strategy === 'name-segment') {
|
|
66
|
+
componentMapping = { strategy: 'name-segment', separator: opts.separator ?? '.', segment: opts.segment ?? 0 };
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
componentMapping = { strategy: 'channel-segment', separator: opts.separator ?? '.', segment: opts.segment ?? 0 };
|
|
70
|
+
}
|
|
71
|
+
return { adapter: 'asyncapi', spec, componentMapping };
|
|
72
|
+
}
|
|
@@ -20,7 +20,11 @@ export async function runImport(config, runtimeConfig) {
|
|
|
20
20
|
const specPath = path.resolve(entry.spec);
|
|
21
21
|
const resolvedEntry = { ...entry, spec: specPath };
|
|
22
22
|
const adapter = getAdapter(resolvedEntry.adapter);
|
|
23
|
-
const result = await adapter.import(resolvedEntry, {
|
|
23
|
+
const result = await adapter.import(resolvedEntry, {
|
|
24
|
+
packConfig,
|
|
25
|
+
templates: graph.templates,
|
|
26
|
+
componentNameReplacements: config.componentNameReplacements ?? [],
|
|
27
|
+
});
|
|
24
28
|
allDiagnostics.push(...result.diagnostics);
|
|
25
29
|
if (result.diagnostics.some(d => d.severity === 'error'))
|
|
26
30
|
continue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atolis-hq/corum",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@apidevtools/swagger-parser": "^12.1.0",
|
|
26
|
+
"@asyncapi/parser": "^3.6.0",
|
|
26
27
|
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
27
28
|
"@toon-format/toon": "^2.1.0",
|
|
28
29
|
"commander": "^14.0.3",
|