UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

60 lines (59 loc) 2.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.dxApi = void 0; const axios_1 = __importDefault(require("axios")); const chalk_1 = __importDefault(require("chalk")); const lodash_1 = require("lodash"); const app_config_1 = require("../../app.config"); async function dxApi(options) { var _a, _b; const { method, dxKey } = options; // timeout: 30s options.timeout = 30000; if ((0, lodash_1.isEmpty)(options.headers)) options.headers = {}; // console.log("DXKEY :", dxKey); options.baseURL = app_config_1.Config.DX_API_URL; // options.headers.Authorization = `Bearer ${licenseKey}`; if (dxKey) options.headers["X-API-Key"] = dxKey; // console.log("HEADER OPTIONS:", options.headers["X-API-Key"]); if (["POST", "PATCH", "DELETE"].includes(method === null || method === void 0 ? void 0 : method.toUpperCase())) { if ((0, lodash_1.isEmpty)(options.headers["content-type"])) options.headers["content-type"] = "application/json"; } if (options.isDebugging && options.data) { console.log(chalk_1.default.yellow("dxApi() >"), `${app_config_1.Config.DX_API_URL}${options.url} > options.data :>>`); console.dir(options.data, { depth: 10 }); } if (options.isDebugging) console.log(chalk_1.default.yellow("dxApi() >"), `${app_config_1.Config.DX_API_URL}${options.url} > options.headers :>>`, options.headers); try { const res = await (0, axios_1.default)(options); const { data } = res; if (options.isDebugging) console.log(chalk_1.default.yellow("dxApi() >"), `${app_config_1.Config.DX_API_URL}${options.url} > response :>>`, data); return { status: 1, data, messages: [] }; } catch (e) { console.log(chalk_1.default.yellow("dxApi() > Error :>>"), e); const err = ((_a = e.data) === null || _a === void 0 ? void 0 : _a.message) === "UNAUTHORIZED" || ((_b = e.data) === null || _b === void 0 ? void 0 : _b.status) === 401 ? "UNAUTHORIZED." : e.message; return { status: 0, messages: [`${err}`] }; // retry with backup url // options.baseURL = Config.DX_SITE_BACKUP_URL; // try { // const res = await axios(options); // const { data: responseData } = res; // if (options.isDebugging) // console.log(chalk.yellow("[BACKUP] dxApi() >"), `${Config.DX_SITE_BACKUP_URL}${options.url} > response :>>`, responseData); // } catch (e2) { // // throw official error // const err: string = e.data?.message === "UNAUTHORIZED" || e.data?.status === 401 ? "UNAUTHORIZED." : e.message; // return { status: 0, messages: [`${err}`] } as T; // } } } exports.dxApi = dxApi;