2020-11-24 02:22:14 -08:00
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"]}}' ,
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' ,
} ,
] ,
default : 'getSynonyms' ,
} ,
{
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 ,
2022-05-06 14:01:25 -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 ,
2021-11-25 09:10:06 -08: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-05-06 14:01:25 -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-05-06 14:01:25 -07:00
description : 'With this, 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 ,
2021-11-25 09:10:06 -08: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 ,
2021-11-25 09:10:06 -08: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-05-06 14:01:25 -07:00
description : 'Indicates that 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-05-06 14:01:25 -07:00
description : 'Indicates that 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 ( ) ;
const returnData : IDataObject [ ] = [ ] ;
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 ;
const options = this . getNodeParameter ( 'options' , i ) as IDataObject ;
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
2021-07-19 23:58:54 -07:00
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 ) ;
}
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
returnData . push ( { error : error.message } ) ;
continue ;
}
throw error ;
2020-11-24 02:22:14 -08:00
}
}
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}