@citec-spbu/common 1.1.4 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,2 @@
1
- export declare const InjectGrpcClient: (token: string) => PropertyDecorator & ParameterDecorator;
1
+ import { GRPC_CLIENTS } from "../registry/grpc.registry";
2
+ export declare const InjectGrpcClient: (token: keyof typeof GRPC_CLIENTS) => PropertyDecorator & ParameterDecorator;
@@ -0,0 +1,13 @@
1
+ import type { OnModuleInit } from "@nestjs/common";
2
+ import { ClientGrpc } from "@nestjs/microservices";
3
+ import { type Observable } from "rxjs";
4
+ type UnwrapObservable<U> = U extends Observable<infer R> ? R : U;
5
+ export declare abstract class AbstractGrpcClient<T extends Record<string, any>> implements OnModuleInit {
6
+ private readonly client;
7
+ private readonly serviceName;
8
+ protected service: T;
9
+ protected constructor(client: ClientGrpc, serviceName: string);
10
+ onModuleInit(): void;
11
+ call<K extends keyof T>(method: K, payload: Parameters<T[K]>[0]): Promise<UnwrapObservable<ReturnType<T[K]>>>;
12
+ }
13
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractGrpcClient = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ class AbstractGrpcClient {
6
+ client;
7
+ serviceName;
8
+ service;
9
+ constructor(client, serviceName) {
10
+ this.client = client;
11
+ this.serviceName = serviceName;
12
+ }
13
+ onModuleInit() {
14
+ this.service = this.client.getService(this.serviceName);
15
+ }
16
+ async call(method, payload) {
17
+ try {
18
+ const observable = this.service[method](payload);
19
+ const result = await (0, rxjs_1.lastValueFrom)(observable);
20
+ return result;
21
+ }
22
+ catch (error) {
23
+ throw error;
24
+ }
25
+ }
26
+ }
27
+ exports.AbstractGrpcClient = AbstractGrpcClient;
@@ -97,9 +97,17 @@ let GrpcModule = (() => {
97
97
  }
98
98
  },
99
99
  inject: [grpc_constants_1.GRPC_MODULE_OPTIONS]
100
- }
100
+ },
101
+ ...options.clients.map(token => ({
102
+ provide: `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`,
103
+ useFactory: (factory) => factory.getClient(token),
104
+ inject: [grpc_factory_1.GrpcClientFactory]
105
+ }))
101
106
  ],
102
- exports: [grpc_factory_1.GrpcClientFactory]
107
+ exports: [
108
+ grpc_factory_1.GrpcClientFactory,
109
+ ...options.clients.map(token => `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`)
110
+ ]
103
111
  };
104
112
  }
105
113
  static createClientProvider(token, url) {
@@ -1,3 +1,4 @@
1
1
  export * from "./grpc.module";
2
2
  export * from "./decorators";
3
3
  export * from "./interfaces";
4
+ export * from "./grpc.abstract-client";
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./grpc.module"), exports);
18
18
  __exportStar(require("./decorators"), exports);
19
19
  __exportStar(require("./interfaces"), exports);
20
+ __exportStar(require("./grpc.abstract-client"), exports);
@@ -2,6 +2,7 @@ import { ModuleMetadata } from "@nestjs/common";
2
2
  import { GRPC_CLIENTS } from "../registry/grpc.registry";
3
3
  export type GrpcClientUrls = Partial<Record<keyof typeof GRPC_CLIENTS, string>>;
4
4
  export interface GrpcModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
5
+ clients: Array<keyof typeof GRPC_CLIENTS>;
5
6
  useFactory: (...args: any[]) => Promise<GrpcClientUrls> | GrpcClientUrls;
6
7
  inject?: any[];
7
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@citec-spbu/common",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Core shared components for microservices",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,6 +23,7 @@
23
23
  "dependencies": {
24
24
  "@citec-spbu/contracts": "^1.0.9",
25
25
  "@nestjs/common": "^11.1.12",
26
- "@nestjs/microservices": "^11.1.12"
26
+ "@nestjs/microservices": "^11.1.12",
27
+ "rxjs": "^7.8.2"
27
28
  }
28
29
  }