Skip to content

Commit

Permalink
chore: Ensure event payload is an object (#169)
Browse files Browse the repository at this point in the history
* chore: Ensure event payload is an object

* fix tests
  • Loading branch information
felipecsl authored Dec 9, 2024
1 parent b418702 commit d9b8308
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,12 +979,12 @@ export default class EppoClient {
* Enqueues an arbitrary event. Events must have a type and a payload.
* TODO: enforce max message size
*/
track(type: string, event: unknown) {
track(type: string, payload: Record<string, unknown>) {
this.eventDispatcher.dispatch({
uuid: randomUUID(),
type,
timestamp: new Date().getTime(),
payload: event,
payload,
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/events/batch-event-processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ describe('BatchEventProcessor', () => {
expect(processor.nextBatch()).toHaveLength(0);
const timestamp = new Date().getTime();
const type = 'test';
const event1 = { uuid: 'foo-1', payload: 'event1', timestamp, type };
const event2 = { uuid: 'foo-2', payload: 'event2', timestamp, type };
const event3 = { uuid: 'foo-3', payload: 'event3', timestamp, type };
const event1 = { uuid: 'foo-1', payload: { id: 'event1' }, timestamp, type };
const event2 = { uuid: 'foo-2', payload: { id: 'event2' }, timestamp, type };
const event3 = { uuid: 'foo-3', payload: { id: 'event3' }, timestamp, type };
processor.push(event1);
processor.push(event2);
processor.push(event3);
Expand Down
30 changes: 15 additions & 15 deletions src/events/default-event-dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ describe('DefaultEventDispatcher', () => {
// Add three events to the queue
dispatcher.dispatch({
uuid: 'foo-1',
payload: 'event1',
payload: { foo: 'event1' },
timestamp: new Date().getTime(),
type: 'foo',
});
dispatcher.dispatch({
uuid: 'foo-2',
payload: 'event2',
payload: { foo: 'event2' },
timestamp: new Date().getTime(),
type: 'foo',
});
dispatcher.dispatch({
uuid: 'foo-3',
payload: 'event3',
payload: { foo: 'event3' },
timestamp: new Date().getTime(),
type: 'foo',
});
Expand All @@ -81,19 +81,19 @@ describe('DefaultEventDispatcher', () => {
const { dispatcher } = createDispatcher();
dispatcher.dispatch({
uuid: 'foo-1',
payload: 'event1',
payload: { foo: 'event1' },
timestamp: new Date().getTime(),
type: 'foo',
});
dispatcher.dispatch({
uuid: 'foo-2',
payload: 'event2',
payload: { foo: 'event2' },
timestamp: new Date().getTime(),
type: 'foo',
});
dispatcher.dispatch({
uuid: 'foo-3',
payload: 'event3',
payload: { foo: 'event3' },
timestamp: new Date().getTime(),
type: 'foo',
});
Expand All @@ -114,8 +114,8 @@ describe('DefaultEventDispatcher', () => {
let fetchOptions = fetch.mock.calls[0][1];
let payload = JSON.parse(fetchOptions.body);
expect(payload).toEqual([
expect.objectContaining({ payload: 'event1' }),
expect.objectContaining({ payload: 'event2' }),
expect.objectContaining({ payload: { foo: 'event1' } }),
expect.objectContaining({ payload: { foo: 'event2' } }),
]);

await new Promise((resolve) => setTimeout(resolve, 100));
Expand All @@ -130,7 +130,7 @@ describe('DefaultEventDispatcher', () => {

fetchOptions = fetch.mock.calls[1][1];
payload = JSON.parse(fetchOptions.body);
expect(payload).toEqual([expect.objectContaining({ payload: 'event3' })]);
expect(payload).toEqual([expect.objectContaining({ payload: { foo: 'event3' } })]);
});

it('does not schedule delivery if the queue is empty', async () => {
Expand All @@ -156,13 +156,13 @@ describe('DefaultEventDispatcher', () => {
const { dispatcher } = createDispatcher({ networkStatusListener });
dispatcher.dispatch({
uuid: '1',
payload: 'event1',
payload: { foo: 'event1' },
timestamp: new Date().getTime(),
type: 'foo',
});
dispatcher.dispatch({
uuid: '2',
payload: 'event2',
payload: { foo: 'event2' },
timestamp: new Date().getTime(),
type: 'foo',
});
Expand Down Expand Up @@ -190,13 +190,13 @@ describe('DefaultEventDispatcher', () => {
const { dispatcher } = createDispatcher({ networkStatusListener });
dispatcher.dispatch({
uuid: '1',
payload: 'event1',
payload: { foo: 'event1' },
timestamp: new Date().getTime(),
type: 'foo',
});
dispatcher.dispatch({
uuid: '2',
payload: 'event2',
payload: { foo: 'event2' },
timestamp: new Date().getTime(),
type: 'foo',
});
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('DefaultEventDispatcher', () => {
const { dispatcher } = createDispatcher();
dispatcher.dispatch({
uuid: 'foo',
payload: 'event1',
payload: { foo: 'event1' },
timestamp: new Date().getTime(),
type: 'foo',
});
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('DefaultEventDispatcher', () => {
const { dispatcher } = createDispatcher({ maxRetries: 1 }, eventQueue);
dispatcher.dispatch({
uuid: 'foo',
payload: 'event1',
payload: { foo: 'event1' },
timestamp: new Date().getTime(),
type: 'foo',
});
Expand Down
2 changes: 1 addition & 1 deletion src/events/event-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type Event = {
uuid: string;
timestamp: number;
type: string;
payload: unknown;
payload: Record<string, unknown>;
};

export default interface EventDispatcher {
Expand Down

0 comments on commit d9b8308

Please sign in to comment.