UNPKG

n8n-bookstack-agent-tool

Version:

Comprehensive BookStack API integration tool for n8n AI Agent with MCP framework compatibility

138 lines 5.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BookStackClient = void 0; const axios_1 = __importDefault(require("axios")); class BookStackClient { constructor(config) { this.config = config; this.axiosInstance = axios_1.default.create({ baseURL: config.baseUrl, timeout: config.timeout || 30000, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', ...this.getAuthHeaders() } }); this.setupInterceptors(); } getAuthHeaders() { const headers = {}; if (this.config.authType === 'bearer' && this.config.token) { if (this.config.tokenSecret) { headers['Authorization'] = `Token ${this.config.token}:${this.config.tokenSecret}`; } else { headers['Authorization'] = `Token ${this.config.token}`; } } else if (this.config.authType === 'session' && this.config.sessionCookie) { headers['Cookie'] = this.config.sessionCookie; } return headers; } setupInterceptors() { this.axiosInstance.interceptors.request.use((config) => { console.log(`BookStack API Request: ${config.method?.toUpperCase()} ${config.url}`); console.log(`BookStack API Headers:`, JSON.stringify(config.headers, null, 2)); return config; }, (error) => { console.error('BookStack API Request Error:', error); return Promise.reject(error); }); this.axiosInstance.interceptors.response.use((response) => { console.log(`BookStack API Response: ${response.status} ${response.config.url}`); return response; }, (error) => { console.error('BookStack API Response Error:', error.response?.status, error.response?.data); return Promise.reject(this.handleApiError(error)); }); } handleApiError(error) { if (error.response) { const status = error.response.status; const message = error.response.data?.message || error.response.statusText; switch (status) { case 401: return new Error(`Authentication failed: ${message}`); case 403: return new Error(`Access forbidden: ${message}`); case 404: return new Error(`Resource not found: ${message}`); case 422: return new Error(`Validation error: ${message}`); case 429: return new Error(`Rate limit exceeded: ${message}`); case 500: return new Error(`Server error: ${message}`); default: return new Error(`API error (${status}): ${message}`); } } else if (error.request) { return new Error('Network error: No response received from BookStack API'); } else { return new Error(`Request error: ${error.message}`); } } buildUrl(path, params = {}) { let url = path; const pathParams = {}; const queryParams = {}; Object.entries(params).forEach(([key, value]) => { if (url.includes(`{${key}}`)) { pathParams[key] = value; url = url.replace(`{${key}}`, String(value)); } else { queryParams[key] = value; } }); const filteredParams = Object.entries(queryParams) .filter(([_, value]) => value !== undefined && value !== null) .reduce((acc, [key, value]) => { acc[key] = String(value); return acc; }, {}); const queryString = new URLSearchParams(filteredParams).toString(); return queryString ? `${url}?${queryString}` : url; } async get(path, params = {}) { const url = this.buildUrl(path, params); const response = await this.axiosInstance.get(url); return response.data; } async post(path, params = {}) { const { id, ...pathParams } = params; const url = this.buildUrl(path, { id }); const response = await this.axiosInstance.post(url, pathParams); return response.data; } async put(path, params = {}) { const { id, ...pathParams } = params; const url = this.buildUrl(path, { id }); const response = await this.axiosInstance.put(url, pathParams); return response.data; } async delete(path, params = {}) { const url = this.buildUrl(path, params); const response = await this.axiosInstance.delete(url); return response.data; } async testConnection() { try { await this.get('/api/docs'); return true; } catch (error) { console.error('BookStack connection test failed:', error); return false; } } } exports.BookStackClient = BookStackClient; //# sourceMappingURL=client.js.map