@eventcatalog/sdk 2.17.2 → 2.17.4

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
@@ -1062,6 +1062,55 @@ var getFileFromResource = async (catalogDir, id, file, version) => {
1062
1062
  var getVersionedDirectory = (sourceDirectory, version) => {
1063
1063
  return join2(sourceDirectory, "versioned", version);
1064
1064
  };
1065
+ var CONFIG_FILES = ["examples.config.yaml", "examples.config.yml", "examples.config.json"];
1066
+ function resolveExamplePath(examplesDir, fileName) {
1067
+ const resolved = path.resolve(examplesDir, fileName);
1068
+ if (!resolved.startsWith(path.resolve(examplesDir) + path.sep) && resolved !== path.resolve(examplesDir)) {
1069
+ throw new Error(`Invalid example fileName: path traversal detected in "${fileName}"`);
1070
+ }
1071
+ return resolved;
1072
+ }
1073
+ var addExampleToResource = async (catalogDir, id, example, version) => {
1074
+ const pathToResource = await findFileById(catalogDir, id, version);
1075
+ if (!pathToResource) throw new Error("Cannot find directory to write example to");
1076
+ const examplesDir = join2(dirname2(pathToResource), "examples");
1077
+ const targetPath = resolveExamplePath(examplesDir, example.fileName);
1078
+ fsSync2.mkdirSync(path.dirname(targetPath), { recursive: true });
1079
+ fsSync2.writeFileSync(targetPath, example.content);
1080
+ };
1081
+ var getExamplesFromResource = async (catalogDir, id, version) => {
1082
+ const pathToResource = await findFileById(catalogDir, id, version);
1083
+ if (!pathToResource) throw new Error("Cannot find resource");
1084
+ const examplesDir = join2(dirname2(pathToResource), "examples");
1085
+ if (!fsSync2.existsSync(examplesDir)) return [];
1086
+ const collectFiles = (dir, baseDir) => {
1087
+ const results = [];
1088
+ const entries = fsSync2.readdirSync(dir, { withFileTypes: true });
1089
+ for (const entry of entries) {
1090
+ const fullPath = join2(dir, entry.name);
1091
+ if (entry.isDirectory()) {
1092
+ results.push(...collectFiles(fullPath, baseDir));
1093
+ } else if (entry.isFile() && !CONFIG_FILES.includes(entry.name)) {
1094
+ results.push({
1095
+ fileName: path.relative(baseDir, fullPath),
1096
+ content: fsSync2.readFileSync(fullPath, "utf-8")
1097
+ });
1098
+ }
1099
+ }
1100
+ return results;
1101
+ };
1102
+ return collectFiles(examplesDir, examplesDir).sort((a, b) => a.fileName.localeCompare(b.fileName));
1103
+ };
1104
+ var removeExampleFromResource = async (catalogDir, id, fileName, version) => {
1105
+ const pathToResource = await findFileById(catalogDir, id, version);
1106
+ if (!pathToResource) throw new Error("Cannot find resource");
1107
+ const examplesDir = join2(dirname2(pathToResource), "examples");
1108
+ const examplePath = resolveExamplePath(examplesDir, fileName);
1109
+ if (!fsSync2.existsSync(examplePath)) {
1110
+ throw new Error(`Example file ${fileName} does not exist in resource ${id}${version ? ` v(${version})` : ""}`);
1111
+ }
1112
+ fsSync2.unlinkSync(examplePath);
1113
+ };
1065
1114
  var isLatestVersion = async (catalogDir, id, version) => {
1066
1115
  const resource = await getResource(catalogDir, id, version);
1067
1116
  if (!resource) return false;
@@ -1102,6 +1151,9 @@ var eventHasVersion = (directory) => async (id, version) => {
1102
1151
  const file = await findFileById(directory, id, version);
1103
1152
  return !!file;
1104
1153
  };
1154
+ var addExampleToEvent = (directory) => async (id, example, version) => addExampleToResource(directory, id, example, version);
1155
+ var getExamplesFromEvent = (directory) => async (id, version) => getExamplesFromResource(directory, id, version);
1156
+ var removeExampleFromEvent = (directory) => async (id, fileName, version) => removeExampleFromResource(directory, id, fileName, version);
1105
1157
 
1106
1158
  // src/commands.ts
1107
1159
  import fs3 from "fs/promises";
@@ -1137,6 +1189,9 @@ var commandHasVersion = (directory) => async (id, version) => {
1137
1189
  const file = await findFileById(directory, id, version);
1138
1190
  return !!file;
1139
1191
  };
1192
+ var addExampleToCommand = (directory) => async (id, example, version) => addExampleToResource(directory, id, example, version);
1193
+ var getExamplesFromCommand = (directory) => async (id, version) => getExamplesFromResource(directory, id, version);
1194
+ var removeExampleFromCommand = (directory) => async (id, fileName, version) => removeExampleFromResource(directory, id, fileName, version);
1140
1195
 
1141
1196
  // src/queries.ts
1142
1197
  import fs4 from "fs/promises";
@@ -1174,6 +1229,9 @@ var queryHasVersion = (directory) => async (id, version) => {
1174
1229
  const file = await findFileById(directory, id, version);
1175
1230
  return !!file;
1176
1231
  };
1232
+ var addExampleToQuery = (directory) => async (id, example, version) => addExampleToResource(directory, id, example, version);
1233
+ var getExamplesFromQuery = (directory) => async (id, version) => getExamplesFromResource(directory, id, version);
1234
+ var removeExampleFromQuery = (directory) => async (id, fileName, version) => removeExampleFromResource(directory, id, fileName, version);
1177
1235
 
1178
1236
  // src/services.ts
1179
1237
  import fs5 from "fs/promises";
@@ -1949,6 +2007,7 @@ var dumpCatalog = (directory) => async (options) => {
1949
2007
  // src/snapshots.ts
1950
2008
  import fs11 from "fs";
1951
2009
  import path6 from "path";
2010
+ import { createHash } from "crypto";
1952
2011
  import { execSync } from "child_process";
1953
2012
  import { compare } from "semver";
1954
2013
 
@@ -2139,6 +2198,8 @@ var pickCoreFields = (resource) => {
2139
2198
  if (resource.receives) picked.receives = resource.receives;
2140
2199
  if (resource.deprecated) picked.deprecated = resource.deprecated;
2141
2200
  if (resource.owners) picked.owners = resource.owners;
2201
+ if (resource.schemaPath) picked.schemaPath = resource.schemaPath;
2202
+ if (resource.schemaHash) picked.schemaHash = resource.schemaHash;
2142
2203
  return picked;
2143
2204
  };
2144
2205
  var deduplicateByLatestVersion = (resources) => {
@@ -2166,6 +2227,16 @@ var detectGitInfo = (catalogDir) => {
2166
2227
  return void 0;
2167
2228
  }
2168
2229
  };
2230
+ var enrichWithSchemaHashes = async (getSchemaForMessage2, resources) => {
2231
+ await Promise.all(
2232
+ resources.map(async (resource) => {
2233
+ if (!resource.schemaPath) return;
2234
+ const schema = await getSchemaForMessage2(resource.id, resource.version);
2235
+ if (!schema) return;
2236
+ resource.schemaHash = createHash("sha256").update(schema.schema).digest("hex");
2237
+ })
2238
+ );
2239
+ };
2169
2240
  var createSnapshot = (directory) => {
2170
2241
  return async (options) => {
2171
2242
  const { label, outputDir, git } = options || {};
@@ -2178,6 +2249,11 @@ var createSnapshot = (directory) => {
2178
2249
  sdk.getQueries(),
2179
2250
  sdk.getChannels()
2180
2251
  ]);
2252
+ await Promise.all([
2253
+ enrichWithSchemaHashes(sdk.getSchemaForMessage, events || []),
2254
+ enrichWithSchemaHashes(sdk.getSchemaForMessage, commands || []),
2255
+ enrichWithSchemaHashes(sdk.getSchemaForMessage, queries || [])
2256
+ ]);
2181
2257
  const snapshotDomains = stripToCore(domains);
2182
2258
  const snapshotServices = stripToCore(services);
2183
2259
  const snapshotEvents = stripToCore(events);
@@ -2540,6 +2616,9 @@ var src_default = (path8) => {
2540
2616
  * @returns
2541
2617
  */
2542
2618
  eventHasVersion: eventHasVersion(join18(path8)),
2619
+ addExampleToEvent: addExampleToEvent(join18(path8)),
2620
+ getExamplesFromEvent: getExamplesFromEvent(join18(path8)),
2621
+ removeExampleFromEvent: removeExampleFromEvent(join18(path8)),
2543
2622
  /**
2544
2623
  * ================================
2545
2624
  * Commands
@@ -2617,6 +2696,9 @@ var src_default = (path8) => {
2617
2696
  * @returns
2618
2697
  */
2619
2698
  commandHasVersion: commandHasVersion(join18(path8)),
2699
+ addExampleToCommand: addExampleToCommand(join18(path8)),
2700
+ getExamplesFromCommand: getExamplesFromCommand(join18(path8)),
2701
+ removeExampleFromCommand: removeExampleFromCommand(join18(path8)),
2620
2702
  /**
2621
2703
  * ================================
2622
2704
  * Queries
@@ -2694,6 +2776,9 @@ var src_default = (path8) => {
2694
2776
  * @returns
2695
2777
  */
2696
2778
  queryHasVersion: queryHasVersion(join18(path8)),
2779
+ addExampleToQuery: addExampleToQuery(join18(path8)),
2780
+ getExamplesFromQuery: getExamplesFromQuery(join18(path8)),
2781
+ removeExampleFromQuery: removeExampleFromQuery(join18(path8)),
2697
2782
  /**
2698
2783
  * ================================
2699
2784
  * Channels