@citec-spbu/common 1.1.2 → 1.1.4

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
1
  export declare const GRPC_CLIENT_PREFIX = "GRPC_CLIENT";
2
+ export declare const GRPC_MODULE_OPTIONS = "GRPC_MODULE_OPTIONS";
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GRPC_CLIENT_PREFIX = void 0;
3
+ exports.GRPC_MODULE_OPTIONS = exports.GRPC_CLIENT_PREFIX = void 0;
4
4
  exports.GRPC_CLIENT_PREFIX = "GRPC_CLIENT";
5
+ exports.GRPC_MODULE_OPTIONS = "GRPC_MODULE_OPTIONS";
@@ -1,5 +1,7 @@
1
1
  import { type DynamicModule } from "@nestjs/common";
2
- import { GRPC_CLIENTS } from "./registry/grpc.registry";
2
+ import { GrpcClientUrls, GrpcModuleAsyncOptions } from "./interfaces";
3
3
  export declare class GrpcModule {
4
- static register(clients: Record<keyof typeof GRPC_CLIENTS, string>): DynamicModule;
4
+ static register(urls: GrpcClientUrls): DynamicModule;
5
+ static registerAsync(options: GrpcModuleAsyncOptions): DynamicModule;
6
+ private static createClientProvider;
5
7
  }
@@ -37,7 +37,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
37
37
  exports.GrpcModule = void 0;
38
38
  const common_1 = require("@nestjs/common");
39
39
  const grpc_constants_1 = require("./constants/grpc.constants");
40
- const grpc_factory_1 = require("./factory/grpc-factory");
40
+ const grpc_factory_1 = require("./factory/grpc.factory");
41
41
  const grpc_registry_1 = require("./registry/grpc.registry");
42
42
  let GrpcModule = (() => {
43
43
  let _classDecorators = [(0, common_1.Module)({})];
@@ -53,17 +53,38 @@ let GrpcModule = (() => {
53
53
  if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
54
54
  __runInitializers(_classThis, _classExtraInitializers);
55
55
  }
56
- static register(clients) {
57
- const entries = Object.entries(clients);
56
+ static register(urls) {
57
+ const entries = Object.entries(urls);
58
58
  return {
59
59
  module: GrpcModule,
60
60
  providers: [
61
61
  grpc_factory_1.GrpcClientFactory,
62
- ...entries.map(([token, url]) => {
63
- const config = grpc_registry_1.GRPC_CLIENTS[token];
64
- return {
65
- provide: `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`,
66
- useFactory: (factory) => {
62
+ ...entries.map(([token, url]) => this.createClientProvider(token, url))
63
+ ],
64
+ exports: [
65
+ grpc_factory_1.GrpcClientFactory,
66
+ ...entries.map(([token]) => `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`)
67
+ ]
68
+ };
69
+ }
70
+ static registerAsync(options) {
71
+ return {
72
+ module: GrpcModule,
73
+ imports: options.imports ?? [],
74
+ providers: [
75
+ {
76
+ provide: grpc_constants_1.GRPC_MODULE_OPTIONS,
77
+ useFactory: options.useFactory,
78
+ inject: options.inject ?? []
79
+ },
80
+ {
81
+ provide: grpc_factory_1.GrpcClientFactory,
82
+ useFactory: (urls) => {
83
+ const factory = new grpc_factory_1.GrpcClientFactory();
84
+ for (const [token, url] of Object.entries(urls)) {
85
+ const config = grpc_registry_1.GRPC_CLIENTS[token];
86
+ if (!config)
87
+ throw new Error(`No config provided to token ${token}, update @citec-spbu/common package`);
67
88
  const client = factory.createClient({
68
89
  options: {
69
90
  package: config.package,
@@ -72,16 +93,31 @@ let GrpcModule = (() => {
72
93
  }
73
94
  });
74
95
  factory.register(token, client);
75
- return client;
76
- },
77
- inject: [grpc_factory_1.GrpcClientFactory]
78
- };
79
- })
96
+ return factory;
97
+ }
98
+ },
99
+ inject: [grpc_constants_1.GRPC_MODULE_OPTIONS]
100
+ }
80
101
  ],
81
- exports: [
82
- grpc_factory_1.GrpcClientFactory,
83
- ...entries.map(([token, _]) => `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`)
84
- ]
102
+ exports: [grpc_factory_1.GrpcClientFactory]
103
+ };
104
+ }
105
+ static createClientProvider(token, url) {
106
+ const config = grpc_registry_1.GRPC_CLIENTS[token];
107
+ return {
108
+ provide: `${grpc_constants_1.GRPC_CLIENT_PREFIX}_${token}`,
109
+ useFactory: (factory) => {
110
+ const client = factory.createClient({
111
+ options: {
112
+ package: config.package,
113
+ protoPath: config.protoPath,
114
+ url
115
+ }
116
+ });
117
+ factory.register(token, client);
118
+ return client;
119
+ },
120
+ inject: [grpc_factory_1.GrpcClientFactory]
85
121
  };
86
122
  }
87
123
  };
@@ -1,2 +1,3 @@
1
1
  export * from "./grpc.module";
2
2
  export * from "./decorators";
3
+ export * from "./interfaces";
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./grpc.module"), exports);
18
18
  __exportStar(require("./decorators"), exports);
19
+ __exportStar(require("./interfaces"), exports);
@@ -0,0 +1,7 @@
1
+ import { ModuleMetadata } from "@nestjs/common";
2
+ import { GRPC_CLIENTS } from "../registry/grpc.registry";
3
+ export type GrpcClientUrls = Partial<Record<keyof typeof GRPC_CLIENTS, string>>;
4
+ export interface GrpcModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
5
+ useFactory: (...args: any[]) => Promise<GrpcClientUrls> | GrpcClientUrls;
6
+ inject?: any[];
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export * from "./grpc-options.interface";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./grpc-options.interface"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@citec-spbu/common",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Core shared components for microservices",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",