From e46619061a2c5c1e018f4207f9768db4e5316bf2 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Tue, 24 Nov 2020 05:22:14 -0500 Subject: [PATCH] :sparkles: Add OpenThesaurus node (#1195) * :sparkles: Add OpenThesaurus node * :zap: Small improvements to OpenThesaurus Node Improvements to #1192 Co-authored-by: Tanay Pant --- .../nodes/OpenThesaurus/GenericFunctions.ts | 44 +++++ .../nodes/OpenThesaurus/OpenThesaurus.node.ts | 167 ++++++++++++++++++ .../nodes/OpenThesaurus/openthesaurus.png | Bin 0 -> 1419 bytes packages/nodes-base/package.json | 1 + 4 files changed, 212 insertions(+) create mode 100644 packages/nodes-base/nodes/OpenThesaurus/GenericFunctions.ts create mode 100644 packages/nodes-base/nodes/OpenThesaurus/OpenThesaurus.node.ts create mode 100644 packages/nodes-base/nodes/OpenThesaurus/openthesaurus.png diff --git a/packages/nodes-base/nodes/OpenThesaurus/GenericFunctions.ts b/packages/nodes-base/nodes/OpenThesaurus/GenericFunctions.ts new file mode 100644 index 000000000..5f50af03e --- /dev/null +++ b/packages/nodes-base/nodes/OpenThesaurus/GenericFunctions.ts @@ -0,0 +1,44 @@ +import { + OptionsWithUri, +} from 'request'; + +import { + IExecuteFunctions, + IExecuteSingleFunctions, + IHookFunctions, + ILoadOptionsFunctions, +} from 'n8n-core'; + +import { + IDataObject, +} from 'n8n-workflow'; + +export async function openThesaurusApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any + try { + let options: OptionsWithUri = { + headers: { + 'User-Agent': 'https://n8n.io', + }, + method, + qs, + body, + uri: uri || `https://www.openthesaurus.de${resource}`, + json: true, + }; + + options = Object.assign({}, options, option); + options.qs.format = 'application/json'; + + return await this.helpers.request!(options); + } catch (error) { + + if (error.response && error.response.body && error.response.body.message) { + // Try to return the error prettier + const errorBody = error.response.body; + throw new Error(`OpenThesaurus error response [${error.statusCode}]: ${errorBody.message}`); + } + + // Expected error data did not get returned so throw the actual error + throw error; + } +} diff --git a/packages/nodes-base/nodes/OpenThesaurus/OpenThesaurus.node.ts b/packages/nodes-base/nodes/OpenThesaurus/OpenThesaurus.node.ts new file mode 100644 index 000000000..e43fd9583 --- /dev/null +++ b/packages/nodes-base/nodes/OpenThesaurus/OpenThesaurus.node.ts @@ -0,0 +1,167 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; + +import { + IDataObject, + INodeExecutionData, + INodeType, + INodeTypeDescription, +} from 'n8n-workflow'; + +import { + openThesaurusApiRequest, +} from './GenericFunctions'; + +export class OpenThesaurus implements INodeType { + description: INodeTypeDescription = { + displayName: 'OpenThesaurus', + name: 'openThesaurus', + icon: 'file:openthesaurus.png', + group: ['output'], + version: 1, + subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', + description: 'Consume OpenThesaurus API', + defaults: { + name: 'OpenThesaurus', + color: '#00ade8', + }, + inputs: ['main'], + outputs: ['main'], + properties: [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + options: [ + { + name: 'Get Synonyms', + value: 'getSynonyms', + description: 'Get synonyms for a German word in German', + }, + ], + default: 'getSynonyms', + description: 'The operation to perform.', + }, + { + displayName: 'Text', + name: 'text', + type: 'string', + default: '', + description: 'The word to get synonyms for', + required: true, + displayOptions: { + show: { + operation: [ + 'getSynonyms', + ], + }, + }, + }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + placeholder: 'Add Options', + displayOptions: { + show: { + operation: [ + 'getSynonyms', + ], + }, + }, + default: {}, + options: [ + { + displayName: 'Baseform', + name: 'baseform', + type: 'boolean', + default: false, + description: 'Specifies the basic form for the search term if it is not already a basic form.', + }, + { + displayName: 'Similar', + name: 'similar', + type: 'boolean', + default: false, + description: 'This also returns up to five similarly written words for each answer.
This is useful to be able to make a suggestion to the user in the event of a possible typing error.', + }, + { + displayName: 'Starts With', + name: 'startswith', + type: 'boolean', + default: false, + description: 'Like substring = true, but only finds words that begin with the specified search term.', + }, + { + displayName: 'Substring', + name: 'substring', + type: 'boolean', + default: false, + description: 'With this, up to ten words are returned for each answer that only contain the search term as a partial word.', + }, + { + displayName: 'Substring From Results', + name: 'substringFromResults', + type: 'number', + default: 0, + description: 'Specifies from which entry the partial word hits are to be returned.
Only works together with substring = true.', + }, + { + displayName: 'Substring Max Results', + name: 'substringMaxResults', + type: 'number', + typeOptions: { + maxValue: 250, + }, + default: 10, + description: 'Specifies how many partial word hits should be returned in total.
Only works together with substring = true.', + }, + { + displayName: 'Subsynsets', + name: 'subsynsets', + type: 'boolean', + default: false, + description: 'Indicates that each synonym group has its (optional) sub-terms supplied.', + }, + { + displayName: 'Supersynsets', + name: 'supersynsets', + type: 'boolean', + default: false, + description: 'Indicates that each synonym group is supplied with its (optional) generic terms.', + }, + ], + }, + ], + }; + + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + const length = items.length as unknown as number; + const qs: IDataObject = {}; + let responseData; + const operation = this.getNodeParameter('operation', 0) as string; + + for (let i = 0; i < length; i++) { + if (operation === 'getSynonyms') { + const text = this.getNodeParameter('text', i) as string; + const options = this.getNodeParameter('options', i) as IDataObject; + + qs.q = text; + + Object.assign(qs, options); + + responseData = await openThesaurusApiRequest.call(this, 'GET', `/synonyme/search`, {}, qs); + responseData = responseData.synsets; + } + if (Array.isArray(responseData)) { + returnData.push.apply(returnData, responseData as IDataObject[]); + } else { + returnData.push(responseData as IDataObject); + } + } + return [this.helpers.returnJsonArray(returnData)]; + } +} diff --git a/packages/nodes-base/nodes/OpenThesaurus/openthesaurus.png b/packages/nodes-base/nodes/OpenThesaurus/openthesaurus.png new file mode 100644 index 0000000000000000000000000000000000000000..374ec03aa117b9349023b9538ec2b34d75da9f3c GIT binary patch literal 1419 zcmV;61$6p}P)kC-s1}#I6&^9y;_0S`9j?AEYY!-JW=5T*%9z(N>7+)}8YRQC2?ILD$ z2I!VdFq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mV?d00001 diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 8cc914097..b08d3ac0f 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -359,6 +359,7 @@ "dist/nodes/MySql/MySql.node.js", "dist/nodes/NextCloud/NextCloud.node.js", "dist/nodes/NoOp.node.js", + "dist/nodes/OpenThesaurus/OpenThesaurus.node.js", "dist/nodes/OpenWeatherMap.node.js", "dist/nodes/Orbit/Orbit.node.js", "dist/nodes/Paddle/Paddle.node.js",