2022-08-17 08:50:24 -07:00
import { IExecuteFunctions } from 'n8n-core' ;
2020-11-24 02:22:14 -08:00
2022-08-17 08:50:24 -07:00
import { IDataObject , INodeExecutionData , INodeType , INodeTypeDescription } from 'n8n-workflow' ;
2020-11-24 02:22:14 -08:00
2022-08-17 08:50:24 -07:00
import { openThesaurusApiRequest } from './GenericFunctions' ;
2020-11-24 02:22:14 -08:00
export class OpenThesaurus implements INodeType {
description : INodeTypeDescription = {
displayName : 'OpenThesaurus' ,
name : 'openThesaurus' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-11-24 02:22:14 -08:00
icon : 'file:openthesaurus.png' ,
group : [ 'output' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2020-11-24 02:23:05 -08:00
description : 'Get synonmns for German words using the OpenThesaurus API' ,
2020-11-24 02:22:14 -08:00
defaults : {
name : 'OpenThesaurus' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
properties : [
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-11-24 02:22:14 -08:00
options : [
{
name : 'Get Synonyms' ,
value : 'getSynonyms' ,
description : 'Get synonyms for a German word in German' ,
2022-07-10 13:50:51 -07:00
action : 'Get synonyms' ,
2020-11-24 02:22:14 -08:00
} ,
] ,
default : 'getSynonyms' ,
} ,
{
displayName : 'Text' ,
name : 'text' ,
type : 'string' ,
default : '' ,
description : 'The word to get synonyms for' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'getSynonyms' ] ,
2020-11-24 02:22:14 -08:00
} ,
} ,
} ,
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
placeholder : 'Add Options' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'getSynonyms' ] ,
2020-11-24 02:22:14 -08:00
} ,
} ,
default : { } ,
options : [
{
displayName : 'Baseform' ,
name : 'baseform' ,
type : 'boolean' ,
default : false ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
2022-08-17 08:50:24 -07:00
description :
'Specifies the basic form for the search term if it is not already a basic form' ,
2020-11-24 02:22:14 -08:00
} ,
{
displayName : 'Similar' ,
name : 'similar' ,
type : 'boolean' ,
default : false ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
2022-08-17 08:50:24 -07:00
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.' ,
2020-11-24 02:22:14 -08:00
} ,
{
displayName : 'Starts With' ,
name : 'startswith' ,
type : 'boolean' ,
default : false ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
2022-08-17 08:50:24 -07:00
description :
'Like substring = true, but only finds words that begin with the specified search term' ,
2020-11-24 02:22:14 -08:00
} ,
{
displayName : 'Substring' ,
name : 'substring' ,
type : 'boolean' ,
default : false ,
2022-08-17 08:50:24 -07:00
description :
'Whether up to ten words are returned for each answer that only contain the search term as a partial word' ,
2020-11-24 02:22:14 -08:00
} ,
{
displayName : 'Substring From Results' ,
name : 'substringFromResults' ,
type : 'number' ,
default : 0 ,
2022-08-17 08:50:24 -07:00
description :
'Specifies from which entry the partial word hits are to be returned. Only works together with substring = true.' ,
2020-11-24 02:22:14 -08:00
} ,
{
displayName : 'Substring Max Results' ,
name : 'substringMaxResults' ,
type : 'number' ,
typeOptions : {
maxValue : 250 ,
} ,
default : 10 ,
2022-08-17 08:50:24 -07:00
description :
'Specifies how many partial word hits should be returned in total. Only works together with substring = true.' ,
2020-11-24 02:22:14 -08:00
} ,
{
displayName : 'Subsynsets' ,
name : 'subsynsets' ,
type : 'boolean' ,
default : false ,
2022-06-20 07:54:01 -07:00
description : 'Whether each synonym group has its (optional) sub-terms supplied' ,
2020-11-24 02:22:14 -08:00
} ,
{
displayName : 'Supersynsets' ,
name : 'supersynsets' ,
type : 'boolean' ,
default : false ,
2022-06-20 07:54:01 -07:00
description : 'Whether each synonym group is supplied with its (optional) generic terms' ,
2020-11-24 02:22:14 -08:00
} ,
] ,
} ,
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
2022-08-30 08:55:33 -07:00
const returnData : INodeExecutionData [ ] = [ ] ;
2022-04-22 09:29:51 -07:00
const length = items . length ;
2020-11-24 02:22:14 -08:00
const qs : IDataObject = { } ;
let responseData ;
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
for ( let i = 0 ; i < length ; i ++ ) {
2021-07-19 23:58:54 -07:00
try {
if ( operation === 'getSynonyms' ) {
const text = this . getNodeParameter ( 'text' , i ) as string ;
2022-11-18 07:29:44 -08:00
const options = this . getNodeParameter ( 'options' , i ) ;
2020-11-24 02:22:14 -08:00
2021-07-19 23:58:54 -07:00
qs . q = text ;
2020-11-24 02:22:14 -08:00
2021-07-19 23:58:54 -07:00
Object . assign ( qs , options ) ;
2020-11-24 02:22:14 -08:00
2022-08-17 08:50:24 -07:00
responseData = await openThesaurusApiRequest . call (
this ,
'GET' ,
` /synonyme/search ` ,
{ } ,
qs ,
) ;
2021-07-19 23:58:54 -07:00
responseData = responseData . synsets ;
}
2022-08-30 08:55:33 -07:00
const executionData = this . helpers . constructExecutionMetaData (
this . helpers . returnJsonArray ( responseData ) ,
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2021-07-19 23:58:54 -07:00
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
2022-08-30 08:55:33 -07:00
const executionErrorData = this . helpers . constructExecutionMetaData (
this . helpers . returnJsonArray ( { error : error.message } ) ,
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionErrorData ) ;
2021-07-19 23:58:54 -07:00
continue ;
}
throw error ;
2020-11-24 02:22:14 -08:00
}
}
2022-08-30 08:55:33 -07:00
return this . prepareOutputData ( returnData ) ;
2020-11-24 02:22:14 -08:00
}
}