fix(messaging): apply unknown tag when replying to event-like message
MODIFY
packages/messaging/src/effects/messaging.effects.helper.ts
packages/messaging/src/effects/messaging.effects.helper.ts
import { Event, EventMetadata } from '@marblejs/core';
import { isString, NamedError } from '@marblejs/core/dist/+internal/utils';
+export const UNKNOWN_TAG = '_UNKNOWN_';
+
export class MissingEventTypeError extends NamedError {
constructor() {
super('MissingEventTypeError', `#reply - Missing type literal`);
@@ -15,12 +17,18 @@ function isEventMetadata(metadata: any): metadata is EventMetadata {
return metadata.correlationId || metadata.replyTo;
}
+const composeMetadata = (originMetadata?: EventMetadata) => (customMetadata?: EventMetadata): EventMetadata => ({
+ raw: originMetadata?.raw ?? customMetadata?.raw,
+ correlationId: originMetadata?.correlationId ?? customMetadata?.correlationId,
+ replyTo: originMetadata?.replyTo ?? customMetadata?.replyTo ?? UNKNOWN_TAG,
+});
+
export const reply = (to: string | EventMetadata | Event) => <T extends Event>(event: T): T => {
assertEventType(event);
return isString(to)
- ? { ...event, metadata: { replyTo: to } }
+ ? { ...event, metadata: composeMetadata(event.metadata)({ replyTo: to }) }
: isEventMetadata(to)
- ? { ...event, metadata: to }
- : { ...to, ...event };
+ ? { ...event, metadata: composeMetadata(event.metadata)(to) }
+ : { ...to, ...event, metadata: composeMetadata(event.metadata)(to.metadata) };
};

MODIFY
packages/messaging/src/effects/specs/messaging.effects.helper.spec.ts
packages/messaging/src/effects/specs/messaging.effects.helper.spec.ts
import { Event, EventMetadata } from '@marblejs/core';
-import { reply, MissingEventTypeError } from '../messaging.effects.helper';
+import { reply, MissingEventTypeError, UNKNOWN_TAG } from '../messaging.effects.helper';
describe('#reply', () => {
test('creates success response based on originated event', () => {
@@ -85,6 +85,25 @@ describe('#reply', () => {
});
});
+ test(`creates response and applies ${UNKNOWN_TAG} to "replyTo" attribute if incoming event routing metadata is undefined`, () => {
+ // given
+ const origin: Event = {
+ type: 'TEST',
+ metadata: {},
+ };
+
+ // when
+ const response = reply(origin)({
+ type: 'TEST_RESPONSE',
+ });
+
+ // then
+ expect(response).toEqual({
+ type: 'TEST_RESPONSE',
+ metadata: { replyTo: '_UNKNOWN_' },
+ });
+ });
+
test('throws an error if event type is not provided in reply object', () => {
// given
const origin: Event = {

MODIFY
packages/messaging/src/index.spec.ts
packages/messaging/src/index.spec.ts
@@ -8,5 +8,6 @@ describe('@marblejs/messaging', () => {
expect(API.createMicroservice).toBeDefined();
expect(API.messagingClient).toBeDefined();
expect(API.messagingListener).toBeDefined();
+ expect(API.reply).toBeDefined();
});
});

MODIFY
packages/messaging/src/index.ts
packages/messaging/src/index.ts
@@ -10,7 +10,7 @@ export { LocalStrategyOptions, EVENT_BUS_CHANNEL } from './transport/strategies/
// effects
export * from './effects/messaging.effects.interface';
-export * from './effects/messaging.effects.helper';
+export { reply } from './effects/messaging.effects.helper';
// server
export * from './server/messaging.server';

fix(messaging): apply unknown tag when replying to event-like message