From b7ce82dd66af802d538f0be9f3620cd6d2dce94b Mon Sep 17 00:00:00 2001 From: Haff Date: Thu, 22 Feb 2024 08:13:35 -0500 Subject: [PATCH] Haff/change dispatch to send (#46) * rename dispatchEvent to sendEvent * bump version for event-producer change --- packages/event-producer/package.json | 2 +- packages/event-producer/src/index.ts | 14 ++++----- .../dispatch.test.ts => send/send.test.ts} | 14 ++++----- .../{dispatch/dispatch.ts => send/send.ts} | 30 +++++++++---------- packages/event-producer/src/types.ts | 4 +-- packages/event-producer/test/demo/demo.ts | 13 ++++---- .../event-producer/test/fixtures/events.ts | 4 +-- 7 files changed, 40 insertions(+), 41 deletions(-) rename packages/event-producer/src/{dispatch/dispatch.test.ts => send/send.test.ts} (80%) rename packages/event-producer/src/{dispatch/dispatch.ts => send/send.ts} (80%) diff --git a/packages/event-producer/package.json b/packages/event-producer/package.json index edc1930d..563ef164 100644 --- a/packages/event-producer/package.json +++ b/packages/event-producer/package.json @@ -1,6 +1,6 @@ { "name": "@tidal-music/event-producer", - "version": "1.1.3", + "version": "1.2.0", "type": "module", "files": [ "dist" diff --git a/packages/event-producer/src/index.ts b/packages/event-producer/src/index.ts index e5917feb..f8437572 100644 --- a/packages/event-producer/src/index.ts +++ b/packages/event-producer/src/index.ts @@ -3,29 +3,29 @@ import { IllegalArgumentError } from '@tidal-music/common'; import * as bus from './bus'; import type { Config } from './config'; import { getConfig } from './config'; -import * as dispatch from './dispatch/dispatch'; import { init as _init } from './init'; import * as monitor from './monitor'; import * as outage from './outage'; import * as queue from './queue/queue'; +import * as send from './send/send'; import { submitEvents } from './submit/submit'; -import type { DispatchedEvent } from './types'; +import type { SentEvent } from './types'; export { setConsentCategory, setCredentialsProvider } from './config'; export type * from './types'; /** - * This is the user exposed function that wraps dispatchEvent with the config and credentialsProvider. + * This is the user exposed function that wraps sendEvent with the config and credentialsProvider. * - * @param {DispatchedEvent} event The event to add to the queue + * @param {SentEvent} event The event to add to the queue */ // TODO: error handling. -export const dispatchEvent = (event: DispatchedEvent) => { +export const sendEvent = (event: SentEvent) => { const config = getConfig(); const { credentialsProvider } = config; if (credentialsProvider) { - dispatch - .dispatchEvent({ + send + .sendEvent({ config, credentialsProvider, event, diff --git a/packages/event-producer/src/dispatch/dispatch.test.ts b/packages/event-producer/src/send/send.test.ts similarity index 80% rename from packages/event-producer/src/dispatch/dispatch.test.ts rename to packages/event-producer/src/send/send.test.ts index 075392c6..93b4dd80 100644 --- a/packages/event-producer/src/dispatch/dispatch.test.ts +++ b/packages/event-producer/src/send/send.test.ts @@ -7,7 +7,7 @@ import * as monitor from '../monitor'; import * as queue from '../queue'; import * as uuid from '../uuid/uuid'; -import { type DispatchEventParams, dispatchEvent } from './dispatch'; +import { type SendEventParams, sendEvent } from './send'; vi.mock('../monitor'); vi.mock('../queue'); @@ -15,20 +15,20 @@ vi.mock('@tidal-music/true-time', () => ({ trueTime: { now: vi.fn(() => 1337) }, })); -describe.sequential('dispatchEvent', () => { +describe.sequential('sendEvent', () => { beforeEach(async () => { await uuid.init(); }); it('if event consentCategory is not blocked -> adds event to queue', async () => { vi.spyOn(uuid, 'uuid').mockReturnValue('fakeUuid'); - const dispatchEventPayload: DispatchEventParams = { + const sendEventPayload: SendEventParams = { config, credentialsProvider: credentialsProvider1, event: eventPayload1, }; - await dispatchEvent(dispatchEventPayload); + await sendEvent(sendEventPayload); const { consentCategory, ...eventWithoutConsentCategory } = - dispatchEventPayload.event; + sendEventPayload.event; expect(queue.addEvent).toHaveBeenCalledWith( expect.objectContaining({ payload: JSON.stringify({ @@ -41,7 +41,7 @@ describe.sequential('dispatchEvent', () => { }); it('if event consentCategory is blocked -> monitor called', async () => { - const dispatchEventPayload: DispatchEventParams = { + const sendEventPayload: SendEventParams = { config, credentialsProvider: credentialsProvider1, event: { @@ -49,7 +49,7 @@ describe.sequential('dispatchEvent', () => { consentCategory: 'TARGETING', }, }; - await dispatchEvent(dispatchEventPayload); + await sendEvent(sendEventPayload); expect(queue.addEvent).not.toHaveBeenCalled(); expect(monitor.registerDroppedEvent).toHaveBeenCalledOnce(); diff --git a/packages/event-producer/src/dispatch/dispatch.ts b/packages/event-producer/src/send/send.ts similarity index 80% rename from packages/event-producer/src/dispatch/dispatch.ts rename to packages/event-producer/src/send/send.ts index ef58c2dd..a20f6a5a 100644 --- a/packages/event-producer/src/dispatch/dispatch.ts +++ b/packages/event-producer/src/send/send.ts @@ -4,13 +4,13 @@ import { trueTime } from '@tidal-music/true-time'; import type { Config } from '../config'; import * as monitor from '../monitor'; import * as queue from '../queue'; -import type { DispatchedEvent, EPEvent } from '../types'; +import type { EPEvent, SentEvent } from '../types'; import { getEventHeaders } from '../utils/headerUtils'; import { validateEvent } from '../utils/validateEvent'; import { uuid } from '../uuid/uuid'; type CreatePayloadParams = { - event: DispatchedEvent; + event: SentEvent; id: string; ts: string; }; @@ -30,7 +30,7 @@ const createPayload = ({ event, id, ts }: CreatePayloadParams): string => { /** * Creates an EPEvent to be sent to the event producer * - * @param {DispatchEventParams} params + * @param {SendEventParams} params * * @returns {Promise} */ @@ -38,7 +38,7 @@ const createEvent = async ({ config, credentialsProvider, event, -}: DispatchEventParams): Promise => { +}: SendEventParams): Promise => { const id = uuid(); const sentTimestamp = trueTime.now().toString(); const headers = getEventHeaders({ @@ -62,7 +62,7 @@ const createEvent = async ({ }; /* c8 ignore start debug only */ -export const strictEventCheck = (event: DispatchedEvent) => { +export const strictEventCheck = (event: SentEvent) => { if (!event.payload) { throw new Error(`Event is missing payload!: ${JSON.stringify(event)}`); } @@ -77,32 +77,32 @@ export const strictEventCheck = (event: DispatchedEvent) => { }; /* c8 ignore stop */ -export type DispatchEventParams = { +export type SendEventParams = { config: Config; credentialsProvider: CredentialsProvider; - event: DispatchedEvent; + event: SentEvent; }; /** * Receives an event, validates it, converts it to an EPEvent, and adds it to the queue * - * @param {DispatchEventParams} params + * @param {SendEventParams} params * * @returns {Promise} */ -export const dispatchEvent = async ({ +export const sendEvent = async ({ config, credentialsProvider, - event: dispatchedEvent, -}: DispatchEventParams): Promise | void> => { + event: sentEvent, +}: SendEventParams): Promise | void> => { /* c8 ignore start debug only */ if (config.strictMode) { - strictEventCheck(dispatchedEvent); + strictEventCheck(sentEvent); } /* c8 ignore stop */ - if (config.blockedConsentCategories[dispatchedEvent.consentCategory]) { + if (config.blockedConsentCategories[sentEvent.consentCategory]) { monitor.registerDroppedEvent({ - eventName: dispatchedEvent.name, + eventName: sentEvent.name, reason: 'consentFilteredEvents', }); return Promise.resolve(); @@ -110,7 +110,7 @@ export const dispatchEvent = async ({ const event = await createEvent({ config, credentialsProvider, - event: dispatchedEvent, + event: sentEvent, }); if (validateEvent(event)) { diff --git a/packages/event-producer/src/types.ts b/packages/event-producer/src/types.ts index 58d521f4..a6663aac 100644 --- a/packages/event-producer/src/types.ts +++ b/packages/event-producer/src/types.ts @@ -13,7 +13,7 @@ export type EventHeaders = Record; /** * This is an incoming raw event. */ -export type DispatchedEvent = { +export type SentEvent = { consentCategory: ConsentCategory; headers?: EventHeaders; name: string; @@ -23,7 +23,7 @@ export type DispatchedEvent = { /** * This is an outgoing prepared event. */ -export type EPEvent = Omit & { +export type EPEvent = Omit & { id: string; payload: string; }; diff --git a/packages/event-producer/test/demo/demo.ts b/packages/event-producer/test/demo/demo.ts index 9d38d1c9..e34c609e 100644 --- a/packages/event-producer/test/demo/demo.ts +++ b/packages/event-producer/test/demo/demo.ts @@ -1,6 +1,6 @@ import { init } from '../../src'; import type { EPEvent } from '../../src'; -import { dispatchEvent } from '../../src/dispatch/dispatch'; +import { sendEvent } from '../../src/send/send'; import { config as configFixture } from '../fixtures/config'; import { credentials1, @@ -33,7 +33,7 @@ class EventDemo extends HTMLElement { render() { this.shadow.innerHTML = `

  • -
  • +
  • ${this.fakeQueue .map( ({ id, name }) => @@ -42,8 +42,7 @@ class EventDemo extends HTMLElement { .join('
  • ')}
`; if (this.shadowRoot) { - const dispatchEventBtn = - this.shadowRoot.querySelector('#dispatchEventBtn'); + const sendEventBtn = this.shadowRoot.querySelector('#sendEventBtn'); const setCredentialsTokenBtn = this.shadowRoot.querySelector( '#setCredentialsToken', @@ -58,7 +57,7 @@ class EventDemo extends HTMLElement { const submitEventsBtn = this.shadowRoot.querySelector('#submitEvents'); const removeBtns = this.shadowRoot.querySelectorAll('[role="remove"]'); if ( - dispatchEventBtn && + sendEventBtn && inp && submitEventsBtn && setCredentialsTokenBtn && @@ -72,8 +71,8 @@ class EventDemo extends HTMLElement { console.error('credentials token input is empty'); } }); - dispatchEventBtn.addEventListener('click', () => { - dispatchEvent({ + sendEventBtn.addEventListener('click', () => { + sendEvent({ config: configFixture, credentialsProvider: credentialsProvider1, event: { diff --git a/packages/event-producer/test/fixtures/events.ts b/packages/event-producer/test/fixtures/events.ts index 56a8f433..4e48749f 100644 --- a/packages/event-producer/test/fixtures/events.ts +++ b/packages/event-producer/test/fixtures/events.ts @@ -1,6 +1,6 @@ -import type { DispatchedEvent, EPEvent } from '../../src'; +import type { EPEvent, SentEvent } from '../../src'; -export const eventPayload1: DispatchedEvent = { +export const eventPayload1: SentEvent = { consentCategory: 'NECESSARY', name: 'display_page', payload: {