UNPKG

3.08 kBJavaScriptView Raw
1"use strict";
2var ClientsModule_1;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.ClientsModule = void 0;
5const tslib_1 = require("tslib");
6const common_1 = require("@nestjs/common");
7const client_1 = require("../client");
8let ClientsModule = ClientsModule_1 = class ClientsModule {
9 static register(options) {
10 const clientsOptions = !Array.isArray(options) ? options.clients : options;
11 const clients = (clientsOptions || []).map(item => ({
12 provide: item.name,
13 useValue: this.assignOnAppShutdownHook(client_1.ClientProxyFactory.create(item)),
14 }));
15 return {
16 module: ClientsModule_1,
17 global: !Array.isArray(options) && options.isGlobal,
18 providers: clients,
19 exports: clients,
20 };
21 }
22 static registerAsync(options) {
23 const clientsOptions = !Array.isArray(options) ? options.clients : options;
24 const providers = clientsOptions.reduce((accProviders, item) => accProviders
25 .concat(this.createAsyncProviders(item))
26 .concat(item.extraProviders || []), []);
27 const imports = clientsOptions.reduce((accImports, option) => option.imports && !accImports.includes(option.imports)
28 ? accImports.concat(option.imports)
29 : accImports, []);
30 return {
31 module: ClientsModule_1,
32 global: !Array.isArray(options) && options.isGlobal,
33 imports,
34 providers: providers,
35 exports: providers,
36 };
37 }
38 static createAsyncProviders(options) {
39 if (options.useExisting || options.useFactory) {
40 return [this.createAsyncOptionsProvider(options)];
41 }
42 return [
43 this.createAsyncOptionsProvider(options),
44 {
45 provide: options.useClass,
46 useClass: options.useClass,
47 },
48 ];
49 }
50 static createAsyncOptionsProvider(options) {
51 if (options.useFactory) {
52 return {
53 provide: options.name,
54 useFactory: this.createFactoryWrapper(options.useFactory),
55 inject: options.inject || [],
56 };
57 }
58 return {
59 provide: options.name,
60 useFactory: this.createFactoryWrapper((optionsFactory) => optionsFactory.createClientOptions()),
61 inject: [options.useExisting || options.useClass],
62 };
63 }
64 static createFactoryWrapper(useFactory) {
65 return async (...args) => {
66 const clientOptions = await useFactory(...args);
67 const clientProxyRef = client_1.ClientProxyFactory.create(clientOptions);
68 return this.assignOnAppShutdownHook(clientProxyRef);
69 };
70 }
71 static assignOnAppShutdownHook(client) {
72 client.onApplicationShutdown =
73 client.close;
74 return client;
75 }
76};
77exports.ClientsModule = ClientsModule;
78exports.ClientsModule = ClientsModule = ClientsModule_1 = tslib_1.__decorate([
79 (0, common_1.Module)({})
80], ClientsModule);