@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
130 lines (129 loc) • 5.62 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchApi = void 0;
const agentkeepalive_1 = __importStar(require("agentkeepalive"));
const axios_1 = __importDefault(require("axios"));
const log_1 = require("diginext-utils/dist/xconsole/log");
const inspector_1 = require("inspector");
const config_1 = require("../../config/config");
const keepAliveAgent = new agentkeepalive_1.default({
maxSockets: 160,
maxFreeSockets: 160,
timeout: 60000,
freeSocketTimeout: 30000,
keepAliveMsecs: 60000,
});
const httpsKeepAliveAgent = new agentkeepalive_1.HttpsAgent({
maxSockets: 160,
maxFreeSockets: 160,
timeout: 60000,
freeSocketTimeout: 30000,
keepAliveMsecs: 60000,
});
async function fetchApi(options) {
var _a;
const { access_token, api_key, method = "GET", isDebugging = false } = options;
const { buildServerUrl = process.env.BASE_URL, currentUser, access_token: cachedAccessToken, refresh_token: cachedRefreshToken, apiToken: cachedApiKey, } = (0, config_1.getCliConfig)();
if (options.isDebugging) {
console.log("=====================================");
console.log(" fetchApi() > buildServerUrl :>> ", buildServerUrl);
console.log(" fetchApi() > cachedAccessToken :>> ", cachedAccessToken);
console.log(" fetchApi() > cachedRefreshToken :>> ", cachedRefreshToken);
console.log(" fetchApi() > cachedApiKey :>> ", cachedApiKey);
console.log("=====================================");
}
if (!buildServerUrl) {
(0, log_1.logError)(`"BUILD SERVER URL" not found. Please login with: "dx login <BUILD_SERVER_URL>"`);
return {
status: 0,
messages: [`"BUILD SERVER URL" not found. Please login with: "dx login <BUILD_SERVER_URL>"`],
data: null,
};
}
options.baseURL = buildServerUrl;
options.maxContentLength = 200000000;
options.maxBodyLength = 200000000;
options.httpAgent = keepAliveAgent;
options.httpsAgent = httpsKeepAliveAgent;
if (access_token) {
options.headers = { ...options.headers, Authorization: `Bearer ${access_token}` };
}
else if (cachedAccessToken) {
options.headers = { ...options.headers, Authorization: `Bearer ${cachedAccessToken}` };
}
// else if (currentUser?.token?.access_token) {
// options.headers = { ...options.headers, Authorization: `Bearer ${currentUser.token?.access_token}` };
// }
else {
options.headers = { ...options.headers };
}
// if "API_ACCESS_TOKEN" is defined, ignore "Bearer" token
if (api_key) {
options.headers.Authorization = "";
options.headers = { ...options.headers, "x-api-key": api_key };
}
if (!options.headers.Authorization && !options.headers["x-api-key"] && cachedApiKey) {
options.headers.Authorization = "";
options.headers = { ...options.headers, "x-api-key": cachedApiKey };
}
// Inject "REFRESH_TOKEN" if any
if ((_a = currentUser === null || currentUser === void 0 ? void 0 : currentUser.token) === null || _a === void 0 ? void 0 : _a.refresh_token) {
options.params = { refresh_token: currentUser.token.refresh_token };
}
else if (cachedRefreshToken) {
options.params = { refresh_token: cachedRefreshToken };
}
if (!options.headers["content-type"])
options.headers["content-type"] = "application/json";
if (options.data)
options.data = JSON.stringify(options.data);
if (isDebugging)
console.log("options.url :>> ", options.baseURL + options.url);
if (isDebugging)
console.log("options.params :>> ", options.params);
if (isDebugging)
console.log("options.headers :>> ", options.headers);
if (isDebugging)
console.log("options.data :>> ", options.data);
try {
const { data: responseData } = await (0, axios_1.default)(options);
return responseData;
}
catch (e) {
if (e.toString().indexOf(`ECONNREFUSED`) > -1) {
(0, log_1.logError)(`NETWORK ERROR: Cannot connect to the build server at "${inspector_1.url}".`);
}
else {
(0, log_1.logError)(`${method} - ${options.url} - API ERROR:`, e);
}
return { status: 0, messages: [`Something went wrong: ${e.toString()}`], data: null };
}
}
exports.fetchApi = fetchApi;
exports.default = fetchApi;