@cnbcool/mcp-server
Version:
CNB MCP Server. A comprehensive MCP server that provides seamless integration to the CNB's API(https://cnb.cool), offering a wide range of tools for repository management, pipelines operations and collaboration features
122 lines (121 loc) • 5.29 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 __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CnbRequest = exports.CNB_AXIOS_CANCEL_ERROR_CODE = exports.CNB_AXIOS_ERROR_CODE = void 0;
const header_1 = require("../../../constants/header");
const axios_1 = __importDefault(require("axios"));
exports.CNB_AXIOS_ERROR_CODE = -1;
exports.CNB_AXIOS_CANCEL_ERROR_CODE = -2;
class CnbRequest {
constructor(config) {
this.axiosInstance = axios_1.default.create(config);
this.config = config;
}
formatResponse(res, error) {
var _a, _b;
let status = res === null || res === void 0 ? void 0 : res.status;
let resultData = null;
let resultError = null;
const headers = {};
const responseHeader = (res === null || res === void 0 ? void 0 : res.headers) || ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.headers) || {};
// 根据约定响应头返回响应头数据
if (responseHeader) {
const regexp = new RegExp(`${header_1.API_PRIVATE_RESPONSE_HEADER_PREFIX}(-.+)+`);
const headerKeys = Object.keys(responseHeader);
for (const key of headerKeys) {
if (regexp.test(key) === true) {
headers[key] = responseHeader[key];
}
}
}
const formatError = (status, errData) => {
resultData = null;
resultError = {
code: errData.errcode,
message: errData.errmsg || 'unknown error',
param: errData.errparam || null,
status
};
};
if (error) {
const axiosError = error;
const isAxiosError = 'response' in axiosError;
if (!isAxiosError) {
const errorObj = {
code: (error === null || error === void 0 ? void 0 : error.code) === 'ERR_CANCELED' ? exports.CNB_AXIOS_CANCEL_ERROR_CODE : exports.CNB_AXIOS_ERROR_CODE,
message: error.message || 'unknown error',
status: error === null || error === void 0 ? void 0 : error.status
};
const { config: _, request, ...restErrorInfo } = error;
const errorPrototype = {};
// 在原型对象上定义_error属性,用于存储原始错误的详细信息
Object.defineProperty(errorPrototype, '_error', {
value: restErrorInfo,
enumerable: true,
configurable: true
});
const newError = Object.assign(Object.create(errorPrototype), errorObj);
const errorResponse = {
status,
result: null,
error: newError,
headers
};
return errorResponse;
}
status = error.response.status;
formatError(status, axiosError.response.data);
}
// 基于webapi统一返回进行处理
if (((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.errcode) !== undefined) {
formatError(res.status, res.data);
}
else {
resultData = res ? res.data : null;
}
return { status, result: resultData, error: resultError, headers };
}
async request(config) {
var _a;
const format = (_a = config.options) === null || _a === void 0 ? void 0 : _a.formatResponse;
let result;
try {
const res = await this.axiosInstance.request(config);
result = this.formatResponse(res, null);
if (typeof format === 'function') {
result = format(result, res);
}
}
catch (err) {
result = this.formatResponse(null, err);
if (typeof format === 'function') {
result = format(result, err.response);
}
}
if (result.error) {
const { onError = this.config.onError } = config.options || {};
if (onError !== null && typeof onError === 'function') {
onError === null || onError === void 0 ? void 0 : onError(result.error);
}
}
return result;
}
}
exports.CnbRequest = CnbRequest;
__exportStar(require("./types"), exports);