@eventcatalog/sdk 1.2.3 → 1.3.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.js CHANGED
@@ -75,9 +75,9 @@ var findFileById = async (catalogDir, id, version) => {
75
75
  return match[0].path;
76
76
  }
77
77
  };
78
- var getFiles = async (pattern) => {
78
+ var getFiles = async (pattern, ignore = "") => {
79
79
  try {
80
- const files = await (0, import_glob.glob)(pattern, { ignore: "node_modules/**" });
80
+ const files = await (0, import_glob.glob)(pattern, { ignore: ["node_modules/**", ignore] });
81
81
  return files;
82
82
  } catch (error) {
83
83
  throw new Error(`Error finding files: ${error}`);
@@ -189,6 +189,18 @@ var getResource = async (catalogDir, id, version, options) => {
189
189
  markdown: content.trim()
190
190
  };
191
191
  };
192
+ var getResources = async (catalogDir, { type, latestOnly = false }) => {
193
+ const ignore = latestOnly ? `**/versioned/**` : "";
194
+ const files = await getFiles(`${catalogDir}/**/${type}/**/index.md`, ignore);
195
+ if (files.length === 0) return;
196
+ return files.map((file) => {
197
+ const { data, content } = import_gray_matter2.default.read(file);
198
+ return {
199
+ ...data,
200
+ markdown: content.trim()
201
+ };
202
+ });
203
+ };
192
204
  var rmResourceById = async (catalogDir, id, version, options) => {
193
205
  const files = await getFiles(`${catalogDir}/**/index.md`);
194
206
  const matchedFiles = await searchFilesForId(files, id, version);
@@ -215,6 +227,7 @@ var getVersionedDirectory = (sourceDirectory, version) => {
215
227
 
216
228
  // src/events.ts
217
229
  var getEvent = (directory) => async (id, version) => getResource(directory, id, version, { type: "event" });
230
+ var getEvents = (directory) => async (options) => getResources(directory, { type: "events", ...options });
218
231
  var writeEvent = (directory) => async (event, options = { path: "", override: false }) => writeResource(directory, { ...event }, { ...options, type: "event" });
219
232
  var writeEventToService = (directory) => async (event, service, options = { path: "" }) => {
220
233
  let pathForEvent = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/events` : `/${service.id}/events`;
@@ -241,6 +254,7 @@ var eventHasVersion = (directory) => async (id, version) => {
241
254
  var import_promises4 = __toESM(require("fs/promises"));
242
255
  var import_node_path3 = require("path");
243
256
  var getCommand = (directory) => async (id, version) => getResource(directory, id, version, { type: "command" });
257
+ var getCommands = (directory) => async (options) => getResources(directory, { type: "commands", ...options });
244
258
  var writeCommand = (directory) => async (command, options = { path: "" }) => writeResource(directory, { ...command }, { ...options, type: "command" });
245
259
  var writeCommandToService = (directory) => async (command, service, options = { path: "" }) => {
246
260
  let pathForEvent = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/commands` : `/${service.id}/commands`;
@@ -266,6 +280,7 @@ var import_promises5 = __toESM(require("fs/promises"));
266
280
  var import_node_path4 = require("path");
267
281
  var getQuery = (directory) => async (id, version) => getResource(directory, id, version, { type: "query" });
268
282
  var writeQuery = (directory) => async (query, options = { path: "" }) => writeResource(directory, { ...query }, { ...options, type: "query" });
283
+ var getQueries = (directory) => async (options) => getResources(directory, { type: "queries", ...options });
269
284
  var writeQueryToService = (directory) => async (query, service, options = { path: "" }) => {
270
285
  let pathForQuery = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/queries` : `/${service.id}/queries`;
271
286
  pathForQuery = (0, import_node_path4.join)(pathForQuery, query.id);
@@ -291,6 +306,7 @@ var queryHasVersion = (directory) => async (id, version) => {
291
306
  var import_promises6 = __toESM(require("fs/promises"));
292
307
  var import_node_path5 = require("path");
293
308
  var getService = (directory) => async (id, version) => getResource(directory, id, version, { type: "service" });
309
+ var getServices = (directory) => async (options) => getResources(directory, { type: "services", ...options });
294
310
  var writeService = (directory) => async (service, options = { path: "" }) => {
295
311
  const resource = { ...service };
296
312
  if (Array.isArray(service.sends)) {
@@ -362,8 +378,14 @@ var addMessageToService = (directory) => async (id, direction, event, version) =
362
378
  } else {
363
379
  throw new Error(`Direction ${direction} is invalid, only 'receives' and 'sends' are supported`);
364
380
  }
381
+ const existingResource = await findFileById(directory, id, version);
382
+ if (!existingResource) {
383
+ throw new Error(`Cannot find service ${id} in the catalog`);
384
+ }
385
+ const path = existingResource.split("/services")[0];
386
+ const pathToResource = (0, import_node_path5.join)(path, "services");
365
387
  await rmServiceById(directory)(id, version);
366
- await writeService(directory)(service);
388
+ await writeService(pathToResource)(service);
367
389
  };
368
390
  var serviceHasVersion = (directory) => async (id, version) => {
369
391
  const file = await findFileById(directory, id, version);
@@ -374,6 +396,7 @@ var serviceHasVersion = (directory) => async (id, version) => {
374
396
  var import_promises7 = __toESM(require("fs/promises"));
375
397
  var import_node_path6 = require("path");
376
398
  var getDomain = (directory) => async (id, version) => getResource(directory, id, version, { type: "domain" });
399
+ var getDomains = (directory) => async (options) => getResources(directory, { type: "domains", ...options });
377
400
  var writeDomain = (directory) => async (domain, options = { path: "" }) => {
378
401
  const resource = { ...domain };
379
402
  if (Array.isArray(domain.services)) {
@@ -409,6 +432,7 @@ var addServiceToDomain = (directory) => async (id, service, version) => {
409
432
  var import_promises8 = __toESM(require("fs/promises"));
410
433
  var import_node_path7 = require("path");
411
434
  var getChannel = (directory) => async (id, version) => getResource(directory, id, version, { type: "channel" });
435
+ var getChannels = (directory) => async (options) => getResources(directory, { type: "channels", ...options });
412
436
  var writeChannel = (directory) => async (channel, options = { path: "" }) => writeResource(directory, { ...channel }, { ...options, type: "channel" });
413
437
  var rmChannel = (directory) => async (path) => {
414
438
  await import_promises8.default.rm((0, import_node_path7.join)(directory, path), { recursive: true });
@@ -446,8 +470,14 @@ var addMessageToChannel = (directory, collection) => async (id, _message, versio
446
470
  }
447
471
  const channelInfo = { id, version: channel.version, ..._message.parameters && { parameters: _message.parameters } };
448
472
  message.channels.push(channelInfo);
473
+ const existingResource = await findFileById(directory, _message.id, _message.version);
474
+ if (!existingResource) {
475
+ throw new Error(`Cannot find message ${id} in the catalog`);
476
+ }
477
+ const path = existingResource.split(`/${collection}`)[0];
478
+ const pathToResource = (0, import_node_path7.join)(path, collection);
449
479
  await rmMessageById(directory)(_message.id, _message.version);
450
- await writeMessage(directory)(message);
480
+ await writeMessage(pathToResource)(message);
451
481
  };
452
482
 
453
483
  // src/index.ts
@@ -460,6 +490,12 @@ var src_default = (path) => {
460
490
  * @returns Event|Undefined
461
491
  */
462
492
  getEvent: getEvent((0, import_node_path8.join)(path)),
493
+ /**
494
+ * Returns all events from EventCatalog
495
+ * @param latestOnly - optional boolean, set to true to get only latest versions
496
+ * @returns Event[]|Undefined
497
+ */
498
+ getEvents: getEvents((0, import_node_path8.join)(path)),
463
499
  /**
464
500
  * Adds an event to EventCatalog
465
501
  *
@@ -531,6 +567,12 @@ var src_default = (path) => {
531
567
  * @returns Command|Undefined
532
568
  */
533
569
  getCommand: getCommand((0, import_node_path8.join)(path)),
570
+ /**
571
+ * Returns all commands from EventCatalog
572
+ * @param latestOnly - optional boolean, set to true to get only latest versions
573
+ * @returns Command[]|Undefined
574
+ */
575
+ getCommands: getCommands((0, import_node_path8.join)(path)),
534
576
  /**
535
577
  * Adds an command to EventCatalog
536
578
  *
@@ -602,6 +644,12 @@ var src_default = (path) => {
602
644
  * @returns Query|Undefined
603
645
  */
604
646
  getQuery: getQuery((0, import_node_path8.join)(path)),
647
+ /**
648
+ * Returns all queries from EventCatalog
649
+ * @param latestOnly - optional boolean, set to true to get only latest versions
650
+ * @returns Query[]|Undefined
651
+ */
652
+ getQueries: getQueries((0, import_node_path8.join)(path)),
605
653
  /**
606
654
  * Adds a query to EventCatalog
607
655
  *
@@ -673,6 +721,12 @@ var src_default = (path) => {
673
721
  * @returns Channel|Undefined
674
722
  */
675
723
  getChannel: getChannel((0, import_node_path8.join)(path)),
724
+ /**
725
+ * Returns all channels from EventCatalog
726
+ * @param latestOnly - optional boolean, set to true to get only latest versions
727
+ * @returns Channel[]|Undefined
728
+ */
729
+ getChannels: getChannels((0, import_node_path8.join)(path)),
676
730
  /**
677
731
  * Adds an channel to EventCatalog
678
732
  *
@@ -794,6 +848,12 @@ var src_default = (path) => {
794
848
  * @returns Service|Undefined
795
849
  */
796
850
  getService: getService((0, import_node_path8.join)(path)),
851
+ /**
852
+ * Returns all services from EventCatalog
853
+ * @param latestOnly - optional boolean, set to true to get only latest versions
854
+ * @returns Service[]|Undefined
855
+ */
856
+ getServices: getServices((0, import_node_path8.join)(path)),
797
857
  /**
798
858
  * Moves a given service id to the version directory
799
859
  * @param directory
@@ -915,6 +975,12 @@ var src_default = (path) => {
915
975
  * @returns Domain|Undefined
916
976
  */
917
977
  getDomain: getDomain((0, import_node_path8.join)(path, "domains")),
978
+ /**
979
+ * Returns all domains from EventCatalog
980
+ * @param latestOnly - optional boolean, set to true to get only latest versions
981
+ * @returns Domain[]|Undefined
982
+ */
983
+ getDomains: getDomains((0, import_node_path8.join)(path)),
918
984
  /**
919
985
  * Moves a given domain id to the version directory
920
986
  * @param directory