n8n-nodes-aiscraper
Version:
n8n node to call Parsera API for AI Scraping
173 lines • 7.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareExtractRequestBody = prepareExtractRequestBody;
exports.prepareParseRequestBody = prepareParseRequestBody;
exports.prepareAgentExtractRequestBody = prepareAgentExtractRequestBody;
exports.prepareExtractMarkdownRequestBody = prepareExtractMarkdownRequestBody;
exports.prepareScrapeRequestBody = prepareScrapeRequestBody;
const n8n_workflow_1 = require("n8n-workflow");
const parseAttributes_1 = require("./parseAttributes");
function sanitizeUrl(node, raw, itemIndex) {
if (typeof raw !== 'string' || !raw.trim()) {
throw new n8n_workflow_1.NodeOperationError(node, 'URL is required.', { itemIndex });
}
let url = raw.trim();
if (!/^https?:\/\//i.test(url)) {
url = 'https://' + url;
}
if (/\s/.test(url) || !/^https?:\/\/[^\s/]+\.[^\s/]+/.test(url)) {
throw new n8n_workflow_1.NodeOperationError(node, `Invalid URL: "${raw.trim()}". Please provide a valid URL (e.g. https://example.com).`, { itemIndex });
}
return url;
}
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 (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();
}
body.url = sanitizeUrl(node, body.url, currentItemIndex);
addAttributesToBody(this, body);
addCookiesToBody(this, body);
return requestOptions;
}
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;
}
async function prepareAgentExtractRequestBody(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";
body.url = sanitizeUrl(node, body.url, currentItemIndex);
const prompt = this.getNodeParameter('agentPrompt', currentItemIndex);
if (!prompt || !prompt.trim()) {
throw new n8n_workflow_1.NodeOperationError(node, 'Prompt is required for Agent Extract.', { itemIndex: currentItemIndex });
}
body.prompt = prompt.trim();
const enableColumns = this.getNodeParameter('enableColumns', currentItemIndex);
if (enableColumns) {
const attributesInputMode = this.getNodeParameter('attributesInputMode', currentItemIndex);
let transformedAttributes = [];
if (attributesInputMode === 'fields') {
const attributesFieldsParam = this.getNodeParameter('attributesFields', currentItemIndex);
transformedAttributes = (0, parseAttributes_1.parseAttributesFromFields)(this, attributesFieldsParam);
}
else if (attributesInputMode === 'json') {
const attributesJsonParam = this.getNodeParameter('attributesJson', currentItemIndex);
transformedAttributes = (0, parseAttributes_1.parseAttributesFromJson)(this, attributesJsonParam);
}
if (transformedAttributes.length > 0) {
body.attributes = transformedAttributes;
}
}
return requestOptions;
}
async function prepareExtractMarkdownRequestBody(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";
body.url = sanitizeUrl(node, body.url, currentItemIndex);
return requestOptions;
}
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 existingScraperNameFromBody = body.name;
if (typeof existingScraperNameFromBody !== 'string' || !existingScraperNameFromBody.trim()) {
throw new n8n_workflow_1.NodeOperationError(node, 'Existing Scraper Name is required for Existing Scraper Scrape operation.', { itemIndex: currentItemIndex });
}
const templateId = existingScraperNameFromBody.trim();
delete body.name;
body.template_id = templateId;
if (body.url && typeof body.url === 'string') {
body.url = body.url.trim();
}
addCookiesToBody(this, body);
return requestOptions;
}
//# sourceMappingURL=preSend.js.map