n8n-nodes-espocrm
Version:
n8n Community Node for EspoCRM
89 lines (88 loc) • 3.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiClient = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class ApiClient {
constructor(context) {
this.baseUrl = '';
this.apiKey = '';
this.context = context;
}
async initialize() {
const credentials = await this.context.getCredentials('espoCRMApi');
this.baseUrl = credentials.url.replace(/\/$/, ''); // Trailing slash entfernen
this.apiKey = credentials.apiKey;
if (!this.baseUrl || !this.apiKey) {
throw new n8n_workflow_1.NodeOperationError(this.context.getNode(), 'EspoCRM credentials are not properly configured. Please check your URL and API Key.');
}
}
async get(endpoint, params) {
return await this.request('GET', endpoint, undefined, params);
}
async post(endpoint, data) {
return await this.request('POST', endpoint, data);
}
async put(endpoint, data) {
return await this.request('PUT', endpoint, data);
}
async delete(endpoint) {
return await this.request('DELETE', endpoint);
}
async request(method, endpoint, data, params) {
const options = {
method,
url: `${this.baseUrl}/api/v1/${endpoint}`,
headers: {
'X-API-KEY': this.apiKey,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
json: true,
resolveWithFullResponse: true,
simple: false,
};
if (data) {
options.body = data;
}
if (params) {
options.qs = params;
}
// Debug Logging für AI Tools
console.log('EspoCRM API Request:', {
method,
url: options.url,
hasData: !!data,
hasParams: !!params,
});
try {
const response = await this.context.helpers.request(options);
// Debug Response
console.log('EspoCRM API Response:', {
statusCode: response.statusCode,
hasBody: !!response.body,
});
if (response.statusCode >= 400) {
let errorMessage = `EspoCRM API Error: ${response.statusCode}`;
if (response.body && typeof response.body === 'object') {
if (response.body.message) {
errorMessage += ` - ${response.body.message}`;
}
else if (response.body.error) {
errorMessage += ` - ${response.body.error}`;
}
}
throw new n8n_workflow_1.NodeOperationError(this.context.getNode(), errorMessage);
}
return response.body;
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeOperationError) {
throw error;
}
// Type-safe Error Message Extraction
const errorMessage = error instanceof Error ? error.message : 'Unknown connection error';
throw new n8n_workflow_1.NodeOperationError(this.context.getNode(), `EspoCRM Connection Error: ${errorMessage}`);
}
}
}
exports.ApiClient = ApiClient;