@knawat/advanced-client-module
Advanced tools
@@ -0,1 +1,2 @@ | ||
import { ModuleMetadata, Type } from "@nestjs/common"; | ||
export interface AdvancedClientModuleOptions { | ||
@@ -6,2 +7,11 @@ transport: string; | ||
} | ||
export interface AdvancedClientModuleFactory { | ||
createAdvancedClientModuleOptions: () => Promise<AdvancedClientModuleOptions> | AdvancedClientModuleOptions; | ||
} | ||
export interface AdvancedClientModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> { | ||
inject?: any[]; | ||
useClass?: Type<AdvancedClientModuleFactory>; | ||
useExisting?: Type<AdvancedClientModuleFactory>; | ||
useFactory?: (...args: any[]) => Promise<AdvancedClientModuleOptions> | AdvancedClientModuleOptions; | ||
} | ||
//# sourceMappingURL=advanced-client-module-options.interface.d.ts.map |
@@ -1,8 +0,6 @@ | ||
import { ConfigurableModuleAsyncOptions, DynamicModule } from "@nestjs/common"; | ||
import { DynamicModule } from "@nestjs/common"; | ||
import { AdvancedClientModuleOptions } from "./advanced-client-module-options.interface"; | ||
import { ConfigurableModuleClass } from "./advanced-client.module-definition"; | ||
export declare class AdvancedClientModule extends ConfigurableModuleClass { | ||
export declare class AdvancedClientModule { | ||
static register(options: AdvancedClientModuleOptions): DynamicModule; | ||
static registerAsync(options: ConfigurableModuleAsyncOptions<AdvancedClientModuleOptions, "create">): DynamicModule; | ||
} | ||
//# sourceMappingURL=advanced-client.module.d.ts.map |
@@ -13,6 +13,5 @@ "use strict"; | ||
const microservices_1 = require("@nestjs/microservices"); | ||
const advanced_client_module_definition_1 = require("./advanced-client.module-definition"); | ||
const advanced_client_service_1 = require("./advanced-client.service"); | ||
const constants_1 = require("./constants"); | ||
let AdvancedClientModule = AdvancedClientModule_1 = class AdvancedClientModule extends advanced_client_module_definition_1.ConfigurableModuleClass { | ||
let AdvancedClientModule = AdvancedClientModule_1 = class AdvancedClientModule { | ||
static register(options) { | ||
@@ -36,3 +35,3 @@ return { | ||
const configValue = options.namespace; | ||
const namespace = configValue ? `${configValue}-` : ''; | ||
const namespace = configValue ? `${configValue}-` : ""; | ||
if (topic.cmd) { | ||
@@ -44,3 +43,3 @@ topic.cmd = `${namespace}${topic.cmd}`; | ||
} | ||
if (typeof topic === 'string') { | ||
if (typeof topic === "string") { | ||
topic = `${namespace}${topic}`; | ||
@@ -58,8 +57,2 @@ } | ||
} | ||
static registerAsync(options) { | ||
const res = Object.assign({}, super.registerAsync(options)); | ||
res.providers[0].provide = constants_1.SERVICE_CLIENT; | ||
res.exports = [constants_1.SERVICE_CLIENT]; | ||
return res; | ||
} | ||
}; | ||
@@ -66,0 +59,0 @@ AdvancedClientModule = AdvancedClientModule_1 = __decorate([ |
@@ -5,3 +5,4 @@ import { AdvancedClientModule } from "./advanced-client/advanced-client.module"; | ||
import { EventPatternExtended } from "./advanced-client/decorators/event-pattern-extended"; | ||
export { AdvancedClientModule, AdvancedClientDecoratorProcessorService, MessagePatternExtended, EventPatternExtended }; | ||
import { SERVICE_CLIENT } from "./advanced-client/constants"; | ||
export { AdvancedClientModule, AdvancedClientDecoratorProcessorService, MessagePatternExtended, EventPatternExtended, SERVICE_CLIENT }; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EventPatternExtended = exports.MessagePatternExtended = exports.AdvancedClientDecoratorProcessorService = exports.AdvancedClientModule = void 0; | ||
exports.SERVICE_CLIENT = exports.EventPatternExtended = exports.MessagePatternExtended = exports.AdvancedClientDecoratorProcessorService = exports.AdvancedClientModule = void 0; | ||
const advanced_client_module_1 = require("./advanced-client/advanced-client.module"); | ||
@@ -12,2 +12,4 @@ Object.defineProperty(exports, "AdvancedClientModule", { enumerable: true, get: function () { return advanced_client_module_1.AdvancedClientModule; } }); | ||
Object.defineProperty(exports, "EventPatternExtended", { enumerable: true, get: function () { return event_pattern_extended_1.EventPatternExtended; } }); | ||
const constants_1 = require("./advanced-client/constants"); | ||
Object.defineProperty(exports, "SERVICE_CLIENT", { enumerable: true, get: function () { return constants_1.SERVICE_CLIENT; } }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@knawat/advanced-client-module", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "This is a Client Proxy providing module.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -6,28 +6,9 @@ # Advanced NestJS Client Proxy Module | ||
First import the package: | ||
```typescript | ||
import { AdvancedClientModule } from '@knawat/advanced-client-module'; | ||
``` | ||
### Async usage: | ||
```typescript | ||
AdvancedClientModule.registerAsync({ | ||
imports: [ConfigModule], | ||
useFactory: (config: ConfigService) => { | ||
return { | ||
transport: | ||
config.get<string>('MC_TRANSPORTER_TYPE') || | ||
config.get<string>('TRANSPORTER_TYPE') || | ||
'TCP', | ||
options: config.get<string>('MC_TRANSPORTER') | ||
? JSON.parse(config.get<string>('MC_TRANSPORTER')) | ||
: { | ||
url: config.get<string>('TRANSPORTER'), | ||
}, | ||
}; | ||
}, | ||
inject: [ConfigService], | ||
}), | ||
import { AdvancedClientModule } from "@knawat/advanced-client-module"; | ||
``` | ||
### Sync usage: | ||
### Registration: | ||
```typescript | ||
@@ -40,2 +21,20 @@ AdvancedClientModule.register({ | ||
}), | ||
``` | ||
Replace all `MessagePattern` with `MessagePatternExtended` and `EventPattern` with `EventPatternExtended` decorators to add **NAMESPACE** in calls. | ||
Now add the processor to the `main.ts` as below: | ||
````typescript | ||
app | ||
.get(AdvancedClientDecoratorProcessorService) | ||
.processCustomDecorators(process.env.NAMESPACE, [...CONTROLLERS-WITH-EXTENDED-DECORATORS]); | ||
```` | ||
In order to use microservice client you need to inject the `SERVICE_CLIENT` class: | ||
```typescript | ||
constructor( | ||
@Inject(SERVICE_CLIENT) | ||
private readonly client: ClientProxy, | ||
) {} | ||
``` |
@@ -0,1 +1,3 @@ | ||
import { ModuleMetadata, Type } from "@nestjs/common"; | ||
export interface AdvancedClientModuleOptions { | ||
@@ -5,2 +7,18 @@ transport: string; | ||
namespace?: string; | ||
} | ||
export interface AdvancedClientModuleFactory { | ||
createAdvancedClientModuleOptions: () => | ||
| Promise<AdvancedClientModuleOptions> | ||
| AdvancedClientModuleOptions; | ||
} | ||
export interface AdvancedClientModuleAsyncOptions | ||
extends Pick<ModuleMetadata, "imports"> { | ||
inject?: any[]; | ||
useClass?: Type<AdvancedClientModuleFactory>; | ||
useExisting?: Type<AdvancedClientModuleFactory>; | ||
useFactory?: ( | ||
...args: any[] | ||
) => Promise<AdvancedClientModuleOptions> | AdvancedClientModuleOptions; | ||
} |
@@ -5,5 +5,12 @@ import { | ||
Module, | ||
Provider, | ||
Type, | ||
} from "@nestjs/common"; | ||
import { ClientProxyFactory, Transport } from "@nestjs/microservices"; | ||
import { AdvancedClientModuleOptions } from "./advanced-client-module-options.interface"; | ||
import { setFlagsFromString } from "v8"; | ||
import { | ||
AdvancedClientModuleAsyncOptions, | ||
AdvancedClientModuleFactory, | ||
AdvancedClientModuleOptions, | ||
} from "./advanced-client-module-options.interface"; | ||
import { ConfigurableModuleClass } from "./advanced-client.module-definition"; | ||
@@ -14,3 +21,3 @@ import { AdvancedClientDecoratorProcessorService } from "./advanced-client.service"; | ||
@Module({}) | ||
export class AdvancedClientModule extends ConfigurableModuleClass { | ||
export class AdvancedClientModule { | ||
static register(options: AdvancedClientModuleOptions): DynamicModule { | ||
@@ -36,3 +43,3 @@ return { | ||
const configValue = options.namespace; | ||
const namespace = configValue ? `${configValue}-` : ''; | ||
const namespace = configValue ? `${configValue}-` : ""; | ||
if (topic.cmd) { | ||
@@ -44,3 +51,3 @@ topic.cmd = `${namespace}${topic.cmd}`; | ||
} | ||
if (typeof topic === 'string') { | ||
if (typeof topic === "string") { | ||
topic = `${namespace}${topic}`; | ||
@@ -58,16 +65,2 @@ } | ||
} | ||
static registerAsync( | ||
options: ConfigurableModuleAsyncOptions< | ||
AdvancedClientModuleOptions, | ||
"create" | ||
> | ||
): DynamicModule { | ||
const res = { | ||
...super.registerAsync(options), | ||
}; | ||
(res.providers[0] as unknown as any).provide = SERVICE_CLIENT; | ||
res.exports = [SERVICE_CLIENT]; | ||
return res; | ||
} | ||
} |
@@ -5,3 +5,4 @@ import { AdvancedClientModule } from "./advanced-client/advanced-client.module"; | ||
import { EventPatternExtended } from "./advanced-client/decorators/event-pattern-extended"; | ||
import { SERVICE_CLIENT } from "./advanced-client/constants"; | ||
export { AdvancedClientModule, AdvancedClientDecoratorProcessorService, MessagePatternExtended, EventPatternExtended }; | ||
export { AdvancedClientModule, AdvancedClientDecoratorProcessorService, MessagePatternExtended, EventPatternExtended, SERVICE_CLIENT }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
34610
1.94%357
4.08%38
-2.56%