n8n-nodes-aiscraper
Version:
n8n node to call Parsera API for AI Scraping
120 lines • 5.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareScrapeRequestBody = exports.prepareParseRequestBody = exports.prepareExtractRequestBody = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const parseAttributes_1 = require("./parseAttributes");
function addAttributesToBody(context, body) {
const currentItemIndex = context.getItemIndex();
const node = context.getNode();
const attributesInputMode = context.getNodeParameter('attributesInputMode', currentItemIndex);
let transformedAttributes;
switch (attributesInputMode) {
case 'fields':
const attributesFieldsParam = context.getNodeParameter('attributesFields', currentItemIndex);
transformedAttributes = (0, parseAttributes_1.parseAttributesFromFields)(context, attributesFieldsParam);
break;
case 'json':
const attributesJsonParam = context.getNodeParameter('attributesJson', currentItemIndex);
transformedAttributes = (0, parseAttributes_1.parseAttributesFromJson)(context, attributesJsonParam);
break;
default:
const exhaustiveCheck = attributesInputMode;
throw new n8n_workflow_1.NodeOperationError(node, `Internal error: Unhandled attributes input mode '${exhaustiveCheck}'.`, { itemIndex: currentItemIndex });
}
if (Object.keys(transformedAttributes).length === 0) {
throw new n8n_workflow_1.NodeOperationError(node, 'At least one attribute is required.', { itemIndex: currentItemIndex });
}
body.attributes = transformedAttributes;
}
function addCookiesToBody(context, body) {
const currentItemIndex = context.getItemIndex();
const node = context.getNode();
const cookiesStringParam = context.getNodeParameter('cookies', currentItemIndex);
if (cookiesStringParam && cookiesStringParam.trim() !== '') {
let parsedCookies;
try {
parsedCookies = JSON.parse(cookiesStringParam);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(node, `Invalid JSON in Cookies field: ${error.message}`, { itemIndex: currentItemIndex });
}
if (!Array.isArray(parsedCookies)) {
throw new n8n_workflow_1.NodeOperationError(node, 'Cookies field must be a JSON array.', { itemIndex: currentItemIndex });
}
if (parsedCookies.length === 0) {
body.cookies = null;
}
else {
body.cookies = parsedCookies;
}
}
else {
body.cookies = null;
}
}
async function prepareExtractRequestBody(requestOptions) {
const node = this.getNode();
const currentItemIndex = this.getItemIndex();
if (!requestOptions.body || typeof requestOptions.body !== 'object') {
requestOptions.body = {};
}
const body = requestOptions.body;
body.source = "n8n";
const prompt = this.getNodeParameter('prompt', currentItemIndex);
if (prompt && prompt.trim() !== '') {
body.prompt = prompt.trim();
}
const urlFromBody = body.url;
if (typeof urlFromBody !== 'string' || !urlFromBody.trim()) {
throw new n8n_workflow_1.NodeOperationError(node, 'URL is required.', { itemIndex: currentItemIndex });
}
body.url = urlFromBody.trim();
addAttributesToBody(this, body);
addCookiesToBody(this, body);
return requestOptions;
}
exports.prepareExtractRequestBody = prepareExtractRequestBody;
async function prepareParseRequestBody(requestOptions) {
const node = this.getNode();
const currentItemIndex = this.getItemIndex();
if (!requestOptions.body || typeof requestOptions.body !== 'object') {
requestOptions.body = {};
}
const body = requestOptions.body;
body.source = "n8n";
const prompt = this.getNodeParameter('prompt', currentItemIndex);
if (prompt && prompt.trim() !== '') {
body.prompt = prompt.trim();
}
const contentFromBody = body.content;
if (typeof contentFromBody !== 'string' || !contentFromBody.trim()) {
throw new n8n_workflow_1.NodeOperationError(node, 'Content is required for Parse HTML.', { itemIndex: currentItemIndex });
}
body.content = contentFromBody.trim();
addAttributesToBody(this, body);
return requestOptions;
}
exports.prepareParseRequestBody = prepareParseRequestBody;
async function prepareScrapeRequestBody(requestOptions) {
const node = this.getNode();
const currentItemIndex = this.getItemIndex();
if (!requestOptions.body || typeof requestOptions.body !== 'object') {
requestOptions.body = {};
}
const body = requestOptions.body;
body.source = "n8n";
const agentNameFromBody = body.name;
if (typeof agentNameFromBody !== 'string' || !agentNameFromBody.trim()) {
throw new n8n_workflow_1.NodeOperationError(node, 'Agent Name is required for Agent Scrape operation.', { itemIndex: currentItemIndex });
}
body.name = agentNameFromBody.trim();
const urlFromBody = body.url;
if (typeof urlFromBody !== 'string' || !urlFromBody.trim()) {
throw new n8n_workflow_1.NodeOperationError(node, 'URL is required for Agent Scrape operation.', { itemIndex: currentItemIndex });
}
body.url = urlFromBody.trim();
addCookiesToBody(this, body);
return requestOptions;
}
exports.prepareScrapeRequestBody = prepareScrapeRequestBody;
//# sourceMappingURL=preSend.js.map