UNPKG

n8n-nodes-memory-box

Version:
74 lines 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.memoryBoxApiRequest = memoryBoxApiRequest; exports.formatDateForMemoryBox = formatDateForMemoryBox; exports.validateAndFormatMemory = validateAndFormatMemory; exports.extractItems = extractItems; const n8n_workflow_1 = require("n8n-workflow"); /** * Make an API request to Memory Box */ async function memoryBoxApiRequest(method, endpoint, body = {}, qs = {}, uri) { const credentials = await this.getCredentials('memoryBoxApi'); // Ensure the URL ends with a slash let apiUrl = credentials.apiUrl; if (!apiUrl.endsWith('/')) { apiUrl += '/'; } // Build full API path const path = uri || `v2/${endpoint}`; const url = `${apiUrl}${path}`; const options = { method, body, qs, url, json: true, headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${credentials.token}`, }, }; try { return await this.helpers.httpRequest(options); } catch (error) { // Handle the error with proper typing const errorData = error; throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: errorData.message || 'Unknown error occurred' }); } } /** * Format a date for Memory Box (YYYY-MM-DD) */ function formatDateForMemoryBox(date) { return date.toISOString().split('T')[0]; // YYYY-MM-DD format } /** * Check if memory already has a date prefix, if not add it */ function validateAndFormatMemory(text) { // Check if memory already has a date prefix if (!text.match(/^\d{4}-\d{2}-\d{2}/)) { const today = new Date(); const formattedDate = formatDateForMemoryBox(today); return `${formattedDate}\n\n${text}`; } return text; } /** * Extract memory items from Memory Box response */ function extractItems(response) { // Handle the new response format with items property if (response && typeof response === 'object' && response.items && Array.isArray(response.items)) { return response.items; } // Handle legacy array format if (Array.isArray(response)) { return response; } // Return empty array as fallback return []; } //# sourceMappingURL=GenericFunctions.js.map