Skip to content

Commit

Permalink
Merge pull request #38 from event-catalog/writing-services-messages-a…
Browse files Browse the repository at this point in the history
…re-now-unique

feat(sdk): writing messages to services are now unique
  • Loading branch information
boyney123 authored Oct 2, 2024
2 parents 0286274 + 6a76b8d commit 6f4540e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-doors-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@eventcatalog/sdk": patch
---

feat(sdk): writing messages to services are now unique
18 changes: 16 additions & 2 deletions src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'node:fs/promises';
import { copy, CopyFilterAsync, CopyFilterSync } from 'fs-extra';
import { join } from 'node:path';
import matter from 'gray-matter';
import { satisfies, validRange } from 'semver';
import { satisfies, validRange, valid } from 'semver';

/**
* Returns true if a given version of a resource id exists in the catalog
Expand Down Expand Up @@ -32,7 +32,7 @@ export const findFileById = async (catalogDir: string, id: string, version?: str

const semverRange = validRange(version);

if (semverRange) {
if (semverRange && valid(version)) {
const match = parsedFiles.filter((c) => satisfies(c.version, semverRange));
return match.length > 0 ? match[0].path : undefined;
}
Expand Down Expand Up @@ -107,3 +107,17 @@ export const copyDir = async (catalogDir: string, source: string, target: string
// Remove the tmp directory
await fs.rm(tmpDirectory, { recursive: true });
};

// Makes sure values in sends/recieves are unique
export const uniqueMessages = (messages: { id: string; version: string }[]): { id: string; version: string }[] => {
const uniqueSet = new Set();

return messages.filter((message) => {
const key = `${message.id}-${message.version}`;
if (!uniqueSet.has(key)) {
uniqueSet.add(key);
return true;
}
return false;
});
};
17 changes: 14 additions & 3 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
versionResource,
writeResource,
} from './internal/resources';
import { findFileById } from './internal/utils';
import { findFileById, uniqueMessages } from './internal/utils';

/**
* Returns a service from EventCatalog.
Expand Down Expand Up @@ -67,8 +67,19 @@ export const getService =
*/
export const writeService =
(directory: string) =>
async (service: Service, options: { path: string } = { path: '' }) =>
writeResource(directory, { ...service }, { ...options, type: 'service' });
async (service: Service, options: { path: string } = { path: '' }) => {
const resource: Service = { ...service };

if (Array.isArray(service.sends)) {
resource.sends = uniqueMessages(service.sends);
}

if (Array.isArray(service.receives)) {
resource.receives = uniqueMessages(service.receives);
}

return writeResource(directory, resource, { ...options, type: 'service' });
}

/**
* Version a service by it's id.
Expand Down
22 changes: 22 additions & 0 deletions src/test/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ describe('Events SDK', () => {
});
});

it('returns the version of the event even if the event does not match semver matching', async () =>{

await writeEvent({
id: 'InventoryAdjusted',
name: 'Inventory Adjusted',
version: '100',
summary: 'This is a summary',
markdown: '# Hello world',
});

const test = await getEvent('InventoryAdjusted', '100');

expect(test).toEqual({
id: 'InventoryAdjusted',
name: 'Inventory Adjusted',
version: '100',
summary: 'This is a summary',
markdown: '# Hello world',
});

});

it('returns undefined when a given resource is not found', async () => {
const event = await getEvent('InventoryAdjusted');
await expect(event).toEqual(undefined);
Expand Down
53 changes: 53 additions & 0 deletions src/test/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,59 @@ describe('Services SDK', () => {
expect(fs.existsSync(path.join(CATALOG_PATH, 'services/Inventory/InventoryService', 'index.md'))).toBe(true);
});

it('messages written to a service are always unique', async () => {

await writeService(
{
id: 'InventoryService',
name: 'Inventory Service',
version: '0.0.1',
summary: 'Service tat handles the inventory',
markdown: '# Hello world',
sends: [
{ id: 'InventoryUpdatedEvent', version: '2.0.0' },
{ id: 'InventoryUpdatedEvent', version: '2.0.0' },
{ id: 'InventoryRemoved', version: '1.0.0' },
{ id: 'InventoryRemoved', version: '1.0.0' },
{ id: 'InventoryUpdated', version: '1.0.0' },
],
receives: [
{ id: 'OrderComplete', version: '2.0.0' },
{ id: 'OrderComplete', version: '2.0.0' },
],
},
{ path: '/Inventory/InventoryService' }
);

const service = await getService('InventoryService');

expect(service.sends).toHaveLength(3);
expect(service.receives).toHaveLength(1);

expect(service.sends).toEqual([
{
"id": "InventoryUpdatedEvent",
"version": "2.0.0"
},
{
"id": "InventoryRemoved",
"version": "1.0.0"
},
{
"id": "InventoryUpdated",
"version": "1.0.0"
}
]);

expect(service.receives).toEqual([
{
"id": "OrderComplete",
"version": "2.0.0"
}
]);

});

it('throws an error when trying to write an service that already exists', async () => {
await writeService({
id: 'InventoryService',
Expand Down

0 comments on commit 6f4540e

Please sign in to comment.