@eventcatalog/generator-openapi 7.8.0 → 7.9.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
@@ -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
@@ -1632,6 +1632,53 @@ async function getSchemasByOperationId(filePath, operationId, parsedDocument) {
1632
1632
  return;
1633
1633
  }
1634
1634
  }
1635
+ async function getExamplesByOperationId(filePath, operationId, parsedDocument) {
1636
+ const api = parsedDocument || await import_swagger_parser.default.dereference(filePath);
1637
+ const examples = [];
1638
+ for (const [, pathItem] of Object.entries(api.paths)) {
1639
+ for (const [, operation] of Object.entries(pathItem)) {
1640
+ const typedOperation = operation;
1641
+ if (typedOperation.operationId !== operationId) continue;
1642
+ if (typedOperation.requestBody?.content) {
1643
+ const contentType = Object.keys(typedOperation.requestBody.content)[0];
1644
+ const mediaType = typedOperation.requestBody.content[contentType];
1645
+ if (mediaType.example) {
1646
+ examples.push({ fileName: "example.json", content: JSON.stringify(mediaType.example, null, 2) });
1647
+ }
1648
+ if (mediaType.examples) {
1649
+ for (const [name, exampleObj] of Object.entries(mediaType.examples)) {
1650
+ if (exampleObj.value) {
1651
+ examples.push({ fileName: `${name}.json`, content: JSON.stringify(exampleObj.value, null, 2) });
1652
+ }
1653
+ }
1654
+ }
1655
+ }
1656
+ if (typedOperation.responses) {
1657
+ for (const [statusCode, response] of Object.entries(typedOperation.responses)) {
1658
+ if (response.content) {
1659
+ const contentType = Object.keys(response.content)[0];
1660
+ const mediaType = response.content[contentType];
1661
+ if (mediaType.example) {
1662
+ examples.push({ fileName: `response-${statusCode}.json`, content: JSON.stringify(mediaType.example, null, 2) });
1663
+ }
1664
+ if (mediaType.examples) {
1665
+ for (const [name, exampleObj] of Object.entries(mediaType.examples)) {
1666
+ if (exampleObj.value) {
1667
+ examples.push({
1668
+ fileName: `response-${statusCode}-${name}.json`,
1669
+ content: JSON.stringify(exampleObj.value, null, 2)
1670
+ });
1671
+ }
1672
+ }
1673
+ }
1674
+ }
1675
+ }
1676
+ }
1677
+ return examples;
1678
+ }
1679
+ }
1680
+ return examples;
1681
+ }
1635
1682
  function getDeprecatedValues(openAPIOperation) {
1636
1683
  const deprecatedDate = openAPIOperation["x-eventcatalog-deprecated-date"] || null;
1637
1684
  const deprecatedMessage = openAPIOperation["x-eventcatalog-deprecated-message"] || null;
@@ -1889,7 +1936,10 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1889
1936
  getQuery,
1890
1937
  rmQueryById,
1891
1938
  writeQuery,
1892
- addFileToQuery
1939
+ addFileToQuery,
1940
+ addExampleToEvent,
1941
+ addExampleToCommand,
1942
+ addExampleToQuery
1893
1943
  } = (0, import_sdk.default)(projectDirectory);
1894
1944
  const messageTypeMap = {
1895
1945
  event: {
@@ -1898,6 +1948,7 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1898
1948
  rmMessageById: rmEventById,
1899
1949
  writeMessage: writeEvent,
1900
1950
  addFileToMessage: addFileToEvent,
1951
+ addExampleToMessage: addExampleToEvent,
1901
1952
  collection: "events"
1902
1953
  },
1903
1954
  command: {
@@ -1906,6 +1957,7 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1906
1957
  rmMessageById: rmCommandById,
1907
1958
  writeMessage: writeCommand,
1908
1959
  addFileToMessage: addFileToCommand,
1960
+ addExampleToMessage: addExampleToCommand,
1909
1961
  collection: "commands"
1910
1962
  },
1911
1963
  query: {
@@ -1914,6 +1966,7 @@ var getMessageTypeUtils = (projectDirectory, messageType) => {
1914
1966
  rmMessageById: rmQueryById,
1915
1967
  writeMessage: writeQuery,
1916
1968
  addFileToMessage: addFileToQuery,
1969
+ addExampleToMessage: addExampleToQuery,
1917
1970
  collection: "queries"
1918
1971
  }
1919
1972
  };
@@ -4152,7 +4205,7 @@ var import_node_path = require("path");
4152
4205
  // package.json
4153
4206
  var package_default = {
4154
4207
  name: "@eventcatalog/generator-openapi",
4155
- version: "7.8.0",
4208
+ version: "7.9.0",
4156
4209
  description: "OpenAPI generator for EventCatalog",
4157
4210
  scripts: {
4158
4211
  build: "tsup",
@@ -4189,7 +4242,7 @@ var package_default = {
4189
4242
  dependencies: {
4190
4243
  "@apidevtools/swagger-parser": "^10.1.0",
4191
4244
  "@changesets/cli": "^2.27.7",
4192
- "@eventcatalog/sdk": "2.12.1",
4245
+ "@eventcatalog/sdk": "2.17.4",
4193
4246
  chalk: "4.1.2",
4194
4247
  "js-yaml": "^4.1.0",
4195
4248
  "openapi-types": "^12.1.3",
@@ -4586,6 +4639,7 @@ var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, o
4586
4639
  const sidebarBadgeType = options.sidebarBadgeType || "HTTP_METHOD";
4587
4640
  const version = options.serviceVersion || document2.info.version;
4588
4641
  const preserveExistingMessages = options.preserveExistingMessages ?? true;
4642
+ const parseExamples = options.parseExamples ?? true;
4589
4643
  const isDraft = options.isDraft ?? null;
4590
4644
  let receives = [], sends = [];
4591
4645
  for (const operation of operations) {
@@ -4606,6 +4660,7 @@ var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, o
4606
4660
  console.log(import_chalk4.default.blue(`Processing message: ${message2.name} (v${message2.version})`));
4607
4661
  const {
4608
4662
  addFileToMessage,
4663
+ addExampleToMessage,
4609
4664
  writeMessage,
4610
4665
  getMessage,
4611
4666
  versionMessage,
@@ -4692,6 +4747,15 @@ var processMessagesForOpenAPISpec = async (pathToSpec, document2, servicePath, o
4692
4747
  );
4693
4748
  }
4694
4749
  }
4750
+ if (parseExamples && operation.operationId) {
4751
+ const operationExamples = await getExamplesByOperationId(pathToSpec, operation.operationId, document2);
4752
+ for (const example of operationExamples) {
4753
+ await addExampleToMessage(message2.id, { content: example.content, fileName: example.fileName }, message2.version);
4754
+ }
4755
+ if (operationExamples.length > 0) {
4756
+ console.log(import_chalk4.default.cyan(` - ${operationExamples.length} example(s) added to message (v${message2.version})`));
4757
+ }
4758
+ }
4695
4759
  console.log(import_chalk4.default.cyan(` - Message (v${message2.version}) created`));
4696
4760
  if (!operation.operationId) {
4697
4761
  console.log(import_chalk4.default.yellow(` - OperationId not found for ${operation.method} ${operation.path}, creating one...`));