@eventcatalog/sdk 1.2.2 → 1.3.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
@@ -41,9 +41,9 @@ var findFileById = async (catalogDir, id, version) => {
41
41
  return match[0].path;
42
42
  }
43
43
  };
44
- var getFiles = async (pattern) => {
44
+ var getFiles = async (pattern, ignore = "") => {
45
45
  try {
46
- const files = await glob(pattern, { ignore: "node_modules/**" });
46
+ const files = await glob(pattern, { ignore: ["node_modules/**", ignore] });
47
47
  return files;
48
48
  } catch (error) {
49
49
  throw new Error(`Error finding files: ${error}`);
@@ -95,6 +95,7 @@ var uniqueVersions = (messages) => {
95
95
  import { dirname, join as join2 } from "path";
96
96
  import matter2 from "gray-matter";
97
97
  import fs2 from "node:fs/promises";
98
+ import { satisfies as satisfies2 } from "semver";
98
99
  var versionResource = async (catalogDir, id) => {
99
100
  const files = await getFiles(`${catalogDir}/**/index.md`);
100
101
  const matchedFiles = await searchFilesForId(files, id);
@@ -119,13 +120,28 @@ var versionResource = async (catalogDir, id) => {
119
120
  );
120
121
  });
121
122
  };
122
- var writeResource = async (catalogDir, resource, options = { path: "", type: "", override: false }) => {
123
+ var writeResource = async (catalogDir, resource, options = {
124
+ path: "",
125
+ type: "",
126
+ override: false,
127
+ versionExistingContent: false
128
+ }) => {
123
129
  const path = options.path || `/${resource.id}`;
124
130
  const exists = await versionExists(catalogDir, resource.id, resource.version);
125
131
  if (exists && !options.override) {
126
132
  throw new Error(`Failed to write ${resource.id} (${options.type}) as the version ${resource.version} already exists`);
127
133
  }
128
134
  const { markdown, ...frontmatter } = resource;
135
+ if (options.versionExistingContent) {
136
+ const currentResource = await getResource(catalogDir, resource.id);
137
+ if (currentResource) {
138
+ if (satisfies2(resource.version, `>${currentResource.version}`)) {
139
+ await versionResource(catalogDir, resource.id);
140
+ } else {
141
+ throw new Error(`New version ${resource.version} is not greater than current version ${currentResource.version}`);
142
+ }
143
+ }
144
+ }
129
145
  const document = matter2.stringify(markdown.trim(), frontmatter);
130
146
  await fs2.mkdir(join2(catalogDir, path), { recursive: true });
131
147
  await fs2.writeFile(join2(catalogDir, path, "index.md"), document);
@@ -139,6 +155,18 @@ var getResource = async (catalogDir, id, version, options) => {
139
155
  markdown: content.trim()
140
156
  };
141
157
  };
158
+ var getResources = async (catalogDir, { type, latestOnly = false }) => {
159
+ const ignore = latestOnly ? `**/versioned/**` : "";
160
+ const files = await getFiles(`${catalogDir}/**/${type}/**/index.md`, ignore);
161
+ if (files.length === 0) return;
162
+ return files.map((file) => {
163
+ const { data, content } = matter2.read(file);
164
+ return {
165
+ ...data,
166
+ markdown: content.trim()
167
+ };
168
+ });
169
+ };
142
170
  var rmResourceById = async (catalogDir, id, version, options) => {
143
171
  const files = await getFiles(`${catalogDir}/**/index.md`);
144
172
  const matchedFiles = await searchFilesForId(files, id, version);
@@ -165,6 +193,7 @@ var getVersionedDirectory = (sourceDirectory, version) => {
165
193
 
166
194
  // src/events.ts
167
195
  var getEvent = (directory) => async (id, version) => getResource(directory, id, version, { type: "event" });
196
+ var getEvents = (directory) => async (options) => getResources(directory, { type: "events", ...options });
168
197
  var writeEvent = (directory) => async (event, options = { path: "", override: false }) => writeResource(directory, { ...event }, { ...options, type: "event" });
169
198
  var writeEventToService = (directory) => async (event, service, options = { path: "" }) => {
170
199
  let pathForEvent = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/events` : `/${service.id}/events`;
@@ -191,6 +220,7 @@ var eventHasVersion = (directory) => async (id, version) => {
191
220
  import fs4 from "node:fs/promises";
192
221
  import { join as join4 } from "node:path";
193
222
  var getCommand = (directory) => async (id, version) => getResource(directory, id, version, { type: "command" });
223
+ var getCommands = (directory) => async (options) => getResources(directory, { type: "commands", ...options });
194
224
  var writeCommand = (directory) => async (command, options = { path: "" }) => writeResource(directory, { ...command }, { ...options, type: "command" });
195
225
  var writeCommandToService = (directory) => async (command, service, options = { path: "" }) => {
196
226
  let pathForEvent = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/commands` : `/${service.id}/commands`;
@@ -216,6 +246,7 @@ import fs5 from "node:fs/promises";
216
246
  import { join as join5 } from "node:path";
217
247
  var getQuery = (directory) => async (id, version) => getResource(directory, id, version, { type: "query" });
218
248
  var writeQuery = (directory) => async (query, options = { path: "" }) => writeResource(directory, { ...query }, { ...options, type: "query" });
249
+ var getQueries = (directory) => async (options) => getResources(directory, { type: "queries", ...options });
219
250
  var writeQueryToService = (directory) => async (query, service, options = { path: "" }) => {
220
251
  let pathForQuery = service.version && service.version !== "latest" ? `/${service.id}/versioned/${service.version}/queries` : `/${service.id}/queries`;
221
252
  pathForQuery = join5(pathForQuery, query.id);
@@ -241,6 +272,7 @@ var queryHasVersion = (directory) => async (id, version) => {
241
272
  import fs6 from "node:fs/promises";
242
273
  import { join as join6, dirname as dirname2 } from "node:path";
243
274
  var getService = (directory) => async (id, version) => getResource(directory, id, version, { type: "service" });
275
+ var getServices = (directory) => async (options) => getResources(directory, { type: "services", ...options });
244
276
  var writeService = (directory) => async (service, options = { path: "" }) => {
245
277
  const resource = { ...service };
246
278
  if (Array.isArray(service.sends)) {
@@ -324,6 +356,7 @@ var serviceHasVersion = (directory) => async (id, version) => {
324
356
  import fs7 from "node:fs/promises";
325
357
  import { join as join7 } from "node:path";
326
358
  var getDomain = (directory) => async (id, version) => getResource(directory, id, version, { type: "domain" });
359
+ var getDomains = (directory) => async (options) => getResources(directory, { type: "domains", ...options });
327
360
  var writeDomain = (directory) => async (domain, options = { path: "" }) => {
328
361
  const resource = { ...domain };
329
362
  if (Array.isArray(domain.services)) {
@@ -359,6 +392,7 @@ var addServiceToDomain = (directory) => async (id, service, version) => {
359
392
  import fs8 from "node:fs/promises";
360
393
  import { join as join8 } from "node:path";
361
394
  var getChannel = (directory) => async (id, version) => getResource(directory, id, version, { type: "channel" });
395
+ var getChannels = (directory) => async (options) => getResources(directory, { type: "channels", ...options });
362
396
  var writeChannel = (directory) => async (channel, options = { path: "" }) => writeResource(directory, { ...channel }, { ...options, type: "channel" });
363
397
  var rmChannel = (directory) => async (path) => {
364
398
  await fs8.rm(join8(directory, path), { recursive: true });
@@ -410,6 +444,12 @@ var src_default = (path) => {
410
444
  * @returns Event|Undefined
411
445
  */
412
446
  getEvent: getEvent(join9(path)),
447
+ /**
448
+ * Returns all events from EventCatalog
449
+ * @param latestOnly - optional boolean, set to true to get only latest versions
450
+ * @returns Event[]|Undefined
451
+ */
452
+ getEvents: getEvents(join9(path)),
413
453
  /**
414
454
  * Adds an event to EventCatalog
415
455
  *
@@ -481,6 +521,12 @@ var src_default = (path) => {
481
521
  * @returns Command|Undefined
482
522
  */
483
523
  getCommand: getCommand(join9(path)),
524
+ /**
525
+ * Returns all commands from EventCatalog
526
+ * @param latestOnly - optional boolean, set to true to get only latest versions
527
+ * @returns Command[]|Undefined
528
+ */
529
+ getCommands: getCommands(join9(path)),
484
530
  /**
485
531
  * Adds an command to EventCatalog
486
532
  *
@@ -552,6 +598,12 @@ var src_default = (path) => {
552
598
  * @returns Query|Undefined
553
599
  */
554
600
  getQuery: getQuery(join9(path)),
601
+ /**
602
+ * Returns all queries from EventCatalog
603
+ * @param latestOnly - optional boolean, set to true to get only latest versions
604
+ * @returns Query[]|Undefined
605
+ */
606
+ getQueries: getQueries(join9(path)),
555
607
  /**
556
608
  * Adds a query to EventCatalog
557
609
  *
@@ -623,6 +675,12 @@ var src_default = (path) => {
623
675
  * @returns Channel|Undefined
624
676
  */
625
677
  getChannel: getChannel(join9(path)),
678
+ /**
679
+ * Returns all channels from EventCatalog
680
+ * @param latestOnly - optional boolean, set to true to get only latest versions
681
+ * @returns Channel[]|Undefined
682
+ */
683
+ getChannels: getChannels(join9(path)),
626
684
  /**
627
685
  * Adds an channel to EventCatalog
628
686
  *
@@ -744,6 +802,12 @@ var src_default = (path) => {
744
802
  * @returns Service|Undefined
745
803
  */
746
804
  getService: getService(join9(path)),
805
+ /**
806
+ * Returns all services from EventCatalog
807
+ * @param latestOnly - optional boolean, set to true to get only latest versions
808
+ * @returns Service[]|Undefined
809
+ */
810
+ getServices: getServices(join9(path)),
747
811
  /**
748
812
  * Moves a given service id to the version directory
749
813
  * @param directory
@@ -865,6 +929,12 @@ var src_default = (path) => {
865
929
  * @returns Domain|Undefined
866
930
  */
867
931
  getDomain: getDomain(join9(path, "domains")),
932
+ /**
933
+ * Returns all domains from EventCatalog
934
+ * @param latestOnly - optional boolean, set to true to get only latest versions
935
+ * @returns Domain[]|Undefined
936
+ */
937
+ getDomains: getDomains(join9(path)),
868
938
  /**
869
939
  * Moves a given domain id to the version directory
870
940
  * @param directory