n8n-nodes-google-custom-search
Version:
Google Custom Search node for n8n
101 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleCustomSearch = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class GoogleCustomSearch {
constructor() {
this.description = {
displayName: 'Google Custom Search',
name: 'googleCustomSearch',
icon: 'file:google.svg',
group: ['transform'],
version: 1,
description: 'Search using Google Custom Search API',
defaults: {
name: 'Google Custom Search',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'googleCustomSearchApi',
required: true,
},
],
properties: [
{
displayName: 'Search Query',
name: 'q',
type: 'string',
default: '',
required: true,
description: 'Query string to search for',
},
{
displayName: 'Country Code (gl)',
name: 'gl',
type: 'string',
default: 'ir',
required: false,
description: 'Geolocation of end user (default: ir for Iran)',
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials('googleCustomSearchApi');
if (!credentials) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No credentials found!');
}
const apiKey = credentials.apiKey;
const searchEngineId = credentials.searchEngineId;
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
// Get parameters
const query = this.getNodeParameter('q', itemIndex);
const countryCode = this.getNodeParameter('gl', itemIndex);
// Validate required parameters
if (!query) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Search query is required.', { itemIndex });
}
// Build URL with query parameters
const url = new URL('https://www.googleapis.com/customsearch/v1');
url.searchParams.append('key', apiKey);
url.searchParams.append('cx', searchEngineId);
url.searchParams.append('q', query);
if (countryCode) {
url.searchParams.append('gl', countryCode);
}
// Make the request
const response = await this.helpers.httpRequest({
method: 'GET',
url: url.toString(),
json: true,
});
// Add the response to return data
returnData.push({
json: response,
pairedItem: itemIndex,
});
}
catch (error) {
// Handle errors
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
pairedItem: itemIndex,
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.GoogleCustomSearch = GoogleCustomSearch;
//# sourceMappingURL=GoogleCustomSearch.node.js.map