@nmxjs/api 1.1.14 → 1.1.15
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,8 +1,10 @@
|
|
|
1
1
|
/// <reference types="types" />
|
|
2
|
+
import { ClientNats } from '@nestjs/microservices';
|
|
2
3
|
import { IConfig } from '@nmxjs/config';
|
|
3
4
|
import type { ICreateApiServiceOptions, IApiServiceWithInfo } from '../../ApiService';
|
|
4
5
|
export declare class CreateApiService {
|
|
5
6
|
protected readonly config: IConfig;
|
|
7
|
+
protected readonly clients: Map<string, ClientNats>;
|
|
6
8
|
constructor(config: IConfig);
|
|
7
9
|
call(options: ICreateApiServiceOptions): Promise<IApiServiceWithInfo>;
|
|
8
10
|
}
|
|
@@ -17,10 +17,10 @@ const objHash = require("object-hash");
|
|
|
17
17
|
const microservices_1 = require("@nestjs/microservices");
|
|
18
18
|
const common_1 = require("@nestjs/common");
|
|
19
19
|
const config_1 = require("@nmxjs/config");
|
|
20
|
-
const clients = new Map();
|
|
21
20
|
let CreateApiService = class CreateApiService {
|
|
22
21
|
constructor(config) {
|
|
23
22
|
this.config = config;
|
|
23
|
+
this.clients = new Map();
|
|
24
24
|
}
|
|
25
25
|
async call(options) {
|
|
26
26
|
const natsOptions = {
|
|
@@ -28,22 +28,22 @@ let CreateApiService = class CreateApiService {
|
|
|
28
28
|
options: Object.assign({ servers: this.config.transport.services.map(v => `nats://${v.host || '127.0.0.1'}:${v.port || 4222}`).sort() }, (this.config.transport.keepaliveTimeMs ? { pingInterval: this.config.transport.keepaliveTimeMs } : {})),
|
|
29
29
|
};
|
|
30
30
|
const key = objHash(natsOptions);
|
|
31
|
-
let client = clients.get(key);
|
|
31
|
+
let client = this.clients.get(key);
|
|
32
32
|
if (!client) {
|
|
33
33
|
client = microservices_1.ClientProxyFactory.create(natsOptions);
|
|
34
|
+
this.clients.set(key, client);
|
|
34
35
|
await client.connect();
|
|
35
|
-
|
|
36
|
+
const onClose = () => {
|
|
37
|
+
client.close();
|
|
38
|
+
this.clients.delete(key);
|
|
39
|
+
};
|
|
40
|
+
process.once('SIGTERM', onClose);
|
|
41
|
+
process.once('SIGINT', onClose);
|
|
36
42
|
}
|
|
37
43
|
const service = Object.keys(options.schema).reduce((res, methodName) => {
|
|
38
44
|
res[methodName] = data => client.send(`${options.subService || options.service}.${methodName}`, data);
|
|
39
45
|
return res;
|
|
40
46
|
}, {});
|
|
41
|
-
const onClose = () => {
|
|
42
|
-
client.close();
|
|
43
|
-
clients.delete(key);
|
|
44
|
-
};
|
|
45
|
-
process.once('SIGTERM', onClose);
|
|
46
|
-
process.once('SIGINT', onClose);
|
|
47
47
|
return {
|
|
48
48
|
serviceName: options.service,
|
|
49
49
|
options: natsOptions,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CreateApiService.js","sourceRoot":"","sources":["../../../src/NatsTransport/services/CreateApiService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uCAAuC;AACvC,yDAAmF;AACnF,2CAAoD;AAEpD,0CAAmD;
|
|
1
|
+
{"version":3,"file":"CreateApiService.js","sourceRoot":"","sources":["../../../src/NatsTransport/services/CreateApiService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uCAAuC;AACvC,yDAAmF;AACnF,2CAAoD;AAEpD,0CAAmD;AAI5C,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAG3B,YAA+B,MAAkC;QAAf,WAAM,GAAN,MAAM,CAAS;QAF9C,YAAO,GAAG,IAAI,GAAG,EAAsB,CAAC;IAES,CAAC;IAE9D,KAAK,CAAC,IAAI,CAAC,OAAiC;QACjD,MAAM,WAAW,GAAgB;YAC/B,SAAS,EAAE,yBAAS,CAAC,IAAI;YACzB,OAAO,kBACL,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,IAAI,IAA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