UNPKG

@kaia-team/n8n-nodes-kaia

Version:
368 lines 11.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.documentsApiProperties = void 0; exports.handleDocumentsApi = handleDocumentsApi; const n8n_workflow_1 = require("n8n-workflow"); const utils_1 = require("../utils"); /** * Documents API Properties * Properties specific to the documents API resource */ exports.documentsApiProperties = [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['documents'], }, }, options: [ { name: 'Create', value: 'create', action: 'Create a document', }, { name: 'Delete', value: 'delete', action: 'Delete a document', }, { name: 'List', value: 'list', action: 'List documents', }, { name: 'Read', value: 'read', action: 'Read a document', }, { name: 'Shards', value: 'shards', description: 'Get document shards', action: 'Get document shards', }, { name: 'Update', value: 'update', action: 'Update a document', }, ], default: 'list', }, { displayName: 'Document ID', name: 'documentId', type: 'string', default: '', displayOptions: { show: { resource: ['documents'], operation: ['read', 'update', 'delete', 'shards'], }, }, required: true, }, { displayName: 'Title', name: 'title', type: 'string', default: '', displayOptions: { show: { resource: ['documents'], operation: ['create', 'update'], }, }, }, { displayName: 'Body', name: 'body', type: 'string', typeOptions: { rows: 4 }, default: '', displayOptions: { show: { resource: ['documents'], operation: ['create', 'update'], }, }, }, { displayName: 'Chunk Length', name: 'chunk_length', type: 'number', default: 0, displayOptions: { show: { resource: ['documents'], operation: ['create', 'update'], }, }, }, { displayName: 'Chunk Overlap', name: 'chunk_overlap', type: 'number', default: 0, displayOptions: { show: { resource: ['documents'], operation: ['create', 'update'], }, }, }, { displayName: 'Dividers', name: 'dividers', type: 'string', default: '', displayOptions: { show: { resource: ['documents'], operation: ['create', 'update'], }, }, }, { displayName: 'Worker ID', name: 'worker_id', type: 'string', default: '', displayOptions: { show: { resource: ['documents'], operation: ['create', 'update'], }, }, description: 'Optional for API use', }, { displayName: 'File', name: 'file', type: 'string', default: '', displayOptions: { show: { resource: ['documents'], operation: ['create', 'update'], }, }, description: 'Path to file or base64 data', }, // Properties for shards operation (based on handler usage) { displayName: 'User ID', name: 'user_id', type: 'string', default: '', displayOptions: { show: { resource: ['documents'], operation: ['shards'], }, }, description: 'Filter shards by user ID', }, { displayName: 'Page', name: 'page', type: 'number', default: 1, displayOptions: { show: { resource: ['documents'], operation: ['shards'], }, }, description: 'Page number for pagination', }, { displayName: 'Per Page', name: 'per_page', type: 'number', default: 100, displayOptions: { show: { resource: ['documents'], operation: ['shards'], }, }, description: 'Number of items per page', }, // Additional Fields for custom headers { displayName: 'Additional Fields', name: 'additionalFields', type: 'collection', placeholder: 'Add Field', default: {}, displayOptions: { show: { resource: ['documents'], }, }, options: [ { displayName: 'Headers', name: 'headers', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Header', default: {}, options: [ { name: 'headerParameters', displayName: 'Headers', values: [ { displayName: 'Name', name: 'name', type: 'string', default: '', description: 'Name of the header', }, { displayName: 'Value', name: 'value', type: 'string', default: '', description: 'Value of the header', }, ], }, ], }, ], }, ]; /** * Documents API Handler * Handles requests to the documents API */ async function handleDocumentsApi(executeFunctions, itemIndex, adminUrl, adminToken) { const operation = executeFunctions.getNodeParameter('operation', itemIndex); const additionalFields = executeFunctions.getNodeParameter('additionalFields', itemIndex, {}); const headers = (0, utils_1.createHeaders)(adminToken, additionalFields); if (operation === 'list') { const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/documents.json`, headers, }); return executeFunctions.helpers.returnJsonArray(response); } else if (operation === 'read') { let documentId = executeFunctions.getNodeParameter('documentId', itemIndex); documentId = (0, utils_1.extractIdFromUrlOrId)(documentId, 'documents'); const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/documents/${documentId}.json`, headers, }); return response; } else if (operation === 'create' || operation === 'update') { const payload = { document: {} }; const addIfSet = (key, param, defaultVal = '') => { const val = executeFunctions.getNodeParameter(param, itemIndex, defaultVal); if (val !== defaultVal && (typeof val !== 'string' || val.trim() !== '')) { payload.document[key] = val; } }; addIfSet('title', 'title'); addIfSet('body', 'body'); addIfSet('chunk_length', 'chunk_length', 0); addIfSet('chunk_overlap', 'chunk_overlap', 0); addIfSet('dividers', 'dividers'); addIfSet('worker_id', 'worker_id'); addIfSet('file', 'file'); // Handle file with FormData if set const file = executeFunctions.getNodeParameter('file', itemIndex, ''); const hasFile = file.trim() !== ''; if (hasFile) { const formData = new FormData(); Object.keys(payload.document).forEach(key => { if (key !== 'file') formData.append(`document[${key}]`, payload.document[key]); }); formData.append('document[file]', file); headers['Content-Type'] = 'multipart/form-data'; const method = operation === 'create' ? 'POST' : 'PATCH'; let url; if (operation === 'create') { url = `${adminUrl}/documents.json`; } else { let documentId = executeFunctions.getNodeParameter('documentId', itemIndex); documentId = (0, utils_1.extractIdFromUrlOrId)(documentId, 'documents'); url = `${adminUrl}/documents/${documentId}.json`; } const response = await executeFunctions.helpers.httpRequest({ method: method, url, body: formData, headers, }); return response; } else { const method = operation === 'create' ? 'POST' : 'PATCH'; let url; if (operation === 'create') { url = `${adminUrl}/documents.json`; } else { let documentId = executeFunctions.getNodeParameter('documentId', itemIndex); documentId = (0, utils_1.extractIdFromUrlOrId)(documentId, 'documents'); url = `${adminUrl}/documents/${documentId}.json`; } const response = await executeFunctions.helpers.httpRequest({ method: method, url, body: payload, headers, }); return response; } } else if (operation === 'delete') { let documentId = executeFunctions.getNodeParameter('documentId', itemIndex); documentId = (0, utils_1.extractIdFromUrlOrId)(documentId, 'documents'); const response = await executeFunctions.helpers.httpRequest({ method: 'DELETE', url: `${adminUrl}/documents/${documentId}.json`, headers, }); return { success: true, deletedId: documentId }; } else if (operation === 'shards') { let documentId = executeFunctions.getNodeParameter('documentId', itemIndex); documentId = (0, utils_1.extractIdFromUrlOrId)(documentId, 'documents'); const qs = { document_id: documentId }; const userId = executeFunctions.getNodeParameter('user_id', itemIndex, ''); if (userId) qs.user_id = userId; const page = executeFunctions.getNodeParameter('page', itemIndex, 1); if (page) qs.page = page; const perPage = executeFunctions.getNodeParameter('per_page', itemIndex, 100); if (perPage) qs.per_page = perPage; const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/document_shards.json`, headers, qs, }); return executeFunctions.helpers.returnJsonArray(response); } throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown documents operation: ${operation}`); } //# sourceMappingURL=documentsApi.js.map