@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
50 lines (49 loc) • 1.58 kB
JavaScript
export default class CnbApiClient {
static instance = null;
_baseUrl;
_token;
constructor(options) {
this._baseUrl = options.baseUrl;
this._token = options.token;
}
get baseUrl() {
return this._baseUrl;
}
static initialize(options) {
if (!CnbApiClient.instance) {
CnbApiClient.instance = new CnbApiClient(options);
}
}
static getInstance() {
if (!CnbApiClient.instance) {
throw new Error('CnbApiClient not initialized. Call CnbApiClient.initialize(baseUrl, token) first.');
}
return CnbApiClient.instance;
}
async request(method, path, body, config, responseType = 'json' // 'json' | 'text' | 'raw'
) {
const url = `${this._baseUrl}${path}`;
const headers = {
Authorization: `Bearer ${this._token}`,
Accept: 'application/vnd.cnb.api+json',
...(config?.header || {})
};
const options = {
method,
headers,
body: body ? JSON.stringify(body) : undefined
};
const response = await fetch(url, options);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`API request failed: ${response.status} \n${response.headers.get('traceparent')} \n${errorText} )`);
}
if (responseType === 'raw') {
return response;
}
if (responseType === 'text') {
return response.text();
}
return response.json();
}
}