UNPKG

@kaia-team/n8n-nodes-kaia

Version:
137 lines 4.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractIdFromUrlOrId = extractIdFromUrlOrId; exports.extractCustomHeaders = extractCustomHeaders; exports.createHeaders = createHeaders; exports.parseErrorData = parseErrorData; exports.handleHttpError = handleHttpError; const n8n_workflow_1 = require("n8n-workflow"); /** * Extract ID from URL or ID string * @param input - URL or ID string * @param resource - Resource name to look for in URL * @returns Extracted ID */ function extractIdFromUrlOrId(input, resource) { if (input.includes('/')) { const parts = input.split('/'); const idIndex = parts.indexOf(resource) + 1; if (idIndex > 0 && idIndex < parts.length) { let id = parts[idIndex]; if (id.endsWith('.json')) id = id.slice(0, -5); return id; } } return input; } /** * Extract custom headers from additionalFields * @param additionalFields - Additional fields object * @returns Object containing custom headers */ function extractCustomHeaders(additionalFields) { const customHeaders = {}; if (additionalFields.headers) { const headerParameters = additionalFields.headers .headerParameters; if (headerParameters) { for (const header of headerParameters) { if (header.name && header.value) { customHeaders[header.name] = header.value; } } } } return customHeaders; } /** * Create headers with custom headers applied * @param adminToken - Admin token for authentication * @param additionalFields - Additional fields containing custom headers * @returns Headers object */ function createHeaders(adminToken, additionalFields) { const headers = { 'Content-Type': 'application/json', 'X-User-Token': adminToken, }; if (additionalFields) { const customHeaders = extractCustomHeaders(additionalFields); for (const [name, value] of Object.entries(customHeaders)) { headers[name] = value; } } return headers; } /** * Parse structured error data from error messages * @param errorMessage - The error message that may contain structured error data * @returns Parsed error data object or null if no structured data found */ function parseErrorData(errorMessage) { const errorDataMatch = errorMessage.match(/\| ERROR_DATA: (.+)$/); if (errorDataMatch) { try { return JSON.parse(errorDataMatch[1]); } catch (e) { return null; } } return null; } /** * Handle HTTP request errors and throw NodeOperationError with detailed information * @param error - The error thrown by httpRequest * @param node - The node instance for creating NodeOperationError * @returns Never returns (always throws) */ function handleHttpError(error, node) { // Extract the actual error response from the API if (error.response && error.response.body) { // Create detailed error message const statusCode = error.response.statusCode; const errorBody = error.response.body; // Format the error message let errorMessage = `Request failed with status code ${statusCode}`; // Add detailed error information if available if (typeof errorBody === 'object') { if (errorBody.errors) { // Handle validation errors const validationErrors = Object.entries(errorBody.errors) .map(([field, messages]) => `${field}: ${Array.isArray(messages) ? messages.join(', ') : messages}`) .join('; '); errorMessage += ` - Validation errors: ${validationErrors}`; } else if (errorBody.message) { errorMessage += ` - ${errorBody.message}`; } else if (errorBody.error) { errorMessage += ` - ${errorBody.error}`; } } else if (typeof errorBody === 'string') { errorMessage += ` - ${errorBody}`; } // Add structured error data to the message for easy parsing if (typeof errorBody === 'object') { errorMessage += ` | ERROR_DATA: ${JSON.stringify({ statusCode, responseBody: errorBody, timestamp: new Date().toISOString() })}`; } // Create NodeOperationError with detailed information throw new n8n_workflow_1.NodeOperationError(node || {}, errorMessage); } else if (error.message) { // Fallback to the error message if no response body throw new n8n_workflow_1.NodeOperationError(node || {}, error.message); } else { // Last resort fallback throw new n8n_workflow_1.NodeOperationError(node || {}, 'Unknown error occurred'); } } //# sourceMappingURL=utils.js.map