Skip to content

Commit 1f70e74

Browse files
feat: add support for the /read-event-type endpoint (#243)
* feat: add support for the `/read-event-type` endpoint * Update README.md --------- Co-authored-by: Golo Roden <[email protected]>
1 parent 4f497e9 commit 1f70e74

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ for await (const eventType of client.readEventTypes()) {
417417
controller.abort();
418418
```
419419

420+
### Listing a Specific Event Type
421+
422+
To list a specific event type, call the `readEventType` function with the event type as an argument. The function returns the detailed event type, which includes the schema:
423+
424+
```typescript
425+
eventType = await client.readEventType("io.eventsourcingdb.library.book-acquired")
426+
```
427+
420428
### Using Testcontainers
421429

422430
Import the `Container` class, create an instance, call the `start` function to run a test container, get a client, run your test code, and finally call the `stop` function to stop the test container:

src/Client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,37 @@ class Client {
424424
})();
425425
}
426426

427+
public async readEventType(eventType: string): Promise<EventType> {
428+
const url = this.#getUrl('/api/v1/read-event-type');
429+
const response = await fetch(url, {
430+
method: 'post',
431+
headers: {
432+
authorization: `Bearer ${this.#apiToken}`,
433+
'content-type': 'application/json',
434+
},
435+
body: JSON.stringify({
436+
eventType,
437+
}),
438+
});
439+
440+
if (response.status !== 200) {
441+
throw new Error(
442+
`Failed to read event type, got HTTP status code '${response.status}', expected '200'.`,
443+
);
444+
}
445+
const responseBody = await response.json();
446+
if (
447+
!hasShapeOf(responseBody, {
448+
eventType: 'string',
449+
isPhantom: true,
450+
})
451+
) {
452+
throw new Error('Failed to parse response.');
453+
}
454+
455+
return responseBody;
456+
}
457+
427458
public readEventTypes(signal?: AbortSignal): AsyncGenerator<EventType, void, void> {
428459
const url = this.#getUrl('/api/v1/read-event-types');
429460
const apiToken = this.#apiToken;

src/readEventType.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import assert from 'node:assert/strict';
2+
import { afterEach, beforeEach, suite, test } from 'node:test';
3+
import { Container } from './Container.js';
4+
import type { EventCandidate } from './EventCandidate.js';
5+
import { getImageVersionFromDockerfile } from './getImageVersionFromDockerfile.js';
6+
7+
suite('readEventType', { timeout: 20_000 }, () => {
8+
let container: Container;
9+
10+
beforeEach(async () => {
11+
const imageVersion = getImageVersionFromDockerfile();
12+
container = new Container().withImageTag(imageVersion);
13+
await container.start();
14+
});
15+
16+
afterEach(async () => {
17+
await container.stop();
18+
});
19+
20+
test('fails if the event type does not exist.', async (): Promise<void> => {
21+
const client = container.getClient();
22+
23+
await assert.rejects(
24+
async () => {
25+
await client.readEventType('non.existent.eventType');
26+
},
27+
{
28+
name: 'Error',
29+
message: "Failed to read event type, got HTTP status code '404', expected '200'.",
30+
},
31+
);
32+
});
33+
34+
test('fails if the evenet type is malformed.', async (): Promise<void> => {
35+
const client = container.getClient();
36+
37+
await assert.rejects(
38+
async () => {
39+
await client.readEventType('malformed.eventType.');
40+
},
41+
{
42+
name: 'Error',
43+
message: "Failed to read event type, got HTTP status code '400', expected '200'.",
44+
},
45+
);
46+
});
47+
48+
test('read an existing event type.', async (): Promise<void> => {
49+
const client = container.getClient();
50+
51+
const firstEvent: EventCandidate = {
52+
source: 'https://www.eventsourcingdb.io',
53+
subject: '/test',
54+
type: 'io.eventsourcingdb.test.foo',
55+
data: {
56+
value: 23,
57+
},
58+
};
59+
60+
const secondEvent: EventCandidate = {
61+
source: 'https://www.eventsourcingdb.io',
62+
subject: '/test',
63+
type: 'io.eventsourcingdb.test.bar',
64+
data: {
65+
value: 42,
66+
},
67+
};
68+
69+
await client.writeEvents([firstEvent, secondEvent]);
70+
71+
const readEventType = await client.readEventType('io.eventsourcingdb.test.foo');
72+
assert.equal(readEventType.eventType, 'io.eventsourcingdb.test.foo');
73+
assert.equal(readEventType.isPhantom, false);
74+
assert.equal(readEventType.schema, undefined);
75+
});
76+
});

0 commit comments

Comments
 (0)