1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.MicroservicesModule = void 0;
|
4 | const runtime_exception_1 = require("@nestjs/core/errors/exceptions/runtime.exception");
|
5 | const guards_1 = require("@nestjs/core/guards");
|
6 | const injector_1 = require("@nestjs/core/injector/injector");
|
7 | const interceptors_1 = require("@nestjs/core/interceptors");
|
8 | const pipes_1 = require("@nestjs/core/pipes");
|
9 | const client_1 = require("./client");
|
10 | const container_1 = require("./container");
|
11 | const exception_filters_context_1 = require("./context/exception-filters-context");
|
12 | const rpc_context_creator_1 = require("./context/rpc-context-creator");
|
13 | const rpc_proxy_1 = require("./context/rpc-proxy");
|
14 | const listeners_controller_1 = require("./listeners-controller");
|
15 | class MicroservicesModule {
|
16 | constructor() {
|
17 | this.clientsContainer = new container_1.ClientsContainer();
|
18 | }
|
19 | register(container, graphInspector, config, options) {
|
20 | this.appOptions = options;
|
21 | const exceptionFiltersContext = new exception_filters_context_1.ExceptionFiltersContext(container, config);
|
22 | const contextCreator = new rpc_context_creator_1.RpcContextCreator(new rpc_proxy_1.RpcProxy(), exceptionFiltersContext, new pipes_1.PipesContextCreator(container, config), new pipes_1.PipesConsumer(), new guards_1.GuardsContextCreator(container, config), new guards_1.GuardsConsumer(), new interceptors_1.InterceptorsContextCreator(container, config), new interceptors_1.InterceptorsConsumer());
|
23 | const injector = new injector_1.Injector();
|
24 | this.listenersController = new listeners_controller_1.ListenersController(this.clientsContainer, contextCreator, container, injector, client_1.ClientProxyFactory, exceptionFiltersContext, graphInspector);
|
25 | }
|
26 | setupListeners(container, server) {
|
27 | if (!this.listenersController) {
|
28 | throw new runtime_exception_1.RuntimeException();
|
29 | }
|
30 | const modules = container.getModules();
|
31 | modules.forEach(({ controllers }, moduleRef) => this.bindListeners(controllers, server, moduleRef));
|
32 | }
|
33 | setupClients(container) {
|
34 | if (!this.listenersController) {
|
35 | throw new runtime_exception_1.RuntimeException();
|
36 | }
|
37 | if (this.appOptions?.preview) {
|
38 | return;
|
39 | }
|
40 | const modules = container.getModules();
|
41 | modules.forEach(({ controllers, providers }) => {
|
42 | this.bindClients(controllers);
|
43 | this.bindClients(providers);
|
44 | });
|
45 | }
|
46 | bindListeners(controllers, server, moduleName) {
|
47 | controllers.forEach(wrapper => this.listenersController.registerPatternHandlers(wrapper, server, moduleName));
|
48 | }
|
49 | bindClients(items) {
|
50 | items.forEach(({ instance, isNotMetatype }) => {
|
51 | !isNotMetatype &&
|
52 | this.listenersController.assignClientsToProperties(instance);
|
53 | });
|
54 | }
|
55 | async close() {
|
56 | const clients = this.clientsContainer.getAllClients();
|
57 | await Promise.all(clients.map(client => client.close()));
|
58 | this.clientsContainer.clear();
|
59 | }
|
60 | }
|
61 | exports.MicroservicesModule = MicroservicesModule;
|