@kaia-team/n8n-nodes-kaia
Version:
n8n nodes for Kaia LLM integration
259 lines • 8.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.contentsApiProperties = void 0;
exports.handleContentsApi = handleContentsApi;
const n8n_workflow_1 = require("n8n-workflow");
const utils_1 = require("../utils");
/**
* Contents API Properties
* Properties specific to the contents API resource (content management)
*/
exports.contentsApiProperties = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['contents'],
},
},
options: [
{
name: 'Create',
value: 'create',
action: 'Create a content',
},
{
name: 'Delete',
value: 'delete',
action: 'Delete a content',
},
{
name: 'List',
value: 'list',
action: 'List contents',
},
{
name: 'Read',
value: 'read',
action: 'Read a content',
},
{
name: 'Update',
value: 'update',
action: 'Update a content',
},
],
default: 'list',
},
{
displayName: 'Content ID',
name: 'contentId',
type: 'string',
default: '',
displayOptions: {
show: {
resource: ['contents'],
operation: ['read', 'update', 'delete'],
},
},
description: 'The ID of the content',
required: true,
},
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
displayOptions: {
show: {
resource: ['contents'],
operation: ['create', 'update'],
},
},
description: 'Title of the content',
},
{
displayName: 'Summary',
name: 'summary',
type: 'string',
typeOptions: { rows: 3 },
default: '',
displayOptions: {
show: {
resource: ['contents'],
operation: ['create', 'update'],
},
},
description: 'Summary of the content',
},
{
displayName: 'Body',
name: 'body',
type: 'string',
typeOptions: { rows: 4 },
default: '',
displayOptions: {
show: {
resource: ['contents'],
operation: ['create', 'update'],
},
},
description: 'Body content',
},
{
displayName: 'Headline Image',
name: 'headline_image',
type: 'string',
default: '',
displayOptions: {
show: {
resource: ['contents'],
operation: ['create', 'update'],
},
},
description: 'Path to headline image file or base64 data',
},
{
displayName: 'Extras',
name: 'extras',
type: 'string',
typeOptions: { rows: 3 },
default: '',
displayOptions: {
show: {
resource: ['contents'],
operation: ['create', 'update'],
},
},
description: 'JSON object with additional data',
},
{
displayName: 'Feed ID',
name: 'feed_id',
type: 'string',
default: '',
displayOptions: {
show: {
resource: ['contents'],
operation: ['list'],
},
},
description: 'Filter contents by feed ID',
},
];
/**
* Contents API Handler
* Handles requests to the contents API (content management)
*/
async function handleContentsApi(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 qs = {};
const feedId = executeFunctions.getNodeParameter('feed_id', itemIndex, '');
if (feedId)
qs.feed_id = feedId;
const response = await executeFunctions.helpers.httpRequest({
method: 'GET',
url: `${adminUrl}/contents.json`,
headers,
qs,
});
return executeFunctions.helpers.returnJsonArray(response);
}
else if (operation === 'read') {
let contentId = executeFunctions.getNodeParameter('contentId', itemIndex);
contentId = (0, utils_1.extractIdFromUrlOrId)(contentId, 'contents');
const response = await executeFunctions.helpers.httpRequest({
method: 'GET',
url: `${adminUrl}/contents/${contentId}.json`,
headers,
});
return response;
}
else if (operation === 'create' || operation === 'update') {
const payload = { content: {} };
const addIfSet = (key, param, defaultVal = '') => {
const val = executeFunctions.getNodeParameter(param, itemIndex, defaultVal);
if (val !== defaultVal && (typeof val !== 'string' || val.trim() !== '')) {
payload.content[key] = val;
}
};
addIfSet('title', 'title');
addIfSet('summary', 'summary');
addIfSet('body', 'body');
// parse extras as JSON
const extras = executeFunctions.getNodeParameter('extras', itemIndex, '');
if (extras) {
try {
payload.content['extras'] = JSON.parse(extras);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Invalid JSON in extras field: ${extras}`);
}
}
// Handle headline image with FormData if set
const headlineImage = executeFunctions.getNodeParameter('headline_image', itemIndex, '');
const hasHeadlineImage = headlineImage.trim() !== '';
if (hasHeadlineImage) {
const formData = new FormData();
Object.keys(payload.content).forEach(key => {
formData.append(`content[${key}]`, payload.content[key]);
});
formData.append('content[headline_image]', headlineImage);
headers['Content-Type'] = 'multipart/form-data';
const method = operation === 'create' ? 'POST' : 'PATCH';
let url;
if (operation === 'create') {
url = `${adminUrl}/contents.json`;
}
else {
let contentId = executeFunctions.getNodeParameter('contentId', itemIndex);
contentId = (0, utils_1.extractIdFromUrlOrId)(contentId, 'contents');
url = `${adminUrl}/contents/${contentId}.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}/contents.json`;
}
else {
let contentId = executeFunctions.getNodeParameter('contentId', itemIndex);
contentId = (0, utils_1.extractIdFromUrlOrId)(contentId, 'contents');
url = `${adminUrl}/contents/${contentId}.json`;
}
const response = await executeFunctions.helpers.httpRequest({
method: method,
url,
body: payload,
headers,
});
return response;
}
}
else if (operation === 'delete') {
let contentId = executeFunctions.getNodeParameter('contentId', itemIndex);
contentId = (0, utils_1.extractIdFromUrlOrId)(contentId, 'contents');
const response = await executeFunctions.helpers.httpRequest({
method: 'DELETE',
url: `${adminUrl}/contents/${contentId}.json`,
headers,
});
return { success: true, deletedId: contentId };
}
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown contents operation: ${operation}`);
}
//# sourceMappingURL=contentsApi.js.map