mojaloop vnext platform shared libraries - generic messaging types lib




Generic Messaging Types Library for the vNext Mojaloop platform.
These types are meant to be stable, thus can be used in domain code.
Note: for domain usage, actual implementations should be dependency-injected from outside the domain.
``Typescript
export enum MessageTypes{
'STATE_EVENT' =0, // for private event-sourcing events
'STATE_SNAPSHOT', // for private event-sourcing snapshot events
'DOMAIN_EVENT', // public domain events
'COMMAND', // for internal/private BC commands
}
export interface IMessage{
msgType: MessageTypes;
msgName: string; // name of the event or command
msgId: string; // unique per message
msgTimestamp: number;
msgTopic: string;
msgKey: string | null; // usually the id of the aggregate (used for partitioning)
msgPartition: number | null;
msgOffset: number | null;
payload: any; // this the actual message payload, specific to each type of msg
}
`
`Typescript
export interface IDomainMessage extends IMessage{
aggregateName: string // name of the source/target aggregate (source if event, target if command)
aggregateId: string // id of the source/target aggregate (source if event, target if command)
}
export abstract class DomainMsg implements IDomainMessage {
msgId: string = Crypto.randomUUID();
msgTimestamp: number = Date.now();
msgName: string = (this as any).constructor.name;
msgPartition: number | null = null;
msgOffset: number | null;
abstract msgType: MessageTypes;
abstract msgKey: string;
abstract msgTopic: string;
abstract aggregateId: string;
abstract aggregateName: string;
abstract payload: any
}
`
These are usually public, any code inside the platform can listen to these
`Typescript`
export abstract class DomainEventMsg extends DomainMsg {
msgType: MessageTypes = MessageTypes.DOMAIN_EVENT;
}
These are usually private to the publishing BCs/applications - no other apps should be allowed to listen them (exception is in CQRS pattern where an event handler of the same BC is creating a query data store from these events)
`Typescript`
export abstract class StateEventMsg extends DomainMsg {
msgType: MessageTypes = MessageTypes.STATE_EVENT;
}
`Typescript`
export abstract class CommandMsg extends DomainMsg {
msgType: MessageTypes = MessageTypes.COMMAND;
}
These interfaces provide an abstraction layer over specific infrastructure implementation, facilitating mocks and a future change from Kafka (if beneficial/necessary).
They are stable and can be used in Domain Code, provided the domain code only depends on this lib and not actual implementation libs.
All implementation of message consumers and producer intended for general use must implement these interfaces.
In this repo we provide the implementation for Kafka here - Its npm module
Typescript
export interface IMessageConsumer {
setCallbackFn: (handlerCallback: (message: IMessage) => Promise) => void
setFilteringFn: (filterFn: (message: IMessage) => boolean) => void
setTopics: (topics: string[]) => void
destroy: (force: boolean) => Promise
connect: () => Promise
disconnect: (force: boolean) => Promise
start: () => Promise
stop: () => Promise
}
`$3
`Typescript
export declare interface IMessageProducer {
destroy: () => Promise connect: () => Promise
disconnect: () => Promise
send: (message: IMessage | IMessage[]) => Promise
}
``