@brave/n8n-nodes-brave-search
Version:
A n8n node for the Brave Search API
91 lines • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BraveSearch = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const operations_1 = require("./operations");
class BraveSearch {
constructor() {
this.description = {
displayName: 'Brave Search',
name: 'braveSearch',
subtitle: '={{$parameter["operation"]}}',
icon: 'file:braveSearch.svg',
group: ['transform'],
version: 1,
description: 'Search the web using Brave Search',
defaults: {
name: 'Brave Search',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'braveSearchApi',
required: true,
},
],
usableAsTool: true,
properties: operations_1.PROPERTIES,
};
}
async execute() {
const items = this.getInputData();
const raw_results = [];
for (let i = 0; i < items.length; i++) {
try {
raw_results.push(await BraveSearch.performRequest(this, i));
}
catch (error) {
if (this.continueOnFail()) {
raw_results.push({ json: items[i].json, pairedItem: i, error });
continue;
}
if (error.context) {
error.context.itemIndex = i;
throw error;
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { itemIndex: i });
}
}
return [this.helpers.returnJsonArray(raw_results)];
}
static buildParams(ctx, operation, index) {
var _a;
const params = {};
for (const { name, type } of operation.parameters) {
const nodeParam = ctx.getNodeParameter(name, index);
if (type === 'collection' && name === 'additionalParameters') {
const additional_parameters = (_a = ctx.getNodeParameter('additionalParameters', index)) !== null && _a !== void 0 ? _a : {};
Object.entries(additional_parameters).forEach(([key, value]) => {
if (key === 'result_filter')
value = value.join(',');
if (key === 'goggles') {
const [url] = value.flat();
if (url === null || url === void 0 ? void 0 : url.trim().length)
value = url;
else
return;
}
params[key] = value;
});
}
else {
params[name] = nodeParam;
}
}
return params;
}
static async performRequest(ctx, index) {
const operation = operations_1.OPERATIONS[ctx.getNodeParameter('operation', index)];
const params = BraveSearch.buildParams(ctx, operation, index);
const response = await ctx.helpers.httpRequestWithAuthentication.call(ctx, 'braveSearchApi', {
url: `https://api.search.brave.com/res/v1${operation.endpoint}`,
qs: operation.buildQuery(params),
returnFullResponse: true,
json: true,
});
return response.body;
}
}
exports.BraveSearch = BraveSearch;
//# sourceMappingURL=BraveSearch.node.js.map