Skip to content

Commit 43b85aa

Browse files
committed
feature(microservices): add logic to transform patterns to routes
1 parent df9f6ec commit 43b85aa

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

packages/microservices/client/client-proxy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
WritePacket,
1919
} from '../interfaces';
2020

21+
import * as Utils from '../utils';
22+
2123
export abstract class ClientProxy {
2224
public abstract connect(): Promise<any>;
2325
public abstract close(): any;
@@ -101,6 +103,6 @@ export abstract class ClientProxy {
101103
}
102104

103105
protected normalizePattern<T = any>(pattern: T): string {
104-
return (isString(pattern) ? pattern : JSON.stringify(pattern)) as string;
106+
return Utils.MsvcUtil.transformPatternToRoute(pattern);
105107
}
106108
}

packages/microservices/server/server.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
} from '../interfaces';
1919
import { NO_EVENT_HANDLER } from './../constants';
2020

21+
import * as Utils from '../utils';
22+
2123
export abstract class Server {
2224
protected readonly messageHandlers = new Map<string, MessageHandler>();
2325
protected readonly logger = new Logger(Server.name);
@@ -28,17 +30,19 @@ export abstract class Server {
2830
isEventHandler = false,
2931
) {
3032
const key = isString(pattern) ? pattern : JSON.stringify(pattern);
33+
const route = this.getRouteFromPattern(key);
3134
callback.isEventHandler = isEventHandler;
32-
this.messageHandlers.set(key, callback);
35+
this.messageHandlers.set(route, callback);
3336
}
3437

3538
public getHandlers(): Map<string, MessageHandler> {
3639
return this.messageHandlers;
3740
}
3841

3942
public getHandlerByPattern(pattern: string): MessageHandler | null {
40-
return this.messageHandlers.has(pattern)
41-
? this.messageHandlers.get(pattern)
43+
const route = this.getRouteFromPattern(pattern);
44+
return this.messageHandlers.has(route)
45+
? this.messageHandlers.get(route)
4246
: null;
4347
}
4448

@@ -99,4 +103,25 @@ export abstract class Server {
99103
private isObservable(input: unknown): input is Observable<any> {
100104
return input && isFunction((input as Observable<any>).subscribe);
101105
}
106+
107+
/**
108+
* Transforms the server Pattern to valid type and returns a route for him.
109+
*
110+
* @param {string} pattern - server pattern
111+
* @returns string
112+
*/
113+
private getRouteFromPattern(pattern: string): string {
114+
let validPattern: any;
115+
116+
try {
117+
// Gets the pattern in JSON format
118+
validPattern = JSON.parse(pattern);
119+
} catch (error) {
120+
// Uses a fundamental object (`pattern` variable without any conversion)
121+
validPattern = pattern;
122+
}
123+
124+
// Transform the Pattern to Route
125+
return Utils.MsvcUtil.transformPatternToRoute(validPattern);
126+
}
102127
}

0 commit comments

Comments
 (0)