UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

238 lines 10.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GooglePerspective = void 0; const iso_639_1_1 = __importDefault(require("iso-639-1")); const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class GooglePerspective { description = { displayName: 'Google Perspective', name: 'googlePerspective', icon: { light: 'file:googlePerspective.svg', dark: 'file:googlePerspective.dark.svg' }, group: ['transform'], version: 1, description: 'Consume Google Perspective API', subtitle: '={{$parameter["operation"]}}', defaults: { name: 'Google Perspective', }, usableAsTool: true, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'googlePerspectiveOAuth2Api', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Analyze Comment', value: 'analyzeComment', }, ], default: 'analyzeComment', }, { displayName: 'Text', name: 'text', type: 'string', default: '', required: true, displayOptions: { show: { operation: ['analyzeComment'], }, }, }, { displayName: 'Attributes to Analyze', name: 'requestedAttributesUi', type: 'fixedCollection', default: {}, typeOptions: { multipleValues: true, }, placeholder: 'Add Atrribute', required: true, displayOptions: { show: { operation: ['analyzeComment'], }, }, options: [ { displayName: 'Properties', name: 'requestedAttributesValues', values: [ { displayName: 'Attribute Name', name: 'attributeName', type: 'options', options: [ { name: 'Flirtation', value: 'flirtation', }, { name: 'Identity Attack', value: 'identity_attack', }, { name: 'Insult', value: 'insult', }, { name: 'Profanity', value: 'profanity', }, { name: 'Severe Toxicity', value: 'severe_toxicity', }, { name: 'Sexually Explicit', value: 'sexually_explicit', }, { name: 'Threat', value: 'threat', }, { name: 'Toxicity', value: 'toxicity', }, ], description: 'Attribute to analyze in the text. Details <a href="https://developers.perspectiveapi.com/s/about-the-api-attributes-and-languages">here</a>.', default: 'flirtation', }, { displayName: 'Score Threshold', name: 'scoreThreshold', type: 'number', typeOptions: { numberPrecision: 2, minValue: 0, maxValue: 1, }, description: 'Score above which to return results. At zero, all scores are returned.', default: 0, }, ], }, ], }, { displayName: 'Options', name: 'options', type: 'collection', displayOptions: { show: { operation: ['analyzeComment'], }, }, default: {}, placeholder: 'Add option', options: [ { displayName: 'Language Name or ID', name: 'languages', type: 'options', typeOptions: { loadOptionsMethod: 'getLanguages', }, default: '', description: 'Languages of the text input. If unspecified, the API will auto-detect the comment language. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.', }, ], }, ], }; methods = { loadOptions: { // Get all the available languages to display them to user so that they can // select them easily async getLanguages() { const returnData = []; const supportedLanguages = [ 'English', 'Spanish', 'French', 'German', 'Portuguese', 'Italian', 'Russian', ]; const languages = iso_639_1_1.default.getAllNames().filter((language) => supportedLanguages.includes(language)); for (const language of languages) { const languageName = language; const languageId = iso_639_1_1.default.getCode(language); returnData.push({ name: languageName, value: languageId, }); } return returnData; }, }, }; async execute() { const items = this.getInputData(); const operation = this.getNodeParameter('operation', 0); const returnData = []; let responseData; for (let i = 0; i < items.length; i++) { try { if (operation === 'analyzeComment') { // https://developers.perspectiveapi.com/s/about-the-api-methods const attributes = this.getNodeParameter('requestedAttributesUi.requestedAttributesValues', i, []); if (!attributes.length) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Please enter at least one attribute to analyze.', { itemIndex: i }); } const requestedAttributes = attributes.reduce((acc, cur) => { return Object.assign(acc, { [cur.attributeName.toUpperCase()]: { scoreType: 'probability', scoreThreshold: cur.scoreThreshold, }, }); }, {}); const body = { comment: { type: 'PLAIN_TEXT', text: this.getNodeParameter('text', i), }, requestedAttributes, }; const { languages } = this.getNodeParameter('options', i); if (languages?.length) { body.languages = languages; } responseData = await GenericFunctions_1.googleApiRequest.call(this, 'POST', '/v1alpha1/comments:analyze', body); } } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw error; } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } return [returnData]; } } exports.GooglePerspective = GooglePerspective; //# sourceMappingURL=GooglePerspective.node.js.map