@eventcatalog/sdk 0.0.2 → 0.0.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.
@@ -16051,7 +16051,7 @@ var VitestIndex = /* @__PURE__ */ Object.freeze({
16051
16051
  var expectTypeOf = dist.expectTypeOf;
16052
16052
 
16053
16053
  // src/index.ts
16054
- import { join as join4 } from "path";
16054
+ import { join as join5 } from "path";
16055
16055
 
16056
16056
  // src/events.ts
16057
16057
  import matter from "gray-matter";
@@ -16182,6 +16182,72 @@ var addSchemaToEvent = (directory) => async (id, schema, version) => {
16182
16182
  await addFileToEvent(directory)(id, { content: schema.schema, fileName: schema.fileName }, version);
16183
16183
  };
16184
16184
 
16185
+ // src/services.ts
16186
+ import matter2 from "gray-matter";
16187
+ import fs3 from "fs/promises";
16188
+ import { dirname as dirname3, join as join4 } from "path";
16189
+ var getService = (directory) => async (id, version) => {
16190
+ const file = await findFileById(directory, id, version);
16191
+ if (!file) throw new Error(`No service found for the given id: ${id}` + (version ? ` and version ${version}` : ""));
16192
+ const { data, content } = matter2.read(file);
16193
+ return {
16194
+ ...data,
16195
+ markdown: content.trim()
16196
+ };
16197
+ };
16198
+ var writeService = (directory) => async (service, options = { path: "" }) => {
16199
+ const path3 = options.path || `/${service.id}`;
16200
+ const exists = await versionExists(directory, service.id, service.version);
16201
+ if (exists) {
16202
+ throw new Error(`Failed to write service as the version ${service.version} already exists`);
16203
+ }
16204
+ const { markdown, ...frontmatter } = service;
16205
+ const document = matter2.stringify(markdown.trim(), frontmatter);
16206
+ await fs3.mkdir(join4(directory, path3), { recursive: true });
16207
+ await fs3.writeFile(join4(directory, path3, "index.md"), document);
16208
+ };
16209
+ var versionService = (directory) => async (id) => {
16210
+ const files = await getFiles(`${directory}/**/index.md`);
16211
+ const matchedFiles = await searchFilesForId(files, id);
16212
+ if (matchedFiles.length === 0) {
16213
+ throw new Error(`No service found with id: ${id}`);
16214
+ }
16215
+ const file = matchedFiles[0];
16216
+ const eventDirectory = dirname3(file);
16217
+ const { data: { version = "0.0.1" } = {} } = matter2.read(file);
16218
+ const targetDirectory = join4(eventDirectory, "versioned", version);
16219
+ await fs3.mkdir(targetDirectory, { recursive: true });
16220
+ await copyDir(directory, eventDirectory, targetDirectory, (src) => {
16221
+ return !src.includes("versioned");
16222
+ });
16223
+ await fs3.readdir(eventDirectory).then(async (resourceFiles) => {
16224
+ await Promise.all(
16225
+ resourceFiles.map(async (file2) => {
16226
+ if (file2 !== "versioned") {
16227
+ await fs3.rm(join4(eventDirectory, file2), { recursive: true });
16228
+ }
16229
+ })
16230
+ );
16231
+ });
16232
+ };
16233
+ var rmService = (directory) => async (path3) => {
16234
+ await fs3.rm(join4(directory, path3), { recursive: true });
16235
+ };
16236
+ var rmServiceById = (directory) => async (id, version) => {
16237
+ const files = await getFiles(`${directory}/**/index.md`);
16238
+ const matchedFiles = await searchFilesForId(files, id, version);
16239
+ if (matchedFiles.length === 0) {
16240
+ throw new Error(`No service found with id: ${id}`);
16241
+ }
16242
+ await Promise.all(matchedFiles.map((file) => fs3.rm(file)));
16243
+ };
16244
+ var addFileToService = (directory) => async (id, file, version) => {
16245
+ const pathToEvent = await findFileById(directory, id, version);
16246
+ if (!pathToEvent) throw new Error("Cannot find directory to write file to");
16247
+ const contentDirectory = dirname3(pathToEvent);
16248
+ await fs3.writeFile(join4(contentDirectory, file.fileName), file.content);
16249
+ };
16250
+
16185
16251
  // src/index.ts
16186
16252
  var src_default = (path3) => {
16187
16253
  return {
@@ -16191,7 +16257,7 @@ var src_default = (path3) => {
16191
16257
  * @param version - Optional id of the version to get
16192
16258
  * @returns
16193
16259
  */
16194
- getEvent: getEvent(join4(path3, "events")),
16260
+ getEvent: getEvent(join5(path3, "events")),
16195
16261
  /**
16196
16262
  * Adds an event to EventCatalog
16197
16263
  *
@@ -16199,26 +16265,26 @@ var src_default = (path3) => {
16199
16265
  * @param options - Optional options to write the event
16200
16266
  *
16201
16267
  */
16202
- writeEvent: writeEvent(join4(path3, "events")),
16268
+ writeEvent: writeEvent(join5(path3, "events")),
16203
16269
  /**
16204
16270
  * Remove an event to EventCatalog (modeled on the standard POSIX rm utility)
16205
16271
  *
16206
16272
  * @param path - The path to your event, e.g. `/Inventory/InventoryAdjusted`
16207
16273
  *
16208
16274
  */
16209
- rmEvent: rmEvent(join4(path3, "events")),
16275
+ rmEvent: rmEvent(join5(path3, "events")),
16210
16276
  /**
16211
16277
  * Remove an event by an Event id
16212
16278
  *
16213
16279
  * @param id - The id of the event you want to remove
16214
16280
  *
16215
16281
  */
16216
- rmEventById: rmEventById(join4(path3, "events")),
16282
+ rmEventById: rmEventById(join5(path3, "events")),
16217
16283
  /**
16218
16284
  * Moves a given event id to the version directory
16219
16285
  * @param directory
16220
16286
  */
16221
- versionEvent: versionEvent(join4(path3, "events")),
16287
+ versionEvent: versionEvent(join5(path3, "events")),
16222
16288
  /**
16223
16289
  * Adds a file to the given event
16224
16290
  * @param id - The id of the event to add the file to
@@ -16226,7 +16292,7 @@ var src_default = (path3) => {
16226
16292
  * @param version - Optional version of the event to add the file to
16227
16293
  * @returns
16228
16294
  */
16229
- addFileToEvent: addFileToEvent(join4(path3, "events")),
16295
+ addFileToEvent: addFileToEvent(join5(path3, "events")),
16230
16296
  /**
16231
16297
  * Adds a schema to the given event
16232
16298
  * @param id - The id of the event to add the schema to
@@ -16234,20 +16300,68 @@ var src_default = (path3) => {
16234
16300
  * @param version - Optional version of the event to add the schema to
16235
16301
  * @returns
16236
16302
  */
16237
- addSchemaToEvent: addSchemaToEvent(join4(path3, "events"))
16303
+ addSchemaToEvent: addSchemaToEvent(join5(path3, "events")),
16304
+ /**
16305
+ * ================================
16306
+ * SERVICES
16307
+ * ================================
16308
+ */
16309
+ /**
16310
+ * Adds a service to EventCatalog
16311
+ *
16312
+ * @param service - The service to write
16313
+ * @param options - Optional options to write the event
16314
+ *
16315
+ */
16316
+ writeService: writeService(join5(path3, "services")),
16317
+ /**
16318
+ * Returns a service from EventCatalog
16319
+ * @param id - The id of the service to retrieve
16320
+ * @param version - Optional id of the version to get
16321
+ * @returns
16322
+ */
16323
+ getService: getService(join5(path3, "services")),
16324
+ /**
16325
+ * Moves a given service id to the version directory
16326
+ * @param directory
16327
+ */
16328
+ versionService: versionService(join5(path3, "services")),
16329
+ /**
16330
+ * Remove a service from EventCatalog (modeled on the standard POSIX rm utility)
16331
+ *
16332
+ * @param path - The path to your service, e.g. `/InventoryService`
16333
+ *
16334
+ */
16335
+ rmService: rmService(join5(path3, "services")),
16336
+ /**
16337
+ * Remove an service by an service id
16338
+ *
16339
+ * @param id - The id of the service you want to remove
16340
+ *
16341
+ */
16342
+ rmServiceById: rmServiceById(join5(path3, "services")),
16343
+ /**
16344
+ * Adds a file to the given service
16345
+ * @param id - The id of the service to add the file to
16346
+ * @param file - File contents to add including the content and the file name
16347
+ * @param version - Optional version of the service to add the file to
16348
+ * @returns
16349
+ */
16350
+ addFileToService: addFileToService(join5(path3, "services"))
16238
16351
  };
16239
16352
  };
16240
16353
 
16241
16354
  // src/test/events.test.ts
16242
16355
  import path2 from "path";
16243
- import fs3 from "fs";
16244
- var CATALOG_PATH = path2.join(__dirname, "catalog");
16356
+ import fs4 from "fs";
16357
+ var CATALOG_PATH = path2.join(__dirname, "catalog-events");
16245
16358
  var { writeEvent: writeEvent2, getEvent: getEvent2, rmEvent: rmEvent2, rmEventById: rmEventById2, versionEvent: versionEvent2, addFileToEvent: addFileToEvent2, addSchemaToEvent: addSchemaToEvent2 } = src_default(CATALOG_PATH);
16246
16359
  beforeEach(() => {
16247
- fs3.rmSync(CATALOG_PATH, { recursive: true, force: true });
16248
- fs3.mkdirSync(CATALOG_PATH, { recursive: true });
16360
+ fs4.rmSync(CATALOG_PATH, { recursive: true, force: true });
16361
+ fs4.mkdirSync(CATALOG_PATH, { recursive: true });
16249
16362
  });
16250
16363
  afterEach(() => {
16364
+ fs4.rmSync(CATALOG_PATH, { recursive: true, force: true });
16251
16365
  });
16252
16366
  describe("Events SDK", () => {
16253
16367
  describe("getEvent", () => {
@@ -16319,7 +16433,7 @@ describe("Events SDK", () => {
16319
16433
  markdown: "# Hello world"
16320
16434
  });
16321
16435
  const event = await getEvent2("InventoryAdjusted");
16322
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16436
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16323
16437
  globalExpect(event).toEqual({
16324
16438
  id: "InventoryAdjusted",
16325
16439
  name: "Inventory Adjusted",
@@ -16339,7 +16453,7 @@ describe("Events SDK", () => {
16339
16453
  },
16340
16454
  { path: "/Inventory/InventoryAdjusted" }
16341
16455
  );
16342
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/Inventory/InventoryAdjusted", "index.md"))).toBe(true);
16456
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/Inventory/InventoryAdjusted", "index.md"))).toBe(true);
16343
16457
  });
16344
16458
  it("throws an error when trying to write an event that already exists", async () => {
16345
16459
  const createEvent = async () => writeEvent2({
@@ -16370,9 +16484,9 @@ describe("Events SDK", () => {
16370
16484
  summary: "This is a summary",
16371
16485
  markdown: "# Hello world"
16372
16486
  });
16373
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16487
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16374
16488
  await rmEvent2("/InventoryAdjusted");
16375
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16489
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16376
16490
  });
16377
16491
  });
16378
16492
  describe("rmEventById", () => {
@@ -16384,9 +16498,9 @@ describe("Events SDK", () => {
16384
16498
  summary: "This is a summary",
16385
16499
  markdown: "# Hello world"
16386
16500
  });
16387
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16501
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16388
16502
  await rmEventById2("InventoryAdjusted");
16389
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16503
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16390
16504
  });
16391
16505
  it("removes an event from eventcatalog by id and version", async () => {
16392
16506
  await writeEvent2({
@@ -16396,9 +16510,9 @@ describe("Events SDK", () => {
16396
16510
  summary: "This is a summary",
16397
16511
  markdown: "# Hello world"
16398
16512
  });
16399
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16513
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16400
16514
  await rmEventById2("InventoryAdjusted", "0.0.1");
16401
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16515
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16402
16516
  });
16403
16517
  it("if version is given, only removes that version and not any other versions of the event", async () => {
16404
16518
  await writeEvent2({
@@ -16416,11 +16530,11 @@ describe("Events SDK", () => {
16416
16530
  summary: "This is a summary",
16417
16531
  markdown: "# Hello world"
16418
16532
  });
16419
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16420
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.1", "index.md"))).toBe(true);
16533
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16534
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.1", "index.md"))).toBe(true);
16421
16535
  await rmEventById2("InventoryAdjusted", "0.0.1");
16422
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16423
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "index.md"))).toBe(false);
16536
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(true);
16537
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "index.md"))).toBe(false);
16424
16538
  });
16425
16539
  });
16426
16540
  describe("versionEvent", () => {
@@ -16433,8 +16547,8 @@ describe("Events SDK", () => {
16433
16547
  markdown: "# Hello world"
16434
16548
  });
16435
16549
  await versionEvent2("InventoryAdjusted");
16436
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "index.md"))).toBe(true);
16437
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16550
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "index.md"))).toBe(true);
16551
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16438
16552
  });
16439
16553
  it("adds the given event to the versioned directory and all files that are associated to it", async () => {
16440
16554
  await writeEvent2({
@@ -16444,12 +16558,12 @@ describe("Events SDK", () => {
16444
16558
  summary: "This is a summary",
16445
16559
  markdown: "# Hello world"
16446
16560
  });
16447
- await fs3.writeFileSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "schema.json"), "SCHEMA!");
16561
+ await fs4.writeFileSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "schema.json"), "SCHEMA!");
16448
16562
  await versionEvent2("InventoryAdjusted");
16449
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "index.md"))).toBe(true);
16450
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "schema.json"))).toBe(true);
16451
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16452
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "schema.json"))).toBe(false);
16563
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "index.md"))).toBe(true);
16564
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.2", "schema.json"))).toBe(true);
16565
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "index.md"))).toBe(false);
16566
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "schema.json"))).toBe(false);
16453
16567
  });
16454
16568
  });
16455
16569
  describe("addFileToEvent", () => {
@@ -16463,7 +16577,7 @@ describe("Events SDK", () => {
16463
16577
  markdown: "# Hello world"
16464
16578
  });
16465
16579
  await addFileToEvent2("InventoryAdjusted", file);
16466
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "test.txt"))).toBe(true);
16580
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "test.txt"))).toBe(true);
16467
16581
  });
16468
16582
  it("takes a given file and version and writes the file to the correct location", async () => {
16469
16583
  const file = { content: "hello", fileName: "test.txt" };
@@ -16476,7 +16590,7 @@ describe("Events SDK", () => {
16476
16590
  });
16477
16591
  await versionEvent2("InventoryAdjusted");
16478
16592
  await addFileToEvent2("InventoryAdjusted", file, "0.0.1");
16479
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.1", "test.txt"))).toBe(true);
16593
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.1", "test.txt"))).toBe(true);
16480
16594
  });
16481
16595
  it("throws an error when trying to write to a event that does not exist", () => {
16482
16596
  const file = { content: "hello", fileName: "test.txt" };
@@ -16505,7 +16619,7 @@ describe("Events SDK", () => {
16505
16619
  markdown: "# Hello world"
16506
16620
  });
16507
16621
  await addSchemaToEvent2("InventoryAdjusted", file);
16508
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "schema.json"))).toBe(true);
16622
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted", "schema.json"))).toBe(true);
16509
16623
  });
16510
16624
  it("takes a given file and version and writes the file to the correct location", async () => {
16511
16625
  const schema = `{
@@ -16529,7 +16643,7 @@ describe("Events SDK", () => {
16529
16643
  });
16530
16644
  await versionEvent2("InventoryAdjusted");
16531
16645
  await addSchemaToEvent2("InventoryAdjusted", file, "0.0.1");
16532
- globalExpect(fs3.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.1", "schema.json"))).toBe(true);
16646
+ globalExpect(fs4.existsSync(path2.join(CATALOG_PATH, "events/InventoryAdjusted/versioned/0.0.1", "schema.json"))).toBe(true);
16533
16647
  });
16534
16648
  it("throws an error when trying to write to a event that does not exist", () => {
16535
16649
  const file = { schema: "hello", fileName: "test.txt" };