@eventcatalog/generator-openapi 7.7.1 → 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.1",
4070
+ version: "7.7.2",
4072
4071
  description: "OpenAPI generator for EventCatalog",
4073
4072
  scripts: {
4074
4073
  build: "tsup",
@@ -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)) {
@@ -4346,7 +4400,7 @@ Processing domain: ${domainName} (v${domainVersion})`));
4346
4400
  const persistPreviousSpecificationFiles = Array.isArray(serviceSpec.path) === false;
4347
4401
  if (latestServiceInCatalog) {
4348
4402
  serviceMarkdown = latestServiceInCatalog.markdown;
4349
- serviceSpecificationsFiles = latestVersionSpecificationFiles;
4403
+ serviceSpecificationsFiles = await getExistingSpecificationFiles(service.id, latestServiceInCatalog.specifications);
4350
4404
  sends = latestServiceInCatalog.sends || [];
4351
4405
  owners = latestServiceInCatalog.owners || [];
4352
4406
  repository = latestServiceInCatalog.repository || null;
@@ -4356,8 +4410,10 @@ Processing domain: ${domainName} (v${domainVersion})`));
4356
4410
  serviceDiagrams = latestServiceInCatalog.diagrams || null;
4357
4411
  serviceWritesTo = latestServiceInCatalog.writesTo || [];
4358
4412
  serviceReadsFrom = latestServiceInCatalog.readsFrom || [];
4359
- if (persistPreviousSpecificationFiles && latestServiceInCatalog.specifications) {
4360
- serviceSpecifications = mergeOpenApiIntoSpecifications(latestServiceInCatalog.specifications, service.schemaPath);
4413
+ if (persistPreviousSpecificationFiles) {
4414
+ serviceSpecifications = mergeSpecifications(latestServiceInCatalog.specifications, serviceSpecifications, {
4415
+ preferArray: true
4416
+ });
4361
4417
  }
4362
4418
  if (latestServiceInCatalog.version === version) {
4363
4419
  receives = latestServiceInCatalog.receives ? (
@@ -4411,6 +4467,35 @@ Processing domain: ${domainName} (v${domainVersion})`));
4411
4467
  }
4412
4468
  }
4413
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
+ };
4414
4499
  var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, options) => {
4415
4500
  const operations = await getOperationsByType(pathToSpec, options.httpMethodsToMessages, document2);
4416
4501
  const sidebarBadgeType = options.sidebarBadgeType || "HTTP_METHOD";