@eventcatalog/generator-asyncapi 5.5.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -241,6 +241,7 @@ declare const optionsSchema: z.ZodObject<{
241
241
  debug: z.ZodOptional<z.ZodBoolean>;
242
242
  parseSchemas: z.ZodOptional<z.ZodBoolean>;
243
243
  parseChannels: z.ZodOptional<z.ZodBoolean>;
244
+ attachHeadersToSchema: z.ZodOptional<z.ZodBoolean>;
244
245
  saveParsedSpecFile: z.ZodOptional<z.ZodBoolean>;
245
246
  }, "strip", z.ZodTypeAny, {
246
247
  services: {
@@ -303,6 +304,7 @@ declare const optionsSchema: z.ZodObject<{
303
304
  debug?: boolean | undefined;
304
305
  parseSchemas?: boolean | undefined;
305
306
  parseChannels?: boolean | undefined;
307
+ attachHeadersToSchema?: boolean | undefined;
306
308
  saveParsedSpecFile?: boolean | undefined;
307
309
  }, {
308
310
  services: {
@@ -365,6 +367,7 @@ declare const optionsSchema: z.ZodObject<{
365
367
  debug?: boolean | undefined;
366
368
  parseSchemas?: boolean | undefined;
367
369
  parseChannels?: boolean | undefined;
370
+ attachHeadersToSchema?: boolean | undefined;
368
371
  saveParsedSpecFile?: boolean | undefined;
369
372
  }>;
370
373
  type Props = z.infer<typeof optionsSchema>;
package/dist/index.d.ts CHANGED
@@ -241,6 +241,7 @@ declare const optionsSchema: z.ZodObject<{
241
241
  debug: z.ZodOptional<z.ZodBoolean>;
242
242
  parseSchemas: z.ZodOptional<z.ZodBoolean>;
243
243
  parseChannels: z.ZodOptional<z.ZodBoolean>;
244
+ attachHeadersToSchema: z.ZodOptional<z.ZodBoolean>;
244
245
  saveParsedSpecFile: z.ZodOptional<z.ZodBoolean>;
245
246
  }, "strip", z.ZodTypeAny, {
246
247
  services: {
@@ -303,6 +304,7 @@ declare const optionsSchema: z.ZodObject<{
303
304
  debug?: boolean | undefined;
304
305
  parseSchemas?: boolean | undefined;
305
306
  parseChannels?: boolean | undefined;
307
+ attachHeadersToSchema?: boolean | undefined;
306
308
  saveParsedSpecFile?: boolean | undefined;
307
309
  }, {
308
310
  services: {
@@ -365,6 +367,7 @@ declare const optionsSchema: z.ZodObject<{
365
367
  debug?: boolean | undefined;
366
368
  parseSchemas?: boolean | undefined;
367
369
  parseChannels?: boolean | undefined;
370
+ attachHeadersToSchema?: boolean | undefined;
368
371
  saveParsedSpecFile?: boolean | undefined;
369
372
  }>;
370
373
  type Props = z.infer<typeof optionsSchema>;
package/dist/index.js CHANGED
@@ -1543,7 +1543,7 @@ var import_path3 = __toESM(require("path"));
1543
1543
  // package.json
1544
1544
  var package_default = {
1545
1545
  name: "@eventcatalog/generator-asyncapi",
1546
- version: "5.5.0",
1546
+ version: "6.0.0",
1547
1547
  description: "AsyncAPI generator for EventCatalog",
1548
1548
  scripts: {
1549
1549
  build: "tsup",
@@ -2651,30 +2651,6 @@ var getSchemaFileName = (message2) => {
2651
2651
  var getMessageName = (message2) => {
2652
2652
  return message2.hasTitle() && message2.title() ? message2.title() : message2.id();
2653
2653
  };
2654
- var getChannelsForMessage = (message2, channels, document2) => {
2655
- let channelsForMessage = [];
2656
- const globalVersion = document2.info().version();
2657
- for (const channel of channels) {
2658
- for (const channelMessage of channel.messages()) {
2659
- if (channelMessage.id() === message2.id()) {
2660
- channelsForMessage.push(channel);
2661
- }
2662
- }
2663
- }
2664
- for (const messageChannel of message2.channels()) {
2665
- channelsForMessage.push(messageChannel);
2666
- }
2667
- const uniqueChannels = channelsForMessage.filter(
2668
- (channel, index, self) => index === self.findIndex((t) => t.id() === channel.id())
2669
- );
2670
- return uniqueChannels.map((channel) => {
2671
- const channelVersion = channel.extensions().get("x-eventcatalog-channel-version")?.value() || globalVersion;
2672
- return {
2673
- id: channel.id(),
2674
- version: channelVersion
2675
- };
2676
- });
2677
- };
2678
2654
 
2679
2655
  // src/utils/services.ts
2680
2656
  var defaultMarkdown2 = (document2) => {
@@ -4163,6 +4139,7 @@ var optionsSchema = import_zod.z.object({
4163
4139
  debug: import_zod.z.boolean().optional(),
4164
4140
  parseSchemas: import_zod.z.boolean().optional(),
4165
4141
  parseChannels: import_zod.z.boolean().optional(),
4142
+ attachHeadersToSchema: import_zod.z.boolean().optional(),
4166
4143
  saveParsedSpecFile: import_zod.z.boolean({ invalid_type_error: "The saveParsedSpecFile is not a boolean in options" }).optional()
4167
4144
  });
4168
4145
  var toUniqueArray = (array) => {
@@ -4241,7 +4218,13 @@ var index_default = async (config, options) => {
4241
4218
  }
4242
4219
  };
4243
4220
  validateOptions(options);
4244
- const { services, saveParsedSpecFile = false, parseSchemas = true, parseChannels = false } = options;
4221
+ const {
4222
+ services,
4223
+ saveParsedSpecFile = false,
4224
+ parseSchemas = true,
4225
+ parseChannels = false,
4226
+ attachHeadersToSchema = false
4227
+ } = options;
4245
4228
  console.log(import_chalk4.default.green(`Processing ${services.length} AsyncAPI files...`));
4246
4229
  for (const service of services) {
4247
4230
  console.log(import_chalk4.default.gray(`Processing ${service.path}`));
@@ -4419,7 +4402,6 @@ Processing domain: ${domainName} (v${domainVersion})`));
4419
4402
  console.log(import_chalk4.default.cyan(` - Versioned previous message: (v${catalogedMessage.version})`));
4420
4403
  }
4421
4404
  }
4422
- const channelsForMessage = parseChannels ? getChannelsForMessage(message2, channels, document2) : [];
4423
4405
  await writeMessage(
4424
4406
  {
4425
4407
  id: messageId,
@@ -4431,7 +4413,6 @@ Processing domain: ${domainName} (v${domainVersion})`));
4431
4413
  ...messageHasSchema(message2) && { schemaPath: getSchemaFileName(message2) },
4432
4414
  ...owners && { owners },
4433
4415
  ...messageAttachments && { attachments: messageAttachments },
4434
- ...channelsForMessage.length > 0 && { channels: channelsForMessage },
4435
4416
  ...deprecatedDate && {
4436
4417
  deprecated: { date: deprecatedDate, ...deprecatedMessage && { message: deprecatedMessage } }
4437
4418
  },
@@ -4444,10 +4425,7 @@ Processing domain: ${domainName} (v${domainVersion})`));
4444
4425
  );
4445
4426
  console.log(import_chalk4.default.cyan(` - Message (v${messageVersion}) created`));
4446
4427
  if (messageHasSchema(message2)) {
4447
- let schema = message2.payload()?.extensions()?.get("x-parser-original-payload")?.json() || message2.payload()?.json();
4448
- if (schema?.schema) {
4449
- schema = schema.schema;
4450
- }
4428
+ const schema = getSchemaForMessage(message2, attachHeadersToSchema);
4451
4429
  const cleanedMessagePath = messagePath.replace(/\\/g, "/").replace(/^(\.\.\/|\.\/)+/g, "");
4452
4430
  await addSchemaToMessage(
4453
4431
  messageId,
@@ -4463,8 +4441,24 @@ Processing domain: ${domainName} (v${domainVersion})`));
4463
4441
  } else {
4464
4442
  console.log(import_chalk4.default.yellow(` - Skipping external message: ${getMessageName(message2)}(v${messageVersion})`));
4465
4443
  }
4466
- if (isSent) sends.push({ id: messageId, version: messageVersion });
4467
- if (isReceived) receives.push({ id: messageId, version: messageVersion });
4444
+ if (parseChannels) {
4445
+ const operationChannels = operation.channels().all();
4446
+ const channelPointers = operationChannels.map((channel) => {
4447
+ const explicitVersion = channel.extensions().get("x-eventcatalog-channel-version")?.value();
4448
+ return { id: channel.id(), ...explicitVersion && { version: explicitVersion } };
4449
+ });
4450
+ if (isSent)
4451
+ sends.push({ id: messageId, version: messageVersion, ...channelPointers.length > 0 && { to: channelPointers } });
4452
+ if (isReceived)
4453
+ receives.push({
4454
+ id: messageId,
4455
+ version: messageVersion,
4456
+ ...channelPointers.length > 0 && { from: channelPointers }
4457
+ });
4458
+ } else {
4459
+ if (isSent) sends.push({ id: messageId, version: messageVersion });
4460
+ if (isReceived) receives.push({ id: messageId, version: messageVersion });
4461
+ }
4468
4462
  }
4469
4463
  }
4470
4464
  const latestServiceInCatalog = await getService(serviceId, "latest");
@@ -4568,6 +4562,36 @@ Failed to request AsyncAPI file from ${service.path}`));
4568
4562
  return await (0, import_promises.readFile)(service.path, "utf8");
4569
4563
  }
4570
4564
  };
4565
+ var getSchemaForMessage = (message2, attachHeadersToSchema) => {
4566
+ const payloadSchema = getSchemaFromMessagePart(
4567
+ message2.payload()?.extensions()?.get("x-parser-original-payload")?.json() || message2.payload()?.json()
4568
+ );
4569
+ if (!attachHeadersToSchema || !message2.hasHeaders() || !isJSONSchemaMessage(message2)) {
4570
+ return payloadSchema;
4571
+ }
4572
+ const headersSchema = getSchemaFromMessagePart(
4573
+ message2.headers()?.extensions()?.get("x-parser-original-payload")?.json() || message2.headers()?.json()
4574
+ );
4575
+ if (!headersSchema) {
4576
+ return payloadSchema;
4577
+ }
4578
+ return {
4579
+ type: "object",
4580
+ properties: {
4581
+ headers: headersSchema,
4582
+ payload: payloadSchema
4583
+ }
4584
+ };
4585
+ };
4586
+ var getSchemaFromMessagePart = (schema) => {
4587
+ if (schema?.schema) {
4588
+ return schema.schema;
4589
+ }
4590
+ return schema;
4591
+ };
4592
+ var isJSONSchemaMessage = (message2) => {
4593
+ return getSchemaFileName(message2).endsWith(".json");
4594
+ };
4571
4595
  var isServiceMessageOwner = (message2, operation) => {
4572
4596
  const operationRole = operation?.extensions?.().get?.("x-eventcatalog-role")?.value?.();
4573
4597
  const messageRole = message2.extensions().get("x-eventcatalog-role")?.value();