@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.js CHANGED
@@ -1533,6 +1533,7 @@ __export(index_exports, {
1533
1533
  module.exports = __toCommonJS(index_exports);
1534
1534
  var import_sdk2 = __toESM(require("@eventcatalog/sdk"));
1535
1535
  var import_promises = require("fs/promises");
1536
+ var import_promises2 = __toESM(require("fs/promises"));
1536
1537
  var import_chalk4 = __toESM(require("chalk"));
1537
1538
  var import_swagger_parser2 = __toESM(require("@apidevtools/swagger-parser"));
1538
1539
 
@@ -1579,9 +1580,7 @@ var buildService = (serviceOptions, document2, generateMarkdown) => {
1579
1580
  name: document2.info.title,
1580
1581
  summary: getSummary(document2),
1581
1582
  schemaPath,
1582
- specifications: {
1583
- openapiPath: schemaPath
1584
- },
1583
+ specifications: [{ type: "openapi", path: schemaPath }],
1585
1584
  markdown: generateMarkdown ? generateMarkdown({ service: serviceOptions, document: document2, markdown: generatedMarkdownForService }) : generatedMarkdownForService,
1586
1585
  badges: documentTags.map((tag2) => ({ content: tag2.name, textColor: "blue", backgroundColor: "blue" })),
1587
1586
  owners: serviceOptions.owners || [],
@@ -4073,7 +4072,7 @@ var import_node_path = require("path");
4073
4072
  // package.json
4074
4073
  var package_default = {
4075
4074
  name: "@eventcatalog/generator-openapi",
4076
- version: "7.7.1",
4075
+ version: "7.7.2",
4077
4076
  description: "OpenAPI generator for EventCatalog",
4078
4077
  scripts: {
4079
4078
  build: "tsup",
@@ -4182,6 +4181,74 @@ var isVersionLessThan = (version, givenVersion) => {
4182
4181
  return (0, import_semver.satisfies)(version, `<${givenVersion}`);
4183
4182
  };
4184
4183
 
4184
+ // src/utils/specifications.ts
4185
+ var toArray = (specifications) => {
4186
+ if (!specifications) return [];
4187
+ if (Array.isArray(specifications)) {
4188
+ return specifications.filter((spec) => spec?.type && spec?.path).map((spec) => ({
4189
+ type: spec.type,
4190
+ path: spec.path,
4191
+ ...spec.name ? { name: spec.name } : {},
4192
+ ...spec.headers ? { headers: spec.headers } : {}
4193
+ }));
4194
+ }
4195
+ const output = [];
4196
+ if (specifications.openapiPath) {
4197
+ output.push({ type: "openapi", path: specifications.openapiPath });
4198
+ }
4199
+ if (specifications.asyncapiPath) {
4200
+ output.push({ type: "asyncapi", path: specifications.asyncapiPath });
4201
+ }
4202
+ if (specifications.graphqlPath) {
4203
+ output.push({ type: "graphql", path: specifications.graphqlPath });
4204
+ }
4205
+ return output;
4206
+ };
4207
+ var dedupe = (specifications) => {
4208
+ const unique = /* @__PURE__ */ new Map();
4209
+ for (const spec of specifications) {
4210
+ const key = `${spec.type}:${spec.path}`;
4211
+ if (!unique.has(key)) {
4212
+ unique.set(key, spec);
4213
+ }
4214
+ }
4215
+ return [...unique.values()];
4216
+ };
4217
+ var canUseLegacyFormat = (specifications) => {
4218
+ const countByType = {
4219
+ openapi: 0,
4220
+ asyncapi: 0,
4221
+ graphql: 0
4222
+ };
4223
+ for (const spec of specifications) {
4224
+ countByType[spec.type] += 1;
4225
+ if (spec.name || spec.headers) {
4226
+ return false;
4227
+ }
4228
+ }
4229
+ return Object.values(countByType).every((count) => count <= 1);
4230
+ };
4231
+ var toLegacy = (specifications) => {
4232
+ const legacy = {};
4233
+ for (const spec of specifications) {
4234
+ if (spec.type === "openapi") legacy.openapiPath = spec.path;
4235
+ if (spec.type === "asyncapi") legacy.asyncapiPath = spec.path;
4236
+ if (spec.type === "graphql") legacy.graphqlPath = spec.path;
4237
+ }
4238
+ return legacy;
4239
+ };
4240
+ var mergeSpecifications = (existing, incoming, options) => {
4241
+ const merged = dedupe([...toArray(existing), ...toArray(incoming)]);
4242
+ if (merged.length === 0) return void 0;
4243
+ if (options?.preferArray) {
4244
+ return merged;
4245
+ }
4246
+ if (canUseLegacyFormat(merged)) {
4247
+ return toLegacy(merged);
4248
+ }
4249
+ return merged;
4250
+ };
4251
+
4185
4252
  // src/index.ts
4186
4253
  var toUniqueArray = (array) => {
4187
4254
  return array.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id && t.version === item.version));
@@ -4205,13 +4272,6 @@ var fetchAuthenticatedSpec = async (specUrl, headers) => {
4205
4272
  return import_js_yaml.default.load(content);
4206
4273
  }
4207
4274
  };
4208
- var mergeOpenApiIntoSpecifications = (existingSpecs, openapiFileName) => {
4209
- if (Array.isArray(existingSpecs)) {
4210
- return [...existingSpecs.filter((spec) => spec.type !== "openapi"), { type: "openapi", path: openapiFileName }];
4211
- }
4212
- const { openapiPath: _, ...rest } = existingSpecs || {};
4213
- return { openapiPath: openapiFileName, ...rest };
4214
- };
4215
4275
  var index_default = async (_, options) => {
4216
4276
  if (!process.env.PROJECT_DIR) {
4217
4277
  process.env.PROJECT_DIR = process.cwd();
@@ -4230,8 +4290,7 @@ var index_default = async (_, options) => {
4230
4290
  getService,
4231
4291
  versionService,
4232
4292
  writeService,
4233
- addFileToService,
4234
- getSpecificationFilesForService
4293
+ addFileToService
4235
4294
  } = (0, import_sdk2.default)(process.env.PROJECT_DIR);
4236
4295
  const { services = [], saveParsedSpecFile = false } = options;
4237
4296
  for (const serviceSpec of services) {
@@ -4320,11 +4379,6 @@ Processing domain: ${domainName} (v${domainVersion})`));
4320
4379
  await addServiceToDomain(domainId, { id: service.id, version: service.version }, domainVersion);
4321
4380
  }
4322
4381
  const latestServiceInCatalog = await getService(service.id, "latest");
4323
- let latestVersionSpecificationFiles = [];
4324
- try {
4325
- latestVersionSpecificationFiles = await getSpecificationFilesForService(service.id, "latest");
4326
- } catch (error) {
4327
- }
4328
4382
  const versionTheService = latestServiceInCatalog && isVersionGreaterThan(version, latestServiceInCatalog.version);
4329
4383
  console.log(import_chalk4.default.blue(`Processing service: ${document2.info.title} (v${version})`));
4330
4384
  if (latestServiceInCatalog && isVersionLessThan(version, latestServiceInCatalog.version)) {
@@ -4351,7 +4405,7 @@ Processing domain: ${domainName} (v${domainVersion})`));
4351
4405
  const persistPreviousSpecificationFiles = Array.isArray(serviceSpec.path) === false;
4352
4406
  if (latestServiceInCatalog) {
4353
4407
  serviceMarkdown = latestServiceInCatalog.markdown;
4354
- serviceSpecificationsFiles = latestVersionSpecificationFiles;
4408
+ serviceSpecificationsFiles = await getExistingSpecificationFiles(service.id, latestServiceInCatalog.specifications);
4355
4409
  sends = latestServiceInCatalog.sends || [];
4356
4410
  owners = latestServiceInCatalog.owners || [];
4357
4411
  repository = latestServiceInCatalog.repository || null;
@@ -4361,8 +4415,10 @@ Processing domain: ${domainName} (v${domainVersion})`));
4361
4415
  serviceDiagrams = latestServiceInCatalog.diagrams || null;
4362
4416
  serviceWritesTo = latestServiceInCatalog.writesTo || [];
4363
4417
  serviceReadsFrom = latestServiceInCatalog.readsFrom || [];
4364
- if (persistPreviousSpecificationFiles && latestServiceInCatalog.specifications) {
4365
- serviceSpecifications = mergeOpenApiIntoSpecifications(latestServiceInCatalog.specifications, service.schemaPath);
4418
+ if (persistPreviousSpecificationFiles) {
4419
+ serviceSpecifications = mergeSpecifications(latestServiceInCatalog.specifications, serviceSpecifications, {
4420
+ preferArray: true
4421
+ });
4366
4422
  }
4367
4423
  if (latestServiceInCatalog.version === version) {
4368
4424
  receives = latestServiceInCatalog.receives ? (
@@ -4416,6 +4472,35 @@ Processing domain: ${domainName} (v${domainVersion})`));
4416
4472
  }
4417
4473
  }
4418
4474
  };
4475
+ var toSpecificationEntries = (specifications) => {
4476
+ if (!specifications) return [];
4477
+ if (Array.isArray(specifications)) {
4478
+ return specifications.filter((spec) => Boolean(spec?.type && spec?.path));
4479
+ }
4480
+ const entries = [];
4481
+ if (specifications.openapiPath) entries.push({ type: "openapi", path: specifications.openapiPath });
4482
+ if (specifications.asyncapiPath) entries.push({ type: "asyncapi", path: specifications.asyncapiPath });
4483
+ if (specifications.graphqlPath) entries.push({ type: "graphql", path: specifications.graphqlPath });
4484
+ return entries;
4485
+ };
4486
+ var getExistingSpecificationFiles = async (serviceId, specifications) => {
4487
+ const entries = toSpecificationEntries(specifications);
4488
+ const uniqueEntries = entries.filter(
4489
+ (entry, index, all) => all.findIndex((candidate) => candidate.path === entry.path) === index
4490
+ );
4491
+ const files = await Promise.all(
4492
+ uniqueEntries.map(async (entry) => {
4493
+ const filePath = (0, import_node_path.join)(process.env.PROJECT_DIR, "services", serviceId, entry.path);
4494
+ try {
4495
+ const content = await import_promises2.default.readFile(filePath, "utf8");
4496
+ return { fileName: entry.path, content };
4497
+ } catch (error) {
4498
+ return void 0;
4499
+ }
4500
+ })
4501
+ );
4502
+ return files.filter(Boolean);
4503
+ };
4419
4504
  var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, options) => {
4420
4505
  const operations = await getOperationsByType(pathToSpec, options.httpMethodsToMessages, document2);
4421
4506
  const sidebarBadgeType = options.sidebarBadgeType || "HTTP_METHOD";