@rsdk/grpc.clients 2.6.0-next.0 → 2.6.0-next.10

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.
Files changed (30) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/decorators/inject-client-factory.decorator.js +7 -3
  3. package/dist/decorators/inject-client-factory.decorator.js.map +1 -1
  4. package/dist/decorators/inject-client.decorator.js +1 -1
  5. package/dist/decorators/inject-client.decorator.js.map +1 -1
  6. package/dist/metadata/constants.d.ts +1 -0
  7. package/dist/metadata/constants.js +2 -1
  8. package/dist/metadata/constants.js.map +1 -1
  9. package/dist/metadata/grpc-clients-factory.autodoc-resolver.d.ts +8 -0
  10. package/dist/metadata/grpc-clients-factory.autodoc-resolver.js +19 -0
  11. package/dist/metadata/grpc-clients-factory.autodoc-resolver.js.map +1 -0
  12. package/dist/middleware/exception-mapper.middleware.d.ts +5 -0
  13. package/dist/middleware/exception-mapper.middleware.js +64 -0
  14. package/dist/middleware/exception-mapper.middleware.js.map +1 -0
  15. package/dist/module.generator.d.ts +2 -1
  16. package/dist/module.generator.js +28 -1
  17. package/dist/module.generator.js.map +1 -1
  18. package/dist/repository/grpc-client.repository.d.ts +2 -7
  19. package/dist/repository/grpc-client.repository.js +8 -17
  20. package/dist/repository/grpc-client.repository.js.map +1 -1
  21. package/dist/servers/types.d.ts +3 -0
  22. package/package.json +8 -8
  23. package/src/decorators/inject-client-factory.decorator.ts +13 -4
  24. package/src/decorators/inject-client.decorator.ts +1 -1
  25. package/src/metadata/constants.ts +1 -0
  26. package/src/metadata/grpc-clients-factory.autodoc-resolver.ts +31 -0
  27. package/src/middleware/exception-mapper.middleware.ts +73 -0
  28. package/src/module.generator.ts +61 -4
  29. package/src/repository/grpc-client.repository.ts +13 -29
  30. package/src/servers/types.ts +4 -0
package/CHANGELOG.md CHANGED
@@ -3,6 +3,44 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.6.0-next.10](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.9...v2.6.0-next.10) (2023-07-25)
7
+
8
+ **Note:** Version bump only for package @rsdk/grpc.clients
9
+
10
+ # [2.6.0-next.9](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.8...v2.6.0-next.9) (2023-07-25)
11
+
12
+ **Note:** Version bump only for package @rsdk/grpc.clients
13
+
14
+ # [2.6.0-next.8](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.7...v2.6.0-next.8) (2023-07-23)
15
+
16
+ **Note:** Version bump only for package @rsdk/grpc.clients
17
+
18
+ # [2.6.0-next.7](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.6...v2.6.0-next.7) (2023-07-23)
19
+
20
+ **Note:** Version bump only for package @rsdk/grpc.clients
21
+
22
+ # [2.6.0-next.6](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.5...v2.6.0-next.6) (2023-07-22)
23
+
24
+ **Note:** Version bump only for package @rsdk/grpc.clients
25
+
26
+ # [2.6.0-next.5](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.4...v2.6.0-next.5) (2023-07-22)
27
+
28
+ ### Bug Fixes
29
+
30
+ * remove unused properties ([ccbeba0](https://github.com/R-Vision/rsdk/commit/ccbeba0751bc35a47101723661bc94688c35ed57))
31
+
32
+ # [2.6.0-next.3](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.2...v2.6.0-next.3) (2023-07-07)
33
+
34
+ ### Features
35
+
36
+ * **grpc-clients:** added support grpc client factory ([#78](https://github.com/R-Vision/rsdk/issues/78)) ([4f9d761](https://github.com/R-Vision/rsdk/commit/4f9d761516abfa0264ee4c707fdd784f7ad9fd0c))
37
+
38
+ # [2.6.0-next.1](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.0...v2.6.0-next.1) (2023-07-04)
39
+
40
+ ### Bug Fixes
41
+
42
+ * gRPC error mapping into rsdk exceptions ([8c6a2b8](https://github.com/R-Vision/rsdk/commit/8c6a2b8276a7a309d26bc21e4c9ae36d7ee4210c))
43
+
6
44
  # [2.6.0-next.0](https://github.com/R-Vision/rsdk/compare/v2.5.0...v2.6.0-next.0) (2023-07-03)
7
45
 
8
46
  ### Bug Fixes
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InjectGrpcClientsFactory = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
+ const constants_1 = require("@rsdk/core/dist/rsdk-metadata/constants");
6
+ const metadata_1 = require("@rsdk/metadata");
7
+ const module_generator_1 = require("../module.generator");
5
8
  const token_factories_1 = require("../token.factories");
6
9
  /**
7
10
  * Injects factory of grpc clients for specific grpc-service. Main
@@ -11,9 +14,10 @@ const token_factories_1 = require("../token.factories");
11
14
  * @returns ParameterDecorator
12
15
  */
13
16
  const InjectGrpcClientsFactory = (definition) => {
14
- const token = (0, token_factories_1.getFactoryToken)(definition);
15
- // TODO Add metadata registration
16
- return (0, common_1.Inject)(token);
17
+ return (target, propertyKey, parameterIndex) => {
18
+ (0, common_1.Inject)((0, token_factories_1.getFactoryToken)(definition))(target, propertyKey, parameterIndex);
19
+ metadata_1.RsdkMetadata.setWithScope(target, constants_1.PLATFORM_RAW_GLOBAL_METADATA_SCOPE, definition.fullName, module_generator_1.ModuleGenerator.createClientFactoryModuleDefinition(definition), true);
20
+ };
17
21
  };
18
22
  exports.InjectGrpcClientsFactory = InjectGrpcClientsFactory;
19
23
  //# sourceMappingURL=inject-client-factory.decorator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"inject-client-factory.decorator.js","sourceRoot":"","sources":["../../src/decorators/inject-client-factory.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAGxC,wDAAqD;AAWrD;;;;;;GAMG;AACI,MAAM,wBAAwB,GAAG,CACtC,UAAmC,EACf,EAAE;IACtB,MAAM,KAAK,GAAG,IAAA,iCAAe,EAAC,UAAU,CAAC,CAAC;IAE1C,iCAAiC;IACjC,OAAO,IAAA,eAAM,EAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC;AAPW,QAAA,wBAAwB,4BAOnC"}
1
+ {"version":3,"file":"inject-client-factory.decorator.js","sourceRoot":"","sources":["../../src/decorators/inject-client-factory.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AACxC,uEAA6F;AAC7F,6CAA8C;AAG9C,0DAAsD;AACtD,wDAAqD;AAWrD;;;;;;GAMG;AACI,MAAM,wBAAwB,GAAG,CACtC,UAAmC,EACf,EAAE;IACtB,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,cAAc,EAAE,EAAE;QAC7C,IAAA,eAAM,EAAC,IAAA,iCAAe,EAAC,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;QACzE,uBAAY,CAAC,YAAY,CACvB,MAAM,EACN,8CAAkC,EAClC,UAAU,CAAC,QAAQ,EACnB,kCAAe,CAAC,mCAAmC,CAAC,UAAU,CAAC,EAC/D,IAAI,CACL,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AAbW,QAAA,wBAAwB,4BAanC"}
@@ -17,7 +17,7 @@ const token_factories_1 = require("../token.factories");
17
17
  const InjectGrpcClient = (grpcServerDeclarationValue) => {
18
18
  return (target, propertyKey, parameterIndex) => {
19
19
  (0, common_1.Inject)((0, token_factories_1.getClientToken)(grpcServerDeclarationValue.definition))(target, propertyKey, parameterIndex);
20
- metadata_1.RsdkMetadata.setWithScope(target, constants_1.PLATFORM_RAW_GLOBAL_METADATA_SCOPE, `${grpcServerDeclarationValue.serverName}.${grpcServerDeclarationValue.serviceName}`, module_generator_1.ModuleGenerator.createModuleDefinition(grpcServerDeclarationValue.serverName, grpcServerDeclarationValue.definition), true);
20
+ metadata_1.RsdkMetadata.setWithScope(target, constants_1.PLATFORM_RAW_GLOBAL_METADATA_SCOPE, `${grpcServerDeclarationValue.serverName}.${grpcServerDeclarationValue.serviceName}`, module_generator_1.ModuleGenerator.createClientModuleDefinition(grpcServerDeclarationValue.serverName, grpcServerDeclarationValue.definition), true);
21
21
  };
22
22
  };
23
23
  exports.InjectGrpcClient = InjectGrpcClient;
@@ -1 +1 @@
1
- {"version":3,"file":"inject-client.decorator.js","sourceRoot":"","sources":["../../src/decorators/inject-client.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AACxC,uEAA6F;AAC7F,6CAA8C;AAE9C,0DAAsD;AAEtD,wDAAoD;AAEpD;;;;;;;GAOG;AACI,MAAM,gBAAgB,GAAG,CAI9B,0BAGC,EACmB,EAAE;IACtB,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,cAAc,EAAE,EAAE;QAC7C,IAAA,eAAM,EAAC,IAAA,gCAAc,EAAC,0BAA0B,CAAC,UAAU,CAAC,CAAC,CAC3D,MAAM,EACN,WAAW,EACX,cAAc,CACf,CAAC;QACF,uBAAY,CAAC,YAAY,CACvB,MAAM,EACN,8CAAkC,EAClC,GAAG,0BAA0B,CAAC,UAAU,IAAI,0BAA0B,CAAC,WAAW,EAAE,EACpF,kCAAe,CAAC,sBAAsB,CACpC,0BAA0B,CAAC,UAAU,EACrC,0BAA0B,CAAC,UAAU,CACtC,EACD,IAAI,CACL,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AA1BW,QAAA,gBAAgB,oBA0B3B"}
1
+ {"version":3,"file":"inject-client.decorator.js","sourceRoot":"","sources":["../../src/decorators/inject-client.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AACxC,uEAA6F;AAC7F,6CAA8C;AAE9C,0DAAsD;AAEtD,wDAAoD;AAEpD;;;;;;;GAOG;AACI,MAAM,gBAAgB,GAAG,CAI9B,0BAGC,EACmB,EAAE;IACtB,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,cAAc,EAAE,EAAE;QAC7C,IAAA,eAAM,EAAC,IAAA,gCAAc,EAAC,0BAA0B,CAAC,UAAU,CAAC,CAAC,CAC3D,MAAM,EACN,WAAW,EACX,cAAc,CACf,CAAC;QACF,uBAAY,CAAC,YAAY,CACvB,MAAM,EACN,8CAAkC,EAClC,GAAG,0BAA0B,CAAC,UAAU,IAAI,0BAA0B,CAAC,WAAW,EAAE,EACpF,kCAAe,CAAC,4BAA4B,CAC1C,0BAA0B,CAAC,UAAU,EACrC,0BAA0B,CAAC,UAAU,CACtC,EACD,IAAI,CACL,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AA1BW,QAAA,gBAAgB,oBA0B3B"}
@@ -1 +1,2 @@
1
1
  export declare const GRPC_CLIENTS_RSDK_METADATA_SCOPE = "grpc-clients";
2
+ export declare const GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE = "grpc-clients-factory";
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GRPC_CLIENTS_RSDK_METADATA_SCOPE = void 0;
3
+ exports.GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE = exports.GRPC_CLIENTS_RSDK_METADATA_SCOPE = void 0;
4
4
  exports.GRPC_CLIENTS_RSDK_METADATA_SCOPE = 'grpc-clients';
5
+ exports.GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE = 'grpc-clients-factory';
5
6
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/metadata/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,gCAAgC,GAAG,cAAc,CAAC"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/metadata/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,gCAAgC,GAAG,cAAc,CAAC;AAClD,QAAA,wCAAwC,GAAG,sBAAsB,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { DocumentNode, DocumentResolver } from '@rsdk/autodoc.protocol';
2
+ import type { RsdkMetadataProvider } from '@rsdk/metadata';
3
+ import type { Promisable } from 'type-fest';
4
+ declare class GrpcClientsFactoryAutodocResolver implements DocumentResolver {
5
+ getNode(rsdkMetadata: RsdkMetadataProvider): Promisable<DocumentNode | null>;
6
+ }
7
+ export declare const grpcClientsFactoryAutodocResolver: GrpcClientsFactoryAutodocResolver;
8
+ export {};
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.grpcClientsFactoryAutodocResolver = void 0;
4
+ const autodoc_protocol_1 = require("@rsdk/autodoc.protocol");
5
+ const constants_1 = require("./constants");
6
+ class GrpcClientsFactoryAutodocResolver {
7
+ getNode(rsdkMetadata) {
8
+ const resources = rsdkMetadata.get(constants_1.GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE);
9
+ if (!resources) {
10
+ return null;
11
+ }
12
+ return new autodoc_protocol_1.Composite(new autodoc_protocol_1.Table(resources.map(({ value: { definition } }) => ({
13
+ name: definition.name.toString(),
14
+ fullName: definition.fullName.toString(),
15
+ }))), 'Grpc clients factory');
16
+ }
17
+ }
18
+ exports.grpcClientsFactoryAutodocResolver = new GrpcClientsFactoryAutodocResolver();
19
+ //# sourceMappingURL=grpc-clients-factory.autodoc-resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grpc-clients-factory.autodoc-resolver.js","sourceRoot":"","sources":["../../src/metadata/grpc-clients-factory.autodoc-resolver.ts"],"names":[],"mappings":";;;AACA,6DAA0D;AAM1D,2CAAuE;AAEvE,MAAM,iCAAiC;IACrC,OAAO,CAAC,YAAkC;QACxC,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,CAChC,oDAAwC,CACzC,CAAC;QACF,IAAI,CAAC,SAAS,EAAE;YACd,OAAO,IAAI,CAAC;SACb;QACD,OAAO,IAAI,4BAAS,CAClB,IAAI,wBAAK,CACP,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;YAC5C,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,QAAQ,EAAE;YAChC,QAAQ,EAAE,UAAU,CAAC,QAAQ,CAAC,QAAQ,EAAE;SACzC,CAAC,CAAC,CACJ,EACD,sBAAsB,CACvB,CAAC;IACJ,CAAC;CACF;AAEY,QAAA,iCAAiC,GAC5C,IAAI,iCAAiC,EAAE,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { ClientMiddleware } from 'nice-grpc-common';
2
+ /**
3
+ * Client middleware that performs gRPC code mapping into RSDK exception
4
+ */
5
+ export declare const exceptionMapperMiddleware: () => ClientMiddleware;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exceptionMapperMiddleware = void 0;
4
+ const core_1 = require("@rsdk/core");
5
+ const nice_grpc_1 = require("nice-grpc");
6
+ /**
7
+ * Client middleware that performs gRPC code mapping into RSDK exception
8
+ */
9
+ const exceptionMapperMiddleware = () =>
10
+ // eslint-disable-next-line unicorn/consistent-function-scoping
11
+ async function* (call, options) {
12
+ try {
13
+ return yield* call.next(call.request, options);
14
+ }
15
+ catch (error) {
16
+ if (!(error instanceof nice_grpc_1.ClientError)) {
17
+ throw new core_1.InternalException('GRPC call failed with unknown error', {
18
+ cause: error,
19
+ });
20
+ }
21
+ const message = 'GRPC Call failed!';
22
+ const props = {
23
+ details: {
24
+ /*
25
+ * Details - will be original message from server,
26
+ * while message field will be a composite string with
27
+ * status path and original message in one line
28
+ */
29
+ message: error.details,
30
+ /**
31
+ * GRPC-package and method
32
+ */
33
+ path: error.path,
34
+ /**
35
+ * TBD: Other context from original error is not yet passed
36
+ * via GRPC. Will be added later.
37
+ */
38
+ },
39
+ };
40
+ /**
41
+ * According to /docs/images/exceptions.mapping.png
42
+ */
43
+ switch (error.code) {
44
+ case nice_grpc_1.Status.DEADLINE_EXCEEDED:
45
+ throw new core_1.TimeoutException(message, props);
46
+ case nice_grpc_1.Status.INVALID_ARGUMENT:
47
+ throw new core_1.InputException(message, props);
48
+ case nice_grpc_1.Status.ALREADY_EXISTS:
49
+ throw new core_1.DuplicateEntityException(message, props);
50
+ case nice_grpc_1.Status.FAILED_PRECONDITION:
51
+ throw new core_1.ConflictException(message, props);
52
+ case nice_grpc_1.Status.UNAUTHENTICATED:
53
+ throw new core_1.AuthException(message, props);
54
+ case nice_grpc_1.Status.PERMISSION_DENIED:
55
+ throw new core_1.NotAllowedException(message, props);
56
+ case nice_grpc_1.Status.NOT_FOUND:
57
+ throw new core_1.NotFoundException(message, props);
58
+ default:
59
+ throw new core_1.InternalException(message, props);
60
+ }
61
+ }
62
+ };
63
+ exports.exceptionMapperMiddleware = exceptionMapperMiddleware;
64
+ //# sourceMappingURL=exception-mapper.middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exception-mapper.middleware.js","sourceRoot":"","sources":["../../src/middleware/exception-mapper.middleware.ts"],"names":[],"mappings":";;;AAAA,qCASoB;AACpB,yCAAgD;AAGhD;;GAEG;AACI,MAAM,yBAAyB,GAAG,GAAqB,EAAE;AAC9D,+DAA+D;AAC/D,KAAK,SAAS,CAAC,EAAE,IAAI,EAAE,OAAO;IAC5B,IAAI;QACF,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;KAChD;IAAC,OAAO,KAAc,EAAE;QACvB,IAAI,CAAC,CAAC,KAAK,YAAY,uBAAW,CAAC,EAAE;YACnC,MAAM,IAAI,wBAAiB,CAAC,qCAAqC,EAAE;gBACjE,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;SACJ;QAED,MAAM,OAAO,GAAG,mBAAmB,CAAC;QACpC,MAAM,KAAK,GAAG;YACZ,OAAO,EAAE;gBACP;;;;mBAIG;gBACH,OAAO,EAAE,KAAK,CAAC,OAAO;gBAEtB;;mBAEG;gBACH,IAAI,EAAE,KAAK,CAAC,IAAI;gBAEhB;;;mBAGG;aACJ;SACF,CAAC;QAEF;;WAEG;QACH,QAAQ,KAAK,CAAC,IAAI,EAAE;YAClB,KAAK,kBAAM,CAAC,iBAAiB;gBAC3B,MAAM,IAAI,uBAAgB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC7C,KAAK,kBAAM,CAAC,gBAAgB;gBAC1B,MAAM,IAAI,qBAAc,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC3C,KAAK,kBAAM,CAAC,cAAc;gBACxB,MAAM,IAAI,+BAAwB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACrD,KAAK,kBAAM,CAAC,mBAAmB;gBAC7B,MAAM,IAAI,wBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC9C,KAAK,kBAAM,CAAC,eAAe;gBACzB,MAAM,IAAI,oBAAa,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC1C,KAAK,kBAAM,CAAC,iBAAiB;gBAC3B,MAAM,IAAI,0BAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAChD,KAAK,kBAAM,CAAC,SAAS;gBACnB,MAAM,IAAI,wBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC9C;gBACE,MAAM,IAAI,wBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;SAC/C;KACF;AACH,CAAC,CAAC;AAxDS,QAAA,yBAAyB,6BAwDlC"}
@@ -1,7 +1,8 @@
1
1
  import type { DynamicModule } from '@nestjs/common';
2
2
  import type { CompatServiceDefinition } from 'nice-grpc/lib/service-definitions';
3
3
  export declare class ModuleGenerator {
4
- static createModuleDefinition(serverName: string, definition: CompatServiceDefinition): DynamicModule;
4
+ static createClientModuleDefinition(serverName: string, definition: CompatServiceDefinition): DynamicModule;
5
+ static createClientFactoryModuleDefinition(definition: CompatServiceDefinition): DynamicModule;
5
6
  private static createChannelProvider;
6
7
  private static createClientProvider;
7
8
  }
@@ -7,11 +7,12 @@ const grpc_common_1 = require("@rsdk/grpc.common");
7
7
  const metadata_1 = require("@rsdk/metadata");
8
8
  const constants_1 = require("./metadata/constants");
9
9
  const grpc_clients_autodoc_resolver_1 = require("./metadata/grpc-clients.autodoc-resolver");
10
+ const grpc_clients_factory_autodoc_resolver_1 = require("./metadata/grpc-clients-factory.autodoc-resolver");
10
11
  const grpc_client_repository_1 = require("./repository/grpc-client.repository");
11
12
  const grpc_client_repository_module_1 = require("./repository/grpc-client-repository.module");
12
13
  const token_factories_1 = require("./token.factories");
13
14
  class ModuleGenerator {
14
- static createModuleDefinition(serverName, definition) {
15
+ static createClientModuleDefinition(serverName, definition) {
15
16
  const CONFIG_PROPERTY_ALIAS = (0, token_factories_1.getPropertyKey)(serverName);
16
17
  const propertyToken = (0, core_1.getPropertyToken)(CONFIG_PROPERTY_ALIAS);
17
18
  const clientToken = (0, token_factories_1.getClientToken)(definition);
@@ -42,6 +43,32 @@ class ModuleGenerator {
42
43
  autodoc_protocol_1.AutodocMetadata.defineResolver(moduleDefinition, constants_1.GRPC_CLIENTS_RSDK_METADATA_SCOPE, grpc_clients_autodoc_resolver_1.grpcClientAutodocResolver);
43
44
  return moduleDefinition;
44
45
  }
46
+ static createClientFactoryModuleDefinition(definition) {
47
+ const token = (0, token_factories_1.getFactoryToken)(definition);
48
+ const factoryProvider = {
49
+ provide: token,
50
+ inject: [grpc_client_repository_1.GrpcClientRepository],
51
+ useFactory(grpcClientRepository) {
52
+ return {
53
+ create(address) {
54
+ return grpcClientRepository.resolveClient(definition, grpcClientRepository.resolveChannel(address, (0, token_factories_1.getChannelToken)(address)), (0, token_factories_1.getClientToken)(definition));
55
+ },
56
+ };
57
+ },
58
+ };
59
+ const moduleDefinition = {
60
+ global: true,
61
+ imports: [grpc_client_repository_module_1.GrpcClientRepositoryModule],
62
+ module: ModuleGenerator,
63
+ providers: [factoryProvider],
64
+ exports: [factoryProvider],
65
+ };
66
+ metadata_1.RsdkMetadata.add(moduleDefinition, {
67
+ definition,
68
+ }, constants_1.GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE);
69
+ autodoc_protocol_1.AutodocMetadata.defineResolver(moduleDefinition, constants_1.GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE, grpc_clients_factory_autodoc_resolver_1.grpcClientsFactoryAutodocResolver);
70
+ return moduleDefinition;
71
+ }
45
72
  static createChannelProvider(channelToken, propertyToken) {
46
73
  return {
47
74
  provide: channelToken,
@@ -1 +1 @@
1
- {"version":3,"file":"module.generator.js","sourceRoot":"","sources":["../src/module.generator.ts"],"names":[],"mappings":";;;AACA,6DAAyD;AACzD,qCAIoB;AACpB,mDAAsD;AACtD,6CAA8C;AAI9C,oDAAwE;AACxE,4FAAqF;AACrF,gFAA2E;AAC3E,8FAAwF;AAExF,uDAI2B;AAE3B,MAAa,eAAe;IAC1B,MAAM,CAAC,sBAAsB,CAC3B,UAAkB,EAClB,UAAmC;QAEnC,MAAM,qBAAqB,GAAG,IAAA,gCAAc,EAAC,UAAU,CAAC,CAAC;QACzD,MAAM,aAAa,GAAG,IAAA,uBAAgB,EAAC,qBAAqB,CAAC,CAAC;QAC9D,MAAM,WAAW,GAAG,IAAA,gCAAc,EAAC,UAAU,CAAC,CAAC;QAC/C,MAAM,YAAY,GAAG,IAAA,iCAAe,EAAC,UAAU,CAAC,CAAC;QAEjD,MAAM,eAAe,GACnB,eAAe,CAAC,qBAAqB,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;QAErE,MAAM,cAAc,GAClB,eAAe,CAAC,oBAAoB,CAClC,WAAW,EACX,YAAY,EACZ,UAAU,CACX,CAAC;QAEJ,MAAM,gBAAgB,GAAG;YACvB,MAAM,EAAE,IAAI;YACZ,OAAO,EAAE;gBACP,2BAAoB,CAAC,WAAW,CAAC,qBAAqB,CAAC;gBACvD,0DAA0B;aAC3B;YACD,MAAM,EAAE,eAAe;YACvB,SAAS,EAAE,CAAC,eAAe,EAAE,cAAc,CAAC;YAC5C,OAAO,EAAE,CAAC,cAAc,CAAC;SAC1B,CAAC;QAEF,MAAM,GAAG,GAAG,IAAA,gCAAc,EAAC,UAAU,CAAC,CAAC;QAEvC,6BAAsB,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,GAAG,EAAE;YAC7D,GAAG,EAAE,GAAG;YACR,MAAM,EAAE,IAAI,+BAAiB,EAAE;YAC/B,WAAW,EAAE,GAAG,UAAU,CAAC,QAAQ,EAAE,eAAe;YACpD,aAAa,EAAE,KAAK;SACrB,CAAC,CAAC;QAEH,uBAAY,CAAC,GAAG,CACd,gBAAgB,EAChB;YACE,UAAU;YACV,UAAU;SACX,EACD,4CAAgC,CACjC,CAAC;QAEF,kCAAe,CAAC,cAAc,CAC5B,gBAAgB,EAChB,4CAAgC,EAChC,yDAAyB,CAC1B,CAAC;QACF,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IAEO,MAAM,CAAC,qBAAqB,CAClC,YAAoB,EACpB,aAAqD;QAErD,OAAO;YACL,OAAO,EAAE,YAAY;YAErB,MAAM,EAAE,CAAC,aAAa,EAAE,6CAAoB,CAAC;YAC7C,UAAU,EAAE,CAAC,OAAe,EAAE,IAA0B,EAAW,EAAE;gBACnE,OAAO,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;YACpD,CAAC;SACF,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,oBAAoB,CACjC,WAAmB,EACnB,YAAoB,EACpB,UAAmC;QAEnC,OAAO;YACL,OAAO,EAAE,WAAW;YAEpB,MAAM,EAAE,CAAC,YAAY,EAAE,6CAAoB,CAAC;YAC5C,UAAU,EAAE,CACV,OAAgB,EAChB,IAA0B,EACO,EAAE;gBACnC,OAAO,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;YAC9D,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AAxFD,0CAwFC"}
1
+ {"version":3,"file":"module.generator.js","sourceRoot":"","sources":["../src/module.generator.ts"],"names":[],"mappings":";;;AACA,6DAAyD;AACzD,qCAIoB;AACpB,mDAAsD;AACtD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,cAAc,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;YACpD,CAAC;SACF,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,oBAAoB,CACjC,WAAmB,EACnB,YAAoB,EACpB,UAAmC;QAEnC,OAAO;YACL,OAAO,EAAE,WAAW;YAEpB,MAAM,EAAE,CAAC,YAAY,EAAE,6CAAoB,CAAC;YAC5C,UAAU,EAAE,CACV,OAAgB,EAChB,IAA0B,EACO,EAAE;gBACnC,OAAO,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;YAC9D,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AAxID,0CAwIC"}
@@ -6,13 +6,8 @@ import type { CompatServiceDefinition } from 'nice-grpc/lib/service-definitions'
6
6
  */
7
7
  export declare class GrpcClientRepository {
8
8
  private static factory;
9
- logger: import("@rsdk/logging").ILogger;
10
- servers: Set<string>;
11
- definitions: Set<CompatServiceDefinition>;
12
- channels: Map<string, ChannelImplementation>;
13
- clients: Map<string, Client<CompatServiceDefinition>>;
14
- private static createChannel;
15
- private static createClient;
9
+ readonly channels: Map<string, ChannelImplementation>;
10
+ readonly clients: Map<string, Client<CompatServiceDefinition>>;
16
11
  resolveChannel(address: string, channelToken: string): ChannelImplementation;
17
12
  resolveClient(definition: CompatServiceDefinition, channel: ChannelImplementation, clientToken: string): Client<CompatServiceDefinition>;
18
13
  }
@@ -1,36 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GrpcClientRepository = void 0;
4
- const logging_1 = require("@rsdk/logging");
5
4
  const nice_grpc_1 = require("nice-grpc");
6
5
  const nice_grpc_opentelemetry_1 = require("nice-grpc-opentelemetry");
6
+ const exception_mapper_middleware_1 = require("../middleware/exception-mapper.middleware");
7
7
  /**
8
8
  * Простое кеш хранилище внутри DI для кеширования на замену статике
9
9
  */
10
10
  class GrpcClientRepository {
11
- static factory = (0, nice_grpc_1.createClientFactory)().use((0, nice_grpc_opentelemetry_1.openTelemetryClientMiddleware)());
12
- logger = logging_1.LoggerFactory.create(GrpcClientRepository);
13
- // Registration
14
- servers = new Set();
15
- definitions = new Set();
11
+ static factory = (0, nice_grpc_1.createClientFactory)()
12
+ .use((0, nice_grpc_opentelemetry_1.openTelemetryClientMiddleware)())
13
+ .use((0, exception_mapper_middleware_1.exceptionMapperMiddleware)());
16
14
  // Caches
17
15
  channels = new Map();
18
16
  clients = new Map();
19
- static createChannel(address) {
20
- return (0, nice_grpc_1.createChannel)(address);
21
- }
22
- static createClient(definition, channel) {
23
- return GrpcClientRepository.factory.create(definition, channel);
24
- }
25
17
  resolveChannel(address, channelToken) {
26
- const existed = this.channels.get(channelToken) ??
27
- GrpcClientRepository.createChannel(address);
28
- this.channels.set(channelToken, existed);
29
- return existed;
18
+ const channel = this.channels.get(channelToken) ?? (0, nice_grpc_1.createChannel)(address);
19
+ this.channels.set(channelToken, channel);
20
+ return channel;
30
21
  }
31
22
  resolveClient(definition, channel, clientToken) {
32
23
  const existed = this.clients.get(clientToken) ??
33
- GrpcClientRepository.createClient(definition, channel);
24
+ GrpcClientRepository.factory.create(definition, channel);
34
25
  this.clients.set(clientToken, existed);
35
26
  return existed;
36
27
  }
@@ -1 +1 @@
1
- {"version":3,"file":"grpc-client.repository.js","sourceRoot":"","sources":["../../src/repository/grpc-client.repository.ts"],"names":[],"mappings":";;;AACA,2CAA8C;AAE9C,yCAA+D;AAE/D,qEAAwE;AAExE;;GAEG;AACH,MAAa,oBAAoB;IACvB,MAAM,CAAC,OAAO,GAAkB,IAAA,+BAAmB,GAAE,CAAC,GAAG,CAC/D,IAAA,uDAA6B,GAAE,CAChC,CAAC;IAEF,MAAM,GAAG,uBAAa,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;IACpD,eAAe;IACf,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAC5B,WAAW,GAAG,IAAI,GAAG,EAA2B,CAAC;IACjD,SAAS;IACT,QAAQ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IACA,QAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;IACtC,OAAO,GAAG,IAAI,GAAG,EAA2C,CAAC;IAEtE,cAAc,CAAC,OAAe,EAAE,YAAoB;QAClD,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAA,yBAAa,EAAC,OAAO,CAAC,CAAC;QAE1E,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;QAEzC,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,aAAa,CACX,UAAmC,EACnC,OAA8B,EAC9B,WAAmB;QAEnB,MAAM,OAAO,GACX,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;YAC7B,oBAAoB,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAE3D,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QAEvC,OAAO,OAAO,CAAC;IACjB,CAAC;;AA7BH,oDA8BC"}
@@ -26,3 +26,6 @@ export type GrpcServerMetadataValue = {
26
26
  serverName: string;
27
27
  definition: CompatServiceDefinition;
28
28
  };
29
+ export type GrpcFactoryMetadataValue = {
30
+ definition: CompatServiceDefinition;
31
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsdk/grpc.clients",
3
- "version": "2.6.0-next.0",
3
+ "version": "2.6.0-next.10",
4
4
  "description": "Grpc clients infrastructure",
5
5
  "license": "Apache License 2.0",
6
6
  "publishConfig": {
@@ -17,15 +17,15 @@
17
17
  "@grpc/grpc-js": "^1.8.7",
18
18
  "@nestjs/common": "^9.0.0",
19
19
  "@nestjs/microservices": "^9.0.0",
20
- "@rsdk/autodoc.protocol": "^2.4.2",
21
- "@rsdk/common": "^2.4.2",
22
- "@rsdk/core": "^2.5.0",
23
- "@rsdk/grpc.common": "^2.5.0",
24
- "@rsdk/logging": "^2.4.2",
25
- "@rsdk/metadata": "^2.4.2",
20
+ "@rsdk/autodoc.protocol": "^2.6.0-next.9",
21
+ "@rsdk/common": "^2.6.0-next.9",
22
+ "@rsdk/core": "^2.6.0-next.10",
23
+ "@rsdk/grpc.common": "^2.6.0-next.10",
24
+ "@rsdk/logging": "^2.6.0-next.9",
25
+ "@rsdk/metadata": "^2.6.0-next.9",
26
26
  "nice-grpc": "^2.1.4",
27
27
  "nice-grpc-opentelemetry": "^0.1.2",
28
28
  "rxjs": "^7.0.0"
29
29
  },
30
- "gitHead": "0b5c41c482784db44ab35ef3b8990d10f9978645"
30
+ "gitHead": "742e72916c46d5dccc38e0f6c3003f8457921bc4"
31
31
  }
@@ -1,6 +1,9 @@
1
1
  import { Inject } from '@nestjs/common';
2
+ import { PLATFORM_RAW_GLOBAL_METADATA_SCOPE } from '@rsdk/core/dist/rsdk-metadata/constants';
3
+ import { RsdkMetadata } from '@rsdk/metadata';
2
4
  import type { CompatServiceDefinition } from 'nice-grpc/lib/service-definitions';
3
5
 
6
+ import { ModuleGenerator } from '../module.generator';
4
7
  import { getFactoryToken } from '../token.factories';
5
8
 
6
9
  // Factory for grpc-clients
@@ -22,8 +25,14 @@ export interface GrpcClientsFactory<T> {
22
25
  export const InjectGrpcClientsFactory = (
23
26
  definition: CompatServiceDefinition,
24
27
  ): ParameterDecorator => {
25
- const token = getFactoryToken(definition);
26
-
27
- // TODO Add metadata registration
28
- return Inject(token);
28
+ return (target, propertyKey, parameterIndex) => {
29
+ Inject(getFactoryToken(definition))(target, propertyKey, parameterIndex);
30
+ RsdkMetadata.setWithScope(
31
+ target,
32
+ PLATFORM_RAW_GLOBAL_METADATA_SCOPE,
33
+ definition.fullName,
34
+ ModuleGenerator.createClientFactoryModuleDefinition(definition),
35
+ true,
36
+ );
37
+ };
29
38
  };
@@ -33,7 +33,7 @@ export const InjectGrpcClient = <
33
33
  target,
34
34
  PLATFORM_RAW_GLOBAL_METADATA_SCOPE,
35
35
  `${grpcServerDeclarationValue.serverName}.${grpcServerDeclarationValue.serviceName}`,
36
- ModuleGenerator.createModuleDefinition(
36
+ ModuleGenerator.createClientModuleDefinition(
37
37
  grpcServerDeclarationValue.serverName,
38
38
  grpcServerDeclarationValue.definition,
39
39
  ),
@@ -1 +1,2 @@
1
1
  export const GRPC_CLIENTS_RSDK_METADATA_SCOPE = 'grpc-clients';
2
+ export const GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE = 'grpc-clients-factory';
@@ -0,0 +1,31 @@
1
+ import type { DocumentNode, DocumentResolver } from '@rsdk/autodoc.protocol';
2
+ import { Composite, Table } from '@rsdk/autodoc.protocol';
3
+ import type { RsdkMetadataProvider } from '@rsdk/metadata';
4
+ import type { Promisable } from 'type-fest';
5
+
6
+ import type { GrpcFactoryMetadataValue } from '../servers/types';
7
+
8
+ import { GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE } from './constants';
9
+
10
+ class GrpcClientsFactoryAutodocResolver implements DocumentResolver {
11
+ getNode(rsdkMetadata: RsdkMetadataProvider): Promisable<DocumentNode | null> {
12
+ const resources = rsdkMetadata.get<GrpcFactoryMetadataValue>(
13
+ GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE,
14
+ );
15
+ if (!resources) {
16
+ return null;
17
+ }
18
+ return new Composite(
19
+ new Table(
20
+ resources.map(({ value: { definition } }) => ({
21
+ name: definition.name.toString(),
22
+ fullName: definition.fullName.toString(),
23
+ })),
24
+ ),
25
+ 'Grpc clients factory',
26
+ );
27
+ }
28
+ }
29
+
30
+ export const grpcClientsFactoryAutodocResolver =
31
+ new GrpcClientsFactoryAutodocResolver();
@@ -0,0 +1,73 @@
1
+ import {
2
+ AuthException,
3
+ ConflictException,
4
+ DuplicateEntityException,
5
+ InputException,
6
+ InternalException,
7
+ NotAllowedException,
8
+ NotFoundException,
9
+ TimeoutException,
10
+ } from '@rsdk/core';
11
+ import { ClientError, Status } from 'nice-grpc';
12
+ import type { ClientMiddleware } from 'nice-grpc-common';
13
+
14
+ /**
15
+ * Client middleware that performs gRPC code mapping into RSDK exception
16
+ */
17
+ export const exceptionMapperMiddleware = (): ClientMiddleware =>
18
+ // eslint-disable-next-line unicorn/consistent-function-scoping
19
+ async function* (call, options) {
20
+ try {
21
+ return yield* call.next(call.request, options);
22
+ } catch (error: unknown) {
23
+ if (!(error instanceof ClientError)) {
24
+ throw new InternalException('GRPC call failed with unknown error', {
25
+ cause: error,
26
+ });
27
+ }
28
+
29
+ const message = 'GRPC Call failed!';
30
+ const props = {
31
+ details: {
32
+ /*
33
+ * Details - will be original message from server,
34
+ * while message field will be a composite string with
35
+ * status path and original message in one line
36
+ */
37
+ message: error.details,
38
+
39
+ /**
40
+ * GRPC-package and method
41
+ */
42
+ path: error.path,
43
+
44
+ /**
45
+ * TBD: Other context from original error is not yet passed
46
+ * via GRPC. Will be added later.
47
+ */
48
+ },
49
+ };
50
+
51
+ /**
52
+ * According to /docs/images/exceptions.mapping.png
53
+ */
54
+ switch (error.code) {
55
+ case Status.DEADLINE_EXCEEDED:
56
+ throw new TimeoutException(message, props);
57
+ case Status.INVALID_ARGUMENT:
58
+ throw new InputException(message, props);
59
+ case Status.ALREADY_EXISTS:
60
+ throw new DuplicateEntityException(message, props);
61
+ case Status.FAILED_PRECONDITION:
62
+ throw new ConflictException(message, props);
63
+ case Status.UNAUTHENTICATED:
64
+ throw new AuthException(message, props);
65
+ case Status.PERMISSION_DENIED:
66
+ throw new NotAllowedException(message, props);
67
+ case Status.NOT_FOUND:
68
+ throw new NotFoundException(message, props);
69
+ default:
70
+ throw new InternalException(message, props);
71
+ }
72
+ }
73
+ };
@@ -1,4 +1,4 @@
1
- import type { DynamicModule, FactoryProvider } from '@nestjs/common';
1
+ import type { DynamicModule, FactoryProvider, Provider } from '@nestjs/common';
2
2
  import { AutodocMetadata } from '@rsdk/autodoc.protocol';
3
3
  import {
4
4
  ConfigMetadataRegistry,
@@ -10,19 +10,28 @@ import { RsdkMetadata } from '@rsdk/metadata';
10
10
  import type { Channel, Client } from 'nice-grpc';
11
11
  import type { CompatServiceDefinition } from 'nice-grpc/lib/service-definitions';
12
12
 
13
- import { GRPC_CLIENTS_RSDK_METADATA_SCOPE } from './metadata/constants';
13
+ import type { GrpcClientsFactory } from './decorators/inject-client-factory.decorator';
14
+ import {
15
+ GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE,
16
+ GRPC_CLIENTS_RSDK_METADATA_SCOPE,
17
+ } from './metadata/constants';
14
18
  import { grpcClientAutodocResolver } from './metadata/grpc-clients.autodoc-resolver';
19
+ import { grpcClientsFactoryAutodocResolver } from './metadata/grpc-clients-factory.autodoc-resolver';
15
20
  import { GrpcClientRepository } from './repository/grpc-client.repository';
16
21
  import { GrpcClientRepositoryModule } from './repository/grpc-client-repository.module';
17
- import type { GrpcServerMetadataValue } from './servers/types';
22
+ import type {
23
+ GrpcFactoryMetadataValue,
24
+ GrpcServerMetadataValue,
25
+ } from './servers/types';
18
26
  import {
19
27
  getChannelToken,
20
28
  getClientToken,
29
+ getFactoryToken,
21
30
  getPropertyKey,
22
31
  } from './token.factories';
23
32
 
24
33
  export class ModuleGenerator {
25
- static createModuleDefinition(
34
+ static createClientModuleDefinition(
26
35
  serverName: string,
27
36
  definition: CompatServiceDefinition,
28
37
  ): DynamicModule {
@@ -78,6 +87,54 @@ export class ModuleGenerator {
78
87
  return moduleDefinition;
79
88
  }
80
89
 
90
+ static createClientFactoryModuleDefinition(
91
+ definition: CompatServiceDefinition,
92
+ ): DynamicModule {
93
+ const token = getFactoryToken(definition);
94
+ const factoryProvider: Provider = {
95
+ provide: token,
96
+ inject: [GrpcClientRepository],
97
+ useFactory(
98
+ grpcClientRepository: GrpcClientRepository,
99
+ ): GrpcClientsFactory<any> {
100
+ return {
101
+ create(address: string): any {
102
+ return grpcClientRepository.resolveClient(
103
+ definition,
104
+ grpcClientRepository.resolveChannel(
105
+ address,
106
+ getChannelToken(address),
107
+ ),
108
+ getClientToken(definition),
109
+ );
110
+ },
111
+ };
112
+ },
113
+ };
114
+ const moduleDefinition = {
115
+ global: true,
116
+ imports: [GrpcClientRepositoryModule],
117
+ module: ModuleGenerator,
118
+ providers: [factoryProvider],
119
+ exports: [factoryProvider],
120
+ };
121
+
122
+ RsdkMetadata.add<GrpcFactoryMetadataValue>(
123
+ moduleDefinition,
124
+ {
125
+ definition,
126
+ },
127
+ GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE,
128
+ );
129
+
130
+ AutodocMetadata.defineResolver(
131
+ moduleDefinition,
132
+ GRPC_CLIENTS_FACTORY_RSDK_METADATA_SCOPE,
133
+ grpcClientsFactoryAutodocResolver,
134
+ );
135
+ return moduleDefinition;
136
+ }
137
+
81
138
  private static createChannelProvider(
82
139
  channelToken: string,
83
140
  propertyToken: `STANDALONE_CONFIG_PROPERTY_${string}`,
@@ -1,45 +1,29 @@
1
1
  import type { ChannelImplementation } from '@grpc/grpc-js/build/src/channel';
2
- import { LoggerFactory } from '@rsdk/logging';
3
- import type { Channel, Client, ClientFactory } from 'nice-grpc';
2
+ import type { Channel, Client } from 'nice-grpc';
4
3
  import { createChannel, createClientFactory } from 'nice-grpc';
5
4
  import type { CompatServiceDefinition } from 'nice-grpc/lib/service-definitions';
6
5
  import { openTelemetryClientMiddleware } from 'nice-grpc-opentelemetry';
7
6
 
7
+ import { exceptionMapperMiddleware } from '../middleware/exception-mapper.middleware';
8
+
8
9
  /**
9
10
  * Простое кеш хранилище внутри DI для кеширования на замену статике
10
11
  */
11
12
  export class GrpcClientRepository {
12
- private static factory: ClientFactory = createClientFactory().use(
13
- openTelemetryClientMiddleware(),
14
- );
15
-
16
- logger = LoggerFactory.create(GrpcClientRepository);
17
- // Registration
18
- servers = new Set<string>();
19
- definitions = new Set<CompatServiceDefinition>();
20
- // Caches
21
- channels = new Map<string, Channel>();
22
- clients = new Map<string, Client<CompatServiceDefinition>>();
13
+ private static factory = createClientFactory()
14
+ .use(openTelemetryClientMiddleware())
15
+ .use(exceptionMapperMiddleware());
23
16
 
24
- private static createChannel(address: string): Channel {
25
- return createChannel(address);
26
- }
27
-
28
- private static createClient(
29
- definition: CompatServiceDefinition,
30
- channel: ChannelImplementation,
31
- ): Client<CompatServiceDefinition> {
32
- return GrpcClientRepository.factory.create(definition, channel);
33
- }
17
+ // Caches
18
+ readonly channels = new Map<string, Channel>();
19
+ readonly clients = new Map<string, Client<CompatServiceDefinition>>();
34
20
 
35
21
  resolveChannel(address: string, channelToken: string): ChannelImplementation {
36
- const existed: Channel =
37
- this.channels.get(channelToken) ??
38
- GrpcClientRepository.createChannel(address);
22
+ const channel = this.channels.get(channelToken) ?? createChannel(address);
39
23
 
40
- this.channels.set(channelToken, existed);
24
+ this.channels.set(channelToken, channel);
41
25
 
42
- return existed;
26
+ return channel;
43
27
  }
44
28
 
45
29
  resolveClient(
@@ -49,7 +33,7 @@ export class GrpcClientRepository {
49
33
  ): Client<CompatServiceDefinition> {
50
34
  const existed: Client<CompatServiceDefinition> =
51
35
  this.clients.get(clientToken) ??
52
- GrpcClientRepository.createClient(definition, channel);
36
+ GrpcClientRepository.factory.create(definition, channel);
53
37
 
54
38
  this.clients.set(clientToken, existed);
55
39
 
@@ -43,3 +43,7 @@ export type GrpcServerMetadataValue = {
43
43
  serverName: string;
44
44
  definition: CompatServiceDefinition;
45
45
  };
46
+
47
+ export type GrpcFactoryMetadataValue = {
48
+ definition: CompatServiceDefinition;
49
+ };