@krmao/react-basic
Version:
### features
72 lines (71 loc) • 3.32 kB
JavaScript
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols,JSUnresolvedVariable,JSUnresolvedFunction,NodeCoreCodingAssistance
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var index_1 = (0, tslib_1.__importDefault)(require("../index"));
var BasicValueUtil = require("./basic-value-util");
var UAT = "UAT";
var PROD = "PROD";
var URL = typeof window !== "undefined" && window.URL ? window.URL : require("url").URL;
var _apiEnv = "";
var _didLog = false;
/**
* https://github.com/vercel/next.js/issues/1553#issuecomment-290062887
* @param currentHostname
* @return {string}
*/
var getApiEnvByURL = function (currentHostname) {
var _a, _b;
if (currentHostname === void 0) { currentHostname = (_b = (_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : ""; }
var prodHostName = new URL(index_1.default.apiDomainProd()).hostname;
var uatHostName = new URL(index_1.default.apiDomainUat()).hostname;
var devHostName = new URL(index_1.default.apiDomainDev()).hostname;
var currentApiEnv;
if (currentHostname === prodHostName) {
currentApiEnv = PROD;
}
else if (currentHostname === uatHostName) {
currentApiEnv = UAT;
}
else if (currentHostname === devHostName) {
currentApiEnv = UAT;
}
else {
currentApiEnv = PROD;
}
if (!_didLog) {
console.log("env currentHostname =", currentHostname);
console.log("env prodHostName =", prodHostName);
console.log("env uatHostName =", uatHostName);
console.log("env devHostName =", devHostName);
console.log("env", currentApiEnv);
_didLog = true;
}
return currentApiEnv;
};
var getApiEnv = function (serverContext) {
var _a, _b, _c, _d, _e;
if (serverContext === void 0) { serverContext = null; }
if (_apiEnv === "") {
if (typeof window !== "undefined") {
var currentHostName = (_b = (_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : "";
console.log("> call on client side currentHostName=", currentHostName);
_apiEnv = getApiEnvByURL(currentHostName);
}
else {
var currentHost = (_e = (_d = (_c = serverContext === null || serverContext === void 0 ? void 0 : serverContext.req) === null || _c === void 0 ? void 0 : _c.headers) === null || _d === void 0 ? void 0 : _d.host) !== null && _e !== void 0 ? _e : "";
currentHost = (BasicValueUtil.stringStartsWith(currentHost, "http") ? "" : "https://") + currentHost;
var currentHostName = BasicValueUtil.isStringNotBlank(currentHost) ? new URL(currentHost).hostname : "";
console.log("> call on server side currentHostName=", currentHostName, ", currentHost=", currentHost, ", url=", new URL(currentHost), ", serverContext=", serverContext);
_apiEnv = getApiEnvByURL(currentHostName);
}
}
return _apiEnv;
};
var BasicEnv = {
getApiEnv: getApiEnv,
getDeploymentEnv: function () { return process.env.APP_ENV; },
UAT: UAT,
PROD: PROD
};
exports.default = BasicEnv;
;