Skip to content

Commit

Permalink
fixed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
David Boyne authored and David Boyne committed Aug 12, 2024
1 parent 104ca2a commit 2235113
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ await versionEvent('InventoryEvent');
// Returns the service /services/PaymentService
const service await getService('PaymentService');
```

See the [SDK docs](https://www.eventcatalog.dev/docs/sdk) for more information and examples.
30 changes: 19 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import {
addFileToCommand,
addSchemaToCommand,
} from './commands';
import { writeService, getService, versionService, rmService, rmServiceById, addFileToService, addMessageToService } from './services';
import {
writeService,
getService,
versionService,
rmService,
rmServiceById,
addFileToService,
addMessageToService,
} from './services';
import { writeDomain, getDomain, versionDomain, rmDomain, rmDomainById, addFileToDomain } from './domains';

/**
Expand Down Expand Up @@ -236,11 +244,11 @@ export default (path: string) => {
*
* const { addEventToService } = utils('/path/to/eventcatalog');
*
* // adds InventoryUpdatedEvent event with version '2.0.0' to the latest InventoryService event
* // adds a new event (InventoryUpdatedEvent) that the InventoryService will send
* await addEventToService('InventoryService', 'sends', { event: 'InventoryUpdatedEvent', version: '2.0.0' });
*
* // adds InventoryUpdatedEvent enent with version '2.0.0' to a specific version of the InventoryService event
* await addFileToService('InventoryService', 'sends', { content: 'InventoryUpdatedEvent', version: 'version' }, '0.0.1');
*
* // adds a new event (OrderComplete) that the InventoryService will receive
* await addEventToService('InventoryService', 'receives', { event: 'OrderComplete', version: '2.0.0' });
*
* ```
*/
Expand All @@ -254,13 +262,13 @@ export default (path: string) => {
* ```ts
* import utils from '@eventcatalog/utils';
*
* const { addEventToService } = utils('/path/to/eventcatalog');
*
* // adds InventoryUpdatedEvent event with version '2.0.0' to the latest InventoryService event
* await addEventToService('InventoryService', 'sends', { event: 'InventoryUpdatedEvent', version: '2.0.0' });
* const { addCommandToService } = utils('/path/to/eventcatalog');
*
* // adds InventoryUpdatedEvent enent with version '2.0.0' to a specific version of the InventoryService event
* await addFileToService('InventoryService', 'sends', { content: 'InventoryUpdatedEvent', version: 'version' }, '0.0.1');
* // adds a new command (UpdateInventoryCommand) that the InventoryService will send
* await addCommandToService('InventoryService', 'sends', { command: 'UpdateInventoryCommand', version: '2.0.0' });
*
* // adds a new command (VerifyInventory) that the InventoryService will receive
* await addCommandToService('InventoryService', 'receives', { command: 'VerifyInventory', version: '2.0.0' });
*
* ```
*/
Expand Down
43 changes: 21 additions & 22 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,34 @@ export const addFileToService =

export const addMessageToService =
(directory: string) => async (id: string, direction: string, event: { id: string; version: string }, version?: string) => {

let service:Service = await getService(directory)(id, version);
let service: Service = await getService(directory)(id, version);

if (direction === 'sends') {
if(service.sends === undefined) {
service.sends = [];
}
// We first check if the event is already in the list
for (let i = 0; i < service.sends.length; i++) {
if (service.sends[i].id === event.id && service.sends[i].version === event.version) {
return;
}
if (service.sends === undefined) {
service.sends = [];
}
// We first check if the event is already in the list
for (let i = 0; i < service.sends.length; i++) {
if (service.sends[i].id === event.id && service.sends[i].version === event.version) {
return;
}
service.sends.push({id: event.id, version: event.version});
}
service.sends.push({ id: event.id, version: event.version });
} else if (direction === 'receives') {
if(service.receives === undefined) {
service.receives = [];
}
// We first check if the event is already in the list
for (let i = 0; i < service.receives.length; i++) {
if (service.receives[i].id === event.id && service.receives[i].version === event.version) {
return;
}
if (service.receives === undefined) {
service.receives = [];
}
// We first check if the event is already in the list
for (let i = 0; i < service.receives.length; i++) {
if (service.receives[i].id === event.id && service.receives[i].version === event.version) {
return;
}
service.receives.push({id: event.id, version: event.version});
}
service.receives.push({ id: event.id, version: event.version });
} else {
throw new Error(`Direction ${direction} is invalid, only 'receives' and 'sends' are supported`);
throw new Error(`Direction ${direction} is invalid, only 'receives' and 'sends' are supported`);
}

await rmServiceById(directory)(id, version);
await writeService(directory)(service);
}
};
35 changes: 17 additions & 18 deletions src/test/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import fs from 'node:fs';

const CATALOG_PATH = path.join(__dirname, 'catalog-services');

const { writeService, getService, versionService, rmService, rmServiceById, addFileToService, addEventToService, writeEvent } = utils(CATALOG_PATH);
const { writeService, getService, versionService, rmService, rmServiceById, addFileToService, addEventToService, writeEvent } =
utils(CATALOG_PATH);

// clean the catalog before each test
beforeEach(() => {
Expand Down Expand Up @@ -309,11 +310,8 @@ describe('Services SDK', () => {
});
});


describe('addEventToService', () => {

it('takes an existing event and adds it to the sends of an existing service', async () => {

await writeService({
id: 'InventoryService',
name: 'Inventory Service',
Expand All @@ -331,17 +329,17 @@ describe('Services SDK', () => {
name: 'Inventory Service',
version: '0.0.1',
summary: 'Service that handles the inventory',
sends: [{
id: 'InventoryUpdatedEvent',
version: '2.0.0'
}],
sends: [
{
id: 'InventoryUpdatedEvent',
version: '2.0.0',
},
],
markdown: '# Hello world',
});

});

it('takes an existing event and adds it to the receives of an existing service', async () => {

await writeService({
id: 'InventoryService',
name: 'Inventory Service',
Expand All @@ -359,13 +357,14 @@ describe('Services SDK', () => {
name: 'Inventory Service',
version: '0.0.1',
summary: 'Service that handles the inventory',
receives: [{
id: 'InventoryUpdatedEvent',
version: '2.0.0'
}],
receives: [
{
id: 'InventoryUpdatedEvent',
version: '2.0.0',
},
],
markdown: '# Hello world',
});

});

it('throws an error when trying to add an event to a service that does not exist', () => {
Expand All @@ -375,7 +374,6 @@ describe('Services SDK', () => {
});

it('throws an error when trying to add an event to a service with an unsupported direction', async () => {

await writeService({
id: 'InventoryService',
name: 'Inventory Service',
Expand All @@ -384,8 +382,9 @@ describe('Services SDK', () => {
markdown: '# Hello world',
});

expect(addEventToService('InventoryService', 'doesnotexist', { id: 'InventoryUpdatedEvent', version: '2.0.0' }, '0.0.1')).rejects.toThrowError('Direction doesnotexist is invalid, only \'receives\' and \'sends\' are supported');
expect(
addEventToService('InventoryService', 'doesnotexist', { id: 'InventoryUpdatedEvent', version: '2.0.0' }, '0.0.1')
).rejects.toThrowError("Direction doesnotexist is invalid, only 'receives' and 'sends' are supported");
});
});

});

0 comments on commit 2235113

Please sign in to comment.