@eventcatalog/sdk 2.17.3 → 2.18.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.mjs CHANGED
@@ -828,6 +828,11 @@ var copyDir = async (catalogDir, source, target, filter) => {
828
828
  });
829
829
  fsSync.rmSync(tmpDirectory, { recursive: true });
830
830
  };
831
+ var buildMessagePointer = (message) => {
832
+ const pointer = { id: message.id, version: message.version };
833
+ if (message.fields) pointer.fields = message.fields;
834
+ return pointer;
835
+ };
831
836
  var uniqueVersions = (messages) => {
832
837
  const uniqueSet = /* @__PURE__ */ new Set();
833
838
  return messages.filter((message) => {
@@ -1062,6 +1067,55 @@ var getFileFromResource = async (catalogDir, id, file, version) => {
1062
1067
  var getVersionedDirectory = (sourceDirectory, version) => {
1063
1068
  return join2(sourceDirectory, "versioned", version);
1064
1069
  };
1070
+ var CONFIG_FILES = ["examples.config.yaml", "examples.config.yml", "examples.config.json"];
1071
+ function resolveExamplePath(examplesDir, fileName) {
1072
+ const resolved = path.resolve(examplesDir, fileName);
1073
+ if (!resolved.startsWith(path.resolve(examplesDir) + path.sep) && resolved !== path.resolve(examplesDir)) {
1074
+ throw new Error(`Invalid example fileName: path traversal detected in "${fileName}"`);
1075
+ }
1076
+ return resolved;
1077
+ }
1078
+ var addExampleToResource = async (catalogDir, id, example, version) => {
1079
+ const pathToResource = await findFileById(catalogDir, id, version);
1080
+ if (!pathToResource) throw new Error("Cannot find directory to write example to");
1081
+ const examplesDir = join2(dirname2(pathToResource), "examples");
1082
+ const targetPath = resolveExamplePath(examplesDir, example.fileName);
1083
+ fsSync2.mkdirSync(path.dirname(targetPath), { recursive: true });
1084
+ fsSync2.writeFileSync(targetPath, example.content);
1085
+ };
1086
+ var getExamplesFromResource = async (catalogDir, id, version) => {
1087
+ const pathToResource = await findFileById(catalogDir, id, version);
1088
+ if (!pathToResource) throw new Error("Cannot find resource");
1089
+ const examplesDir = join2(dirname2(pathToResource), "examples");
1090
+ if (!fsSync2.existsSync(examplesDir)) return [];
1091
+ const collectFiles = (dir, baseDir) => {
1092
+ const results = [];
1093
+ const entries = fsSync2.readdirSync(dir, { withFileTypes: true });
1094
+ for (const entry of entries) {
1095
+ const fullPath = join2(dir, entry.name);
1096
+ if (entry.isDirectory()) {
1097
+ results.push(...collectFiles(fullPath, baseDir));
1098
+ } else if (entry.isFile() && !CONFIG_FILES.includes(entry.name)) {
1099
+ results.push({
1100
+ fileName: path.relative(baseDir, fullPath),
1101
+ content: fsSync2.readFileSync(fullPath, "utf-8")
1102
+ });
1103
+ }
1104
+ }
1105
+ return results;
1106
+ };
1107
+ return collectFiles(examplesDir, examplesDir).sort((a, b) => a.fileName.localeCompare(b.fileName));
1108
+ };
1109
+ var removeExampleFromResource = async (catalogDir, id, fileName, version) => {
1110
+ const pathToResource = await findFileById(catalogDir, id, version);
1111
+ if (!pathToResource) throw new Error("Cannot find resource");
1112
+ const examplesDir = join2(dirname2(pathToResource), "examples");
1113
+ const examplePath = resolveExamplePath(examplesDir, fileName);
1114
+ if (!fsSync2.existsSync(examplePath)) {
1115
+ throw new Error(`Example file ${fileName} does not exist in resource ${id}${version ? ` v(${version})` : ""}`);
1116
+ }
1117
+ fsSync2.unlinkSync(examplePath);
1118
+ };
1065
1119
  var isLatestVersion = async (catalogDir, id, version) => {
1066
1120
  const resource = await getResource(catalogDir, id, version);
1067
1121
  if (!resource) return false;
@@ -1102,6 +1156,9 @@ var eventHasVersion = (directory) => async (id, version) => {
1102
1156
  const file = await findFileById(directory, id, version);
1103
1157
  return !!file;
1104
1158
  };
1159
+ var addExampleToEvent = (directory) => async (id, example, version) => addExampleToResource(directory, id, example, version);
1160
+ var getExamplesFromEvent = (directory) => async (id, version) => getExamplesFromResource(directory, id, version);
1161
+ var removeExampleFromEvent = (directory) => async (id, fileName, version) => removeExampleFromResource(directory, id, fileName, version);
1105
1162
 
1106
1163
  // src/commands.ts
1107
1164
  import fs3 from "fs/promises";
@@ -1137,6 +1194,9 @@ var commandHasVersion = (directory) => async (id, version) => {
1137
1194
  const file = await findFileById(directory, id, version);
1138
1195
  return !!file;
1139
1196
  };
1197
+ var addExampleToCommand = (directory) => async (id, example, version) => addExampleToResource(directory, id, example, version);
1198
+ var getExamplesFromCommand = (directory) => async (id, version) => getExamplesFromResource(directory, id, version);
1199
+ var removeExampleFromCommand = (directory) => async (id, fileName, version) => removeExampleFromResource(directory, id, fileName, version);
1140
1200
 
1141
1201
  // src/queries.ts
1142
1202
  import fs4 from "fs/promises";
@@ -1174,6 +1234,9 @@ var queryHasVersion = (directory) => async (id, version) => {
1174
1234
  const file = await findFileById(directory, id, version);
1175
1235
  return !!file;
1176
1236
  };
1237
+ var addExampleToQuery = (directory) => async (id, example, version) => addExampleToResource(directory, id, example, version);
1238
+ var getExamplesFromQuery = (directory) => async (id, version) => getExamplesFromResource(directory, id, version);
1239
+ var removeExampleFromQuery = (directory) => async (id, fileName, version) => removeExampleFromResource(directory, id, fileName, version);
1177
1240
 
1178
1241
  // src/services.ts
1179
1242
  import fs5 from "fs/promises";
@@ -1272,7 +1335,7 @@ var addMessageToService = (directory) => async (id, direction, event, version) =
1272
1335
  return;
1273
1336
  }
1274
1337
  }
1275
- service.sends.push({ id: event.id, version: event.version });
1338
+ service.sends.push(buildMessagePointer(event));
1276
1339
  } else if (direction === "receives") {
1277
1340
  if (service.receives === void 0) {
1278
1341
  service.receives = [];
@@ -1282,7 +1345,7 @@ var addMessageToService = (directory) => async (id, direction, event, version) =
1282
1345
  return;
1283
1346
  }
1284
1347
  }
1285
- service.receives.push({ id: event.id, version: event.version });
1348
+ service.receives.push(buildMessagePointer(event));
1286
1349
  } else {
1287
1350
  throw new Error(`Direction ${direction} is invalid, only 'receives' and 'sends' are supported`);
1288
1351
  }
@@ -1502,7 +1565,7 @@ var addMessageToDomain = (directory) => async (id, direction, message, version)
1502
1565
  return;
1503
1566
  }
1504
1567
  }
1505
- domain.sends.push({ id: message.id, version: message.version });
1568
+ domain.sends.push(buildMessagePointer(message));
1506
1569
  } else if (direction === "receives") {
1507
1570
  if (domain.receives === void 0) {
1508
1571
  domain.receives = [];
@@ -1512,7 +1575,7 @@ var addMessageToDomain = (directory) => async (id, direction, message, version)
1512
1575
  return;
1513
1576
  }
1514
1577
  }
1515
- domain.receives.push({ id: message.id, version: message.version });
1578
+ domain.receives.push(buildMessagePointer(message));
1516
1579
  } else {
1517
1580
  throw new Error(`Direction ${direction} is invalid, only 'receives' and 'sends' are supported`);
1518
1581
  }
@@ -2558,6 +2621,9 @@ var src_default = (path8) => {
2558
2621
  * @returns
2559
2622
  */
2560
2623
  eventHasVersion: eventHasVersion(join18(path8)),
2624
+ addExampleToEvent: addExampleToEvent(join18(path8)),
2625
+ getExamplesFromEvent: getExamplesFromEvent(join18(path8)),
2626
+ removeExampleFromEvent: removeExampleFromEvent(join18(path8)),
2561
2627
  /**
2562
2628
  * ================================
2563
2629
  * Commands
@@ -2635,6 +2701,9 @@ var src_default = (path8) => {
2635
2701
  * @returns
2636
2702
  */
2637
2703
  commandHasVersion: commandHasVersion(join18(path8)),
2704
+ addExampleToCommand: addExampleToCommand(join18(path8)),
2705
+ getExamplesFromCommand: getExamplesFromCommand(join18(path8)),
2706
+ removeExampleFromCommand: removeExampleFromCommand(join18(path8)),
2638
2707
  /**
2639
2708
  * ================================
2640
2709
  * Queries
@@ -2712,6 +2781,9 @@ var src_default = (path8) => {
2712
2781
  * @returns
2713
2782
  */
2714
2783
  queryHasVersion: queryHasVersion(join18(path8)),
2784
+ addExampleToQuery: addExampleToQuery(join18(path8)),
2785
+ getExamplesFromQuery: getExamplesFromQuery(join18(path8)),
2786
+ removeExampleFromQuery: removeExampleFromQuery(join18(path8)),
2715
2787
  /**
2716
2788
  * ================================
2717
2789
  * Channels