# @knawat/advanced-client-module

> This is a Client Proxy providing module.

Latest version **1.0.5** (published 2022-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @knawat/advanced-client-module
pnpm add @knawat/advanced-client-module
yarn add @knawat/advanced-client-module
bun add @knawat/advanced-client-module
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2022-08-24 |
| First published | 2022-07-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 31.5 KB |
| Known vulnerabilities | 0 (+3 in 3 direct dependencies) |
| Install scripts | no |
| Author | Mehrdad Mirsamie |
| Maintainers | hazemkhaled, abdelrahman3d, greetly |

## Links

- npm: https://www.npmjs.com/package/@knawat/advanced-client-module
- Repository: https://github.com/Knawat/advanced-nestjs-client-proxy
- Homepage: https://github.com/Knawat/advanced-nestjs-client-proxy#readme
- Issues: https://github.com/Knawat/advanced-nestjs-client-proxy/issues
- npm.io page: https://npm.io/package/@knawat/advanced-client-module

## Dependencies (6)

- [nats](https://npm.io/package/nats.md) ^2.7.1
- [rxjs](https://npm.io/package/rxjs.md) ^7.5.6
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^9.0.4
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^9.0.4
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@nestjs/microservices](https://npm.io/package/@nestjs/microservices.md) ^9.0.4

## Recent versions

- 1.0.5 (latest) — 2022-08-24
- 1.0.4 — 2022-07-27
- 1.0.3 — 2022-07-21
- 1.0.2 — 2022-07-20
- 1.0.1 — 2022-07-20

## README

# Advanced NestJS Client Proxy Module

## Sample usages

First import the package:

```typescript
import { AdvancedClientModule } from "@knawat/advanced-client-module";
```

### Registration:

```typescript
    AdvancedClientModule.register({
        transport: 'TCP',
        options: {
          url: 'http://localhost:8080',
        },
    }),
```

Replace all `MessagePattern` with `MessagePatternExtended` and `EventPattern` with `EventPatternExtended` decorators to add **NAMESPACE** in calls.

Now for replacing the new `Extended` decorators with valid replacement and because we can not alter **Reflect.metaData** over the Controller class from the npm package we have to do it manually.


First you need to create a `service` to process decorators like this:
```typescript
import { Injectable } from '@nestjs/common';
import { EventPattern, MessagePattern } from '@nestjs/microservices';
import {
  EVENT_PATTERN_EXTENDED,
  MESSAGE_PATTERN_EXTENDED,
} from '@knawat/advanced-client-module';

@Injectable()
export class AdvancedClientDecoratorProcessorService {
  processCustomDecorators(namespace: string, types: any[]) {
    for (const type of types) {
      const propNames = Object.getOwnPropertyNames(type.prototype);
      for (const prop of propNames) {
        const propValue = Reflect.getMetadata(
          MESSAGE_PATTERN_EXTENDED,
          Reflect.get(type.prototype, prop),
        );
        if (propValue) {
          propValue.cmd = `${namespace}-${propValue.cmd}`;
          propValue.role = `${namespace}-${propValue.role}`;
          Reflect.decorate(
            [MessagePattern(propValue)],
            type.prototype,
            prop,
            Reflect.getOwnPropertyDescriptor(type.prototype, prop),
          );
        }
        let eventValue = Reflect.getMetadata(
          EVENT_PATTERN_EXTENDED,
          Reflect.get(type.prototype, prop),
        );
        if (eventValue) {
          eventValue = `${namespace}-${eventValue}`;
          Reflect.decorate(
            [EventPattern(eventValue)],
            type.prototype,
            prop,
            Reflect.getOwnPropertyDescriptor(type.prototype, prop),
          );
        }
      }
    }
  }
}
```

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,
  ) {}
```

---
_Source: https://npm.io/package/@knawat/advanced-client-module · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
