@eventcatalog/generator-openapi 7.8.0 → 7.9.1

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
@@ -15,6 +15,7 @@ type Props = {
15
15
  sidebarBadgeType?: 'HTTP_METHOD' | 'MESSAGE_TYPE';
16
16
  httpMethodsToMessages?: HTTP_METHOD_TO_MESSAGE_TYPE;
17
17
  preserveExistingMessages?: boolean;
18
+ parseExamples?: boolean;
18
19
  };
19
20
  declare const _default: (_: any, options: Props) => Promise<void>;
20
21
 
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ type Props = {
15
15
  sidebarBadgeType?: 'HTTP_METHOD' | 'MESSAGE_TYPE';
16
16
  httpMethodsToMessages?: HTTP_METHOD_TO_MESSAGE_TYPE;
17
17
  preserveExistingMessages?: boolean;
18
+ parseExamples?: boolean;
18
19
  };
19
20
  declare const _default: (_: any, options: Props) => Promise<void>;
20
21
 
package/dist/index.js CHANGED
@@ -1610,15 +1610,19 @@ async function getSchemasByOperationId(filePath, operationId, parsedDocument) {
1610
1610
  }
1611
1611
  if (typedOperation.requestBody && typedOperation.requestBody.content) {
1612
1612
  const contentType = Object.keys(typedOperation.requestBody.content)[0];
1613
- schemas.requestBody = typedOperation.requestBody.content[contentType].schema;
1613
+ if (contentType) {
1614
+ schemas.requestBody = typedOperation.requestBody.content[contentType].schema;
1615
+ }
1614
1616
  }
1615
1617
  if (typedOperation.responses) {
1616
1618
  for (const [statusCode, response] of Object.entries(typedOperation.responses)) {
1617
1619
  if (response.content) {
1618
1620
  const contentType = Object.keys(response.content)[0];
1619
- const schemaOrContent = response.content[contentType].schema || response.content[contentType];
1620
- schemas.responses[statusCode] = { ...schemaOrContent };
1621
- schemas.responses[statusCode].isSchema = !!response.content[contentType].schema;
1621
+ if (contentType) {
1622
+ const schemaOrContent = response.content[contentType].schema || response.content[contentType];
1623
+ schemas.responses[statusCode] = { ...schemaOrContent };
1624
+ schemas.responses[statusCode].isSchema = !!response.content[contentType].schema;
1625
+ }
1622
1626
  }
1623
1627
  }
1624
1628
  }
@@ -1632,6 +1636,57 @@ async function getSchemasByOperationId(filePath, operationId, parsedDocument) {
1632
1636
  return;
1633
1637
  }
1634
1638
  }
1639
+ async function getExamplesByOperationId(filePath, operationId, parsedDocument) {
1640
+ const api = parsedDocument || await import_swagger_parser.default.dereference(filePath);
1641
+ const examples = [];
1642
+ for (const [, pathItem] of Object.entries(api.paths)) {
1643
+ for (const [, operation] of Object.entries(pathItem)) {
1644
+ const typedOperation = operation;
1645
+ if (typedOperation.operationId !== operationId) continue;
1646
+ if (typedOperation.requestBody?.content) {
1647
+ const contentType = Object.keys(typedOperation.requestBody.content)[0];
1648
+ if (contentType) {
1649
+ const mediaType = typedOperation.requestBody.content[contentType];
1650
+ if (mediaType.example) {
1651
+ examples.push({ fileName: "example.json", content: JSON.stringify(mediaType.example, null, 2) });
1652
+ }
1653
+ if (mediaType.examples) {
1654
+ for (const [name, exampleObj] of Object.entries(mediaType.examples)) {
1655
+ if (exampleObj.value) {
1656
+ examples.push({ fileName: `${name}.json`, content: JSON.stringify(exampleObj.value, null, 2) });
1657
+ }
1658
+ }
1659
+ }
1660
+ }
1661
+ }
1662
+ if (typedOperation.responses) {
1663
+ for (const [statusCode, response] of Object.entries(typedOperation.responses)) {
1664
+ if (response.content) {
1665
+ const contentType = Object.keys(response.content)[0];
1666
+ if (contentType) {
1667
+ const mediaType = response.content[contentType];
1668
+ if (mediaType.example) {
1669
+ examples.push({ fileName: `response-${statusCode}.json`, content: JSON.stringify(mediaType.example, null, 2) });
1670
+ }
1671
+ if (mediaType.examples) {
1672
+ for (const [name, exampleObj] of Object.entries(mediaType.examples)) {
1673
+ if (exampleObj.value) {
1674
+ examples.push({
1675
+ fileName: `response-${statusCode}-${name}.json`,
1676
+ content: JSON.stringify(exampleObj.value, null, 2)
1677
+ });
1678
+ }
1679
+ }
1680
+ }
1681
+ }
1682
+ }
1683
+ }
1684
+ }
1685
+ return examples;
1686
+ }
1687
+ }
1688
+ return examples;
1689
+ }
1635
1690
  function getDeprecatedValues(openAPIOperation) {
1636
1691
  const deprecatedDate = openAPIOperation["x-eventcatalog-deprecated-date"] || null;
1637
1692
  const deprecatedMessage = openAPIOperation["x-eventcatalog-deprecated-message"] || null;
@@ -1889,7 +1944,10 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1889
1944
  getQuery,
1890
1945
  rmQueryById,
1891
1946
  writeQuery,
1892
- addFileToQuery
1947
+ addFileToQuery,
1948
+ addExampleToEvent,
1949
+ addExampleToCommand,
1950
+ addExampleToQuery
1893
1951
  } = (0, import_sdk.default)(projectDirectory);
1894
1952
  const messageTypeMap = {
1895
1953
  event: {
@@ -1898,6 +1956,7 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1898
1956
  rmMessageById: rmEventById,
1899
1957
  writeMessage: writeEvent,
1900
1958
  addFileToMessage: addFileToEvent,
1959
+ addExampleToMessage: addExampleToEvent,
1901
1960
  collection: "events"
1902
1961
  },
1903
1962
  command: {
@@ -1906,6 +1965,7 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1906
1965
  rmMessageById: rmCommandById,
1907
1966
  writeMessage: writeCommand,
1908
1967
  addFileToMessage: addFileToCommand,
1968
+ addExampleToMessage: addExampleToCommand,
1909
1969
  collection: "commands"
1910
1970
  },
1911
1971
  query: {
@@ -1914,6 +1974,7 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1914
1974
  rmMessageById: rmQueryById,
1915
1975
  writeMessage: writeQuery,
1916
1976
  addFileToMessage: addFileToQuery,
1977
+ addExampleToMessage: addExampleToQuery,
1917
1978
  collection: "queries"
1918
1979
  }
1919
1980
  };
@@ -4152,7 +4213,7 @@ var import_node_path = require("path");
4152
4213
  // package.json
4153
4214
  var package_default = {
4154
4215
  name: "@eventcatalog/generator-openapi",
4155
- version: "7.8.0",
4216
+ version: "7.9.1",
4156
4217
  description: "OpenAPI generator for EventCatalog",
4157
4218
  scripts: {
4158
4219
  build: "tsup",
@@ -4189,7 +4250,7 @@ var package_default = {
4189
4250
  dependencies: {
4190
4251
  "@apidevtools/swagger-parser": "^10.1.0",
4191
4252
  "@changesets/cli": "^2.27.7",
4192
- "@eventcatalog/sdk": "2.12.1",
4253
+ "@eventcatalog/sdk": "2.17.4",
4193
4254
  chalk: "4.1.2",
4194
4255
  "js-yaml": "^4.1.0",
4195
4256
  "openapi-types": "^12.1.3",
@@ -4586,6 +4647,7 @@ var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, o
4586
4647
  const sidebarBadgeType = options.sidebarBadgeType || "HTTP_METHOD";
4587
4648
  const version = options.serviceVersion || document2.info.version;
4588
4649
  const preserveExistingMessages = options.preserveExistingMessages ?? true;
4650
+ const parseExamples = options.parseExamples ?? true;
4589
4651
  const isDraft = options.isDraft ?? null;
4590
4652
  let receives = [], sends = [];
4591
4653
  for (const operation of operations) {
@@ -4606,6 +4668,7 @@ var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, o
4606
4668
  console.log(import_chalk4.default.blue(`Processing message: ${message2.name} (v${message2.version})`));
4607
4669
  const {
4608
4670
  addFileToMessage,
4671
+ addExampleToMessage,
4609
4672
  writeMessage,
4610
4673
  getMessage,
4611
4674
  versionMessage,
@@ -4692,6 +4755,15 @@ var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, o
4692
4755
  );
4693
4756
  }
4694
4757
  }
4758
+ if (parseExamples && operation.operationId) {
4759
+ const operationExamples = await getExamplesByOperationId(pathToSpec, operation.operationId, document2);
4760
+ for (const example of operationExamples) {
4761
+ await addExampleToMessage(message2.id, { content: example.content, fileName: example.fileName }, message2.version);
4762
+ }
4763
+ if (operationExamples.length > 0) {
4764
+ console.log(import_chalk4.default.cyan(` - ${operationExamples.length} example(s) added to message (v${message2.version})`));
4765
+ }
4766
+ }
4695
4767
  console.log(import_chalk4.default.cyan(` - Message (v${message2.version}) created`));
4696
4768
  if (!operation.operationId) {
4697
4769
  console.log(import_chalk4.default.yellow(` - OperationId not found for ${operation.method} ${operation.path}, creating one...`));