@eventcatalog/generator-openapi 7.7.0 → 7.7.2

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.mjs CHANGED
@@ -1528,6 +1528,7 @@ var require_dist2 = __commonJS({
1528
1528
  // src/index.ts
1529
1529
  import utils2 from "@eventcatalog/sdk";
1530
1530
  import { readFile } from "fs/promises";
1531
+ import fs2 from "fs/promises";
1531
1532
  import chalk4 from "chalk";
1532
1533
  import SwaggerParser2 from "@apidevtools/swagger-parser";
1533
1534
 
@@ -1574,9 +1575,7 @@ var buildService = (serviceOptions, document2, generateMarkdown) => {
1574
1575
  name: document2.info.title,
1575
1576
  summary: getSummary(document2),
1576
1577
  schemaPath,
1577
- specifications: {
1578
- openapiPath: schemaPath
1579
- },
1578
+ specifications: [{ type: "openapi", path: schemaPath }],
1580
1579
  markdown: generateMarkdown ? generateMarkdown({ service: serviceOptions, document: document2, markdown: generatedMarkdownForService }) : generatedMarkdownForService,
1581
1580
  badges: documentTags.map((tag2) => ({ content: tag2.name, textColor: "blue", backgroundColor: "blue" })),
1582
1581
  owners: serviceOptions.owners || [],
@@ -4068,7 +4067,7 @@ import { join } from "path";
4068
4067
  // package.json
4069
4068
  var package_default = {
4070
4069
  name: "@eventcatalog/generator-openapi",
4071
- version: "7.7.0",
4070
+ version: "7.7.2",
4072
4071
  description: "OpenAPI generator for EventCatalog",
4073
4072
  scripts: {
4074
4073
  build: "tsup",
@@ -4105,7 +4104,7 @@ var package_default = {
4105
4104
  dependencies: {
4106
4105
  "@apidevtools/swagger-parser": "^10.1.0",
4107
4106
  "@changesets/cli": "^2.27.7",
4108
- "@eventcatalog/sdk": "^2.9.6",
4107
+ "@eventcatalog/sdk": "2.12.1",
4109
4108
  chalk: "4.1.2",
4110
4109
  "js-yaml": "^4.1.0",
4111
4110
  "openapi-types": "^12.1.3",
@@ -4177,6 +4176,74 @@ var isVersionLessThan = (version, givenVersion) => {
4177
4176
  return satisfies(version, `<${givenVersion}`);
4178
4177
  };
4179
4178
 
4179
+ // src/utils/specifications.ts
4180
+ var toArray = (specifications) => {
4181
+ if (!specifications) return [];
4182
+ if (Array.isArray(specifications)) {
4183
+ return specifications.filter((spec) => spec?.type && spec?.path).map((spec) => ({
4184
+ type: spec.type,
4185
+ path: spec.path,
4186
+ ...spec.name ? { name: spec.name } : {},
4187
+ ...spec.headers ? { headers: spec.headers } : {}
4188
+ }));
4189
+ }
4190
+ const output = [];
4191
+ if (specifications.openapiPath) {
4192
+ output.push({ type: "openapi", path: specifications.openapiPath });
4193
+ }
4194
+ if (specifications.asyncapiPath) {
4195
+ output.push({ type: "asyncapi", path: specifications.asyncapiPath });
4196
+ }
4197
+ if (specifications.graphqlPath) {
4198
+ output.push({ type: "graphql", path: specifications.graphqlPath });
4199
+ }
4200
+ return output;
4201
+ };
4202
+ var dedupe = (specifications) => {
4203
+ const unique = /* @__PURE__ */ new Map();
4204
+ for (const spec of specifications) {
4205
+ const key = `${spec.type}:${spec.path}`;
4206
+ if (!unique.has(key)) {
4207
+ unique.set(key, spec);
4208
+ }
4209
+ }
4210
+ return [...unique.values()];
4211
+ };
4212
+ var canUseLegacyFormat = (specifications) => {
4213
+ const countByType = {
4214
+ openapi: 0,
4215
+ asyncapi: 0,
4216
+ graphql: 0
4217
+ };
4218
+ for (const spec of specifications) {
4219
+ countByType[spec.type] += 1;
4220
+ if (spec.name || spec.headers) {
4221
+ return false;
4222
+ }
4223
+ }
4224
+ return Object.values(countByType).every((count) => count <= 1);
4225
+ };
4226
+ var toLegacy = (specifications) => {
4227
+ const legacy = {};
4228
+ for (const spec of specifications) {
4229
+ if (spec.type === "openapi") legacy.openapiPath = spec.path;
4230
+ if (spec.type === "asyncapi") legacy.asyncapiPath = spec.path;
4231
+ if (spec.type === "graphql") legacy.graphqlPath = spec.path;
4232
+ }
4233
+ return legacy;
4234
+ };
4235
+ var mergeSpecifications = (existing, incoming, options) => {
4236
+ const merged = dedupe([...toArray(existing), ...toArray(incoming)]);
4237
+ if (merged.length === 0) return void 0;
4238
+ if (options?.preferArray) {
4239
+ return merged;
4240
+ }
4241
+ if (canUseLegacyFormat(merged)) {
4242
+ return toLegacy(merged);
4243
+ }
4244
+ return merged;
4245
+ };
4246
+
4180
4247
  // src/index.ts
4181
4248
  var toUniqueArray = (array) => {
4182
4249
  return array.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id && t.version === item.version));
@@ -4200,13 +4267,6 @@ var fetchAuthenticatedSpec = async (specUrl, headers) => {
4200
4267
  return yaml.load(content);
4201
4268
  }
4202
4269
  };
4203
- var mergeOpenApiIntoSpecifications = (existingSpecs, openapiFileName) => {
4204
- if (Array.isArray(existingSpecs)) {
4205
- return [...existingSpecs.filter((spec) => spec.type !== "openapi"), { type: "openapi", path: openapiFileName }];
4206
- }
4207
- const { openapiPath: _, ...rest } = existingSpecs || {};
4208
- return { openapiPath: openapiFileName, ...rest };
4209
- };
4210
4270
  var index_default = async (_, options) => {
4211
4271
  if (!process.env.PROJECT_DIR) {
4212
4272
  process.env.PROJECT_DIR = process.cwd();
@@ -4225,8 +4285,7 @@ var index_default = async (_, options) => {
4225
4285
  getService,
4226
4286
  versionService,
4227
4287
  writeService,
4228
- addFileToService,
4229
- getSpecificationFilesForService
4288
+ addFileToService
4230
4289
  } = utils2(process.env.PROJECT_DIR);
4231
4290
  const { services = [], saveParsedSpecFile = false } = options;
4232
4291
  for (const serviceSpec of services) {
@@ -4315,11 +4374,6 @@ Processing domain: ${domainName} (v${domainVersion})`));
4315
4374
  await addServiceToDomain(domainId, { id: service.id, version: service.version }, domainVersion);
4316
4375
  }
4317
4376
  const latestServiceInCatalog = await getService(service.id, "latest");
4318
- let latestVersionSpecificationFiles = [];
4319
- try {
4320
- latestVersionSpecificationFiles = await getSpecificationFilesForService(service.id, "latest");
4321
- } catch (error) {
4322
- }
4323
4377
  const versionTheService = latestServiceInCatalog && isVersionGreaterThan(version, latestServiceInCatalog.version);
4324
4378
  console.log(chalk4.blue(`Processing service: ${document2.info.title} (v${version})`));
4325
4379
  if (latestServiceInCatalog && isVersionLessThan(version, latestServiceInCatalog.version)) {
@@ -4340,22 +4394,26 @@ Processing domain: ${domainName} (v${domainVersion})`));
4340
4394
  let owners = service.owners || [];
4341
4395
  let repository = null;
4342
4396
  let styles2 = null;
4397
+ let serviceDiagrams = null;
4343
4398
  let serviceWritesTo = configuredWritesTo;
4344
4399
  let serviceReadsFrom = configuredReadsFrom;
4345
4400
  const persistPreviousSpecificationFiles = Array.isArray(serviceSpec.path) === false;
4346
4401
  if (latestServiceInCatalog) {
4347
4402
  serviceMarkdown = latestServiceInCatalog.markdown;
4348
- serviceSpecificationsFiles = latestVersionSpecificationFiles;
4403
+ serviceSpecificationsFiles = await getExistingSpecificationFiles(service.id, latestServiceInCatalog.specifications);
4349
4404
  sends = latestServiceInCatalog.sends || [];
4350
4405
  owners = latestServiceInCatalog.owners || [];
4351
4406
  repository = latestServiceInCatalog.repository || null;
4352
4407
  styles2 = latestServiceInCatalog.styles || null;
4353
4408
  serviceBadges = latestServiceInCatalog.badges || null;
4354
4409
  serviceAttachments = latestServiceInCatalog.attachments || null;
4410
+ serviceDiagrams = latestServiceInCatalog.diagrams || null;
4355
4411
  serviceWritesTo = latestServiceInCatalog.writesTo || [];
4356
4412
  serviceReadsFrom = latestServiceInCatalog.readsFrom || [];
4357
- if (persistPreviousSpecificationFiles && latestServiceInCatalog.specifications) {
4358
- serviceSpecifications = mergeOpenApiIntoSpecifications(latestServiceInCatalog.specifications, service.schemaPath);
4413
+ if (persistPreviousSpecificationFiles) {
4414
+ serviceSpecifications = mergeSpecifications(latestServiceInCatalog.specifications, serviceSpecifications, {
4415
+ preferArray: true
4416
+ });
4359
4417
  }
4360
4418
  if (latestServiceInCatalog.version === version) {
4361
4419
  receives = latestServiceInCatalog.receives ? (
@@ -4379,6 +4437,7 @@ Processing domain: ${domainName} (v${domainVersion})`));
4379
4437
  ...owners ? { owners } : {},
4380
4438
  ...repository ? { repository } : {},
4381
4439
  ...styles2 ? { styles: styles2 } : {},
4440
+ ...serviceDiagrams ? { diagrams: serviceDiagrams } : {},
4382
4441
  ...isServiceMarkedAsDraft ? { draft: true } : {},
4383
4442
  ...serviceAttachments ? { attachments: serviceAttachments } : {},
4384
4443
  ...serviceWritesTo.length > 0 ? { writesTo: serviceWritesTo } : {},
@@ -4408,6 +4467,35 @@ Processing domain: ${domainName} (v${domainVersion})`));
4408
4467
  }
4409
4468
  }
4410
4469
  };
4470
+ var toSpecificationEntries = (specifications) => {
4471
+ if (!specifications) return [];
4472
+ if (Array.isArray(specifications)) {
4473
+ return specifications.filter((spec) => Boolean(spec?.type && spec?.path));
4474
+ }
4475
+ const entries = [];
4476
+ if (specifications.openapiPath) entries.push({ type: "openapi", path: specifications.openapiPath });
4477
+ if (specifications.asyncapiPath) entries.push({ type: "asyncapi", path: specifications.asyncapiPath });
4478
+ if (specifications.graphqlPath) entries.push({ type: "graphql", path: specifications.graphqlPath });
4479
+ return entries;
4480
+ };
4481
+ var getExistingSpecificationFiles = async (serviceId, specifications) => {
4482
+ const entries = toSpecificationEntries(specifications);
4483
+ const uniqueEntries = entries.filter(
4484
+ (entry, index, all) => all.findIndex((candidate) => candidate.path === entry.path) === index
4485
+ );
4486
+ const files = await Promise.all(
4487
+ uniqueEntries.map(async (entry) => {
4488
+ const filePath = join(process.env.PROJECT_DIR, "services", serviceId, entry.path);
4489
+ try {
4490
+ const content = await fs2.readFile(filePath, "utf8");
4491
+ return { fileName: entry.path, content };
4492
+ } catch (error) {
4493
+ return void 0;
4494
+ }
4495
+ })
4496
+ );
4497
+ return files.filter(Boolean);
4498
+ };
4411
4499
  var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, options) => {
4412
4500
  const operations = await getOperationsByType(pathToSpec, options.httpMethodsToMessages, document2);
4413
4501
  const sidebarBadgeType = options.sidebarBadgeType || "HTTP_METHOD";