Events Factory is a versatile tool designed to streamline the generation of structured event objects within your JavaScript applications. This package offers a simple yet powerful interface for creating events with customizable properties and defaults.
You can install the Events Factory package via npm:
npm install events-factory
import EventsFactory from '@wizzer-tech/events-factory';
To initialize the Events Factory, simply create an instance of the class, providing the necessary parameters:
const eventsFactory = new EventsFactory('my-service', {
app: 'my-app',
version: '1.0.0',
eventIdPrefix: 'evt'
});
Once initialized, you can use the generate method to create structured event objects:
const event = eventsFactory.generate('user.created', { userId: '12345' });
You can customize the default properties set during initialization or override them when generating events:
eventsFactory.setVersion('2.0.0');
const customEvent = eventsFactory.generate('user.signout', { userId: '12345' }, { app: 'another-app', version: '1.5.0' });
new EventsFactory(service: string = "", props: IEventsFactoryProps)
- service: The service generating the event.
- props: Additional properties for customizing the factory.
-
setVersion(version: string)
Sets the default version for events created by this factory. -
generate(type: string, data: Record<string, any>, props: IEventProperties)
Generates a structured event object.