@duongtrungnguyen/nestro
Version:
Service registry for Nest JS
83 lines • 2.38 kB
JavaScript
import { Logger } from "@nestjs/common";
import { CLASS_REGEX } from "./constants";
function canLog() {
const isDevelopment = process.env.NODE_ENV != "production";
const disableLog = process.env.NESTRO_LOG == "off";
return isDevelopment && !disableLog;
}
function debugLog(context, message, data) {
if (canLog()) {
if (data) {
Logger.debug(`${message} - ${JSON.stringify(data)}`, context);
} else {
Logger.debug(message, context);
}
}
}
function debugWarn(context, message, data) {
if (canLog()) {
if (data) {
Logger.warn(`${message} - ${JSON.stringify(data)}`, context);
} else {
Logger.warn(message, context);
}
}
}
function debugError(context, message, data) {
if (canLog()) {
if (data) {
Logger.error(`${message} - ${JSON.stringify(data)}`, context);
} else {
Logger.error(message, context);
}
}
}
function normalizeJson(data) {
function sortKeys(obj) {
if (Array.isArray(obj)) {
return obj.map(sortKeys);
} else if (obj !== null && typeof obj === "object") {
return Object.keys(obj).sort().reduce((acc, key) => {
acc[key] = sortKeys(obj[key]);
return acc;
}, {});
}
return obj;
}
return JSON.stringify(sortKeys(data));
}
function buildInstanceHttpUrl(instance) {
return `${instance.protocol}://${instance.host}${instance.port ? `:${instance.port}` : ""}`;
}
function buildInstanceWsUrl(instance) {
return `${instance.protocol === "https" ? "wss" : "ws"}://${instance.host}${instance.port ? `:${instance.port}` : ""}`;
}
function buildHttpUrl(host, protocol = "http", port) {
return `${protocol}://${host}${port ? `:${port}` : ""}`;
}
function buildWsUrl(host, protocol = "ws", port) {
return `${protocol}://${host}${port ? `:${port}` : ""}`;
}
function getHttpSecureProtocol(secure) {
return secure ? "https" : "http";
}
function isClass(target) {
return typeof target === "function" && CLASS_REGEX.test(Function.prototype.toString.call(target));
}
function getServerURL(server) {
return server instanceof URL ? server.toString() : buildHttpUrl(server.host, server.protocol, server.port);
}
export {
buildHttpUrl,
buildInstanceHttpUrl,
buildInstanceWsUrl,
buildWsUrl,
debugError,
debugLog,
debugWarn,
getHttpSecureProtocol,
getServerURL,
isClass,
normalizeJson
};
//# sourceMappingURL=utils.js.map