@duongtrungnguyen/nestro
Version:
Service registry for Nest JS
83 lines • 3.25 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __decorateClass = (decorators, target, key, kind) => {
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
for (var i = decorators.length - 1, decorator; i >= 0; i--)
if (decorator = decorators[i])
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
if (kind && result) __defProp(target, key, result);
return result;
};
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
var plain_client_service_exports = {};
__export(plain_client_service_exports, {
PlainClientService: () => PlainClientService
});
module.exports = __toCommonJS(plain_client_service_exports);
var import_common = require("@nestjs/common");
var import_common2 = require("../../common");
var import_constants = require("../constants");
let PlainClientService = class {
constructor(instanceInfo, serverInfo) {
this.instanceInfo = instanceInfo;
this.serverBaseUrl = (0, import_common2.getServerURL)(serverInfo);
}
async onModuleInit() {
await this.register();
}
async beforeApplicationShutdown() {
clearInterval(this.intervalId);
await this.deregister();
}
getHeaders() {
return { "Content-Type": "application/json" };
}
async register() {
const response = await fetch(`${this.serverBaseUrl}/nestro/register`, {
method: "POST",
body: (0, import_common2.normalizeJson)(this.instanceInfo),
headers: this.getHeaders()
});
const data = await response.json();
this.intervalId = setInterval(() => this.sendHeartbeat(), data.heartbeatInterval);
}
async sendHeartbeat() {
await fetch(`${this.serverBaseUrl}/nestro/heartbeat`, {
method: "POST",
body: (0, import_common2.normalizeJson)(this.instanceInfo),
headers: this.getHeaders()
});
}
async deregister() {
await fetch(`${this.serverBaseUrl}/nestro/deregister`, {
method: "DELETE",
body: (0, import_common2.normalizeJson)(this.instanceInfo),
headers: this.getHeaders()
});
}
};
PlainClientService = __decorateClass([
(0, import_common.Injectable)(),
__decorateParam(0, (0, import_common.Inject)(import_constants.INSTANCE_INFO)),
__decorateParam(1, (0, import_common.Inject)(import_constants.SERVER_INFO))
], PlainClientService);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PlainClientService
});
//# sourceMappingURL=plain-client.service.js.map