UNPKG

@zambelz/zhc

Version:
79 lines 3.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRestApiList = void 0; const node_path_1 = __importDefault(require("node:path")); const node_fs_1 = __importDefault(require("node:fs")); const chalk_1 = __importDefault(require("chalk")); const config_1 = require("../../../utils/config"); const global_1 = require("../../../utils/global"); const logger_1 = require("../../../utils/logger"); const common_1 = require("../../../utils/common"); const fileOperation_1 = require("../../../utils/fileOperation"); const endpointInfo = (method, path) => { switch (method) { case "GET": return chalk_1.default.hex("#6fa8fc")(`[${method}] ${path}`); case "PUT": return chalk_1.default.hex("#fb983c")(`[${method}] ${path}`); case "POST": return chalk_1.default.hex("#49f176")(`[${method}] ${path}`); case "DELETE": return chalk_1.default.hex("#f73a3d")(`[${method}] ${path}`); case "OPTIONS": return chalk_1.default.hex("#348EEE")(`[${method}] ${path}`); case "HEAD": return chalk_1.default.hex("#8522f6")(`[${method}] ${path}`); case "PATCH": return chalk_1.default.hex("#49ddba")(`[${method}] ${path}`); } }; const restApiInfo = (filePath, tabCount = 0) => { const fileLists = node_fs_1.default.readdirSync(filePath); const space = (0, common_1.spacer)(tabCount); for (let file of fileLists) { const isDir = (0, fileOperation_1.isDirectory)(node_path_1.default.join(filePath, file)); if (isDir) { const dirName = chalk_1.default.yellow.bold(file); console.log(`${space}\u2022 ${dirName}:`); restApiInfo(node_path_1.default.join(filePath, file), tabCount + 1); } else { const endpointFilePath = node_path_1.default.join(filePath, file || "default"); const endpointContent = node_fs_1.default.readFileSync(endpointFilePath, "utf-8"); if (!endpointContent || endpointContent.length === 0) { continue; } const formattedFileName = chalk_1.default.yellow.bold(file.replace(".jsonc", "")); console.log(`${space}\u2022 ${formattedFileName}:`); const parsedContent = (0, common_1.parseContent)(endpointContent).parsed; const apiList = Object.entries(parsedContent); for (const [key, value] of apiList) { const formattedKey = chalk_1.default.bold(key); // @ts-ignore const info = endpointInfo(value.method, value.path); console.log(`${(0, common_1.spacer)(tabCount + 1)}\u2022 ${formattedKey}: ${info}`); } } } }; const getRestApiList = (options) => { try { const profile = options.profile; const configData = (0, config_1.getConfigData)(); const targetProfile = profile || configData?.defaultProfile; const endpointsDirPath = node_path_1.default.join(global_1.PROFILES_PATH, targetProfile, "endpoints"); console.log(); console.log(`API List (${chalk_1.default.yellow(targetProfile)})`); console.log(); restApiInfo(endpointsDirPath); } catch (err) { (0, logger_1.logError)(`${err}`); return undefined; } }; exports.getRestApiList = getRestApiList; //# sourceMappingURL=restApiManagement.js.map