2023-09-19 03:16:35 -07:00
import type {
FieldType ,
IDataObject ,
IExecuteFunctions ,
INode ,
INodeExecutionData ,
2024-10-28 03:37:23 -07:00
ISupplyDataFunctions ,
2023-09-19 03:16:35 -07:00
} from 'n8n-workflow' ;
2023-12-05 02:17:08 -08:00
import {
2024-02-06 09:34:34 -08:00
ApplicationError ,
2023-12-05 02:17:08 -08:00
NodeOperationError ,
2024-02-06 09:34:34 -08:00
deepCopy ,
2024-06-03 02:09:48 -07:00
getValueDescription ,
2023-12-05 02:17:08 -08:00
jsonParse ,
validateFieldType ,
} from 'n8n-workflow' ;
2023-09-19 03:16:35 -07:00
import get from 'lodash/get' ;
2024-02-06 09:34:34 -08:00
import set from 'lodash/set' ;
2023-09-19 03:16:35 -07:00
import unset from 'lodash/unset' ;
2024-06-20 03:09:23 -07:00
import { getResolvables , sanitizeDataPathKey } from '../../../../utils/utilities' ;
2024-02-06 09:34:34 -08:00
import type { SetNodeOptions } from './interfaces' ;
2023-09-19 03:16:35 -07:00
import { INCLUDE } from './interfaces' ;
const configureFieldHelper = ( dotNotation? : boolean ) = > {
if ( dotNotation !== false ) {
return {
set : ( item : IDataObject , key : string , value : IDataObject ) = > {
set ( item , key , value ) ;
} ,
get : ( item : IDataObject , key : string ) = > {
return get ( item , key ) ;
} ,
unset : ( item : IDataObject , key : string ) = > {
unset ( item , key ) ;
} ,
} ;
} else {
return {
set : ( item : IDataObject , key : string , value : IDataObject ) = > {
2024-06-20 03:09:23 -07:00
item [ sanitizeDataPathKey ( item , key ) ] = value ;
2023-09-19 03:16:35 -07:00
} ,
get : ( item : IDataObject , key : string ) = > {
2024-06-20 03:09:23 -07:00
return item [ sanitizeDataPathKey ( item , key ) ] ;
2023-09-19 03:16:35 -07:00
} ,
unset : ( item : IDataObject , key : string ) = > {
2024-06-20 03:09:23 -07:00
delete item [ sanitizeDataPathKey ( item , key ) ] ;
2023-09-19 03:16:35 -07:00
} ,
} ;
}
} ;
export function composeReturnItem (
2024-10-28 03:37:23 -07:00
this : IExecuteFunctions | ISupplyDataFunctions ,
2023-09-19 03:16:35 -07:00
itemIndex : number ,
inputItem : INodeExecutionData ,
newFields : IDataObject ,
options : SetNodeOptions ,
2024-06-18 01:32:51 -07:00
nodeVersion : number ,
2023-09-19 03:16:35 -07:00
) {
const newItem : INodeExecutionData = {
json : { } ,
2023-10-10 08:36:20 -07:00
pairedItem : { item : itemIndex } ,
2023-09-19 03:16:35 -07:00
} ;
2024-06-18 01:32:51 -07:00
const includeBinary =
( nodeVersion >= 3.4 && ! options . stripBinary && options . include !== 'none' ) ||
( nodeVersion < 3.4 && ! ! options . includeBinary ) ;
if ( includeBinary && inputItem . binary !== undefined ) {
2023-09-19 03:16:35 -07:00
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
newItem . binary = { } ;
Object . assign ( newItem . binary , inputItem . binary ) ;
}
const fieldHelper = configureFieldHelper ( options . dotNotation ) ;
switch ( options . include ) {
case INCLUDE . ALL :
newItem . json = deepCopy ( inputItem . json ) ;
break ;
case INCLUDE . SELECTED :
const includeFields = ( this . getNodeParameter ( 'includeFields' , itemIndex ) as string )
. split ( ',' )
. map ( ( item ) = > item . trim ( ) )
. filter ( ( item ) = > item ) ;
for ( const key of includeFields ) {
const fieldValue = fieldHelper . get ( inputItem . json , key ) as IDataObject ;
let keyToSet = key ;
if ( options . dotNotation !== false && key . includes ( '.' ) ) {
keyToSet = key . split ( '.' ) . pop ( ) as string ;
}
fieldHelper . set ( newItem . json , keyToSet , fieldValue ) ;
}
break ;
case INCLUDE . EXCEPT :
const excludeFields = ( this . getNodeParameter ( 'excludeFields' , itemIndex ) as string )
. split ( ',' )
. map ( ( item ) = > item . trim ( ) )
. filter ( ( item ) = > item ) ;
const inputData = deepCopy ( inputItem . json ) ;
for ( const key of excludeFields ) {
fieldHelper . unset ( inputData , key ) ;
}
newItem . json = inputData ;
break ;
case INCLUDE . NONE :
break ;
default :
2023-12-05 02:17:08 -08:00
throw new ApplicationError ( ` The include option " ${ options . include } " is not known! ` , {
level : 'warning' ,
} ) ;
2023-09-19 03:16:35 -07:00
}
for ( const key of Object . keys ( newFields ) ) {
fieldHelper . set ( newItem . json , key , newFields [ key ] as IDataObject ) ;
}
return newItem ;
}
export const parseJsonParameter = (
jsonData : string | IDataObject ,
node : INode ,
i : number ,
entryName? : string ,
) = > {
let returnData : IDataObject ;
const location = entryName ? ` entry " ${ entryName } " inside 'Fields to Set' ` : "'JSON Output'" ;
if ( typeof jsonData === 'string' ) {
try {
returnData = jsonParse < IDataObject > ( jsonData ) ;
} catch ( error ) {
let recoveredData = '' ;
try {
recoveredData = jsonData
. replace ( /'/g , '"' ) // Replace single quotes with double quotes
. replace ( /(['"])?([a-zA-Z0-9_]+)(['"])?:/g , '"$2":' ) // Wrap keys in double quotes
. replace ( /,\s*([\]}])/g , '$1' ) // Remove trailing commas from objects
. replace ( /,+$/ , '' ) ; // Remove trailing comma
returnData = jsonParse < IDataObject > ( recoveredData ) ;
} catch ( err ) {
const description =
recoveredData === jsonData ? jsonData : ` ${ recoveredData } ; \ n Original input: ${ jsonData } ` ;
throw new NodeOperationError ( node , ` The ${ location } in item ${ i } contains invalid JSON ` , {
description ,
} ) ;
}
}
} else {
returnData = jsonData ;
}
if ( returnData === undefined || typeof returnData !== 'object' || Array . isArray ( returnData ) ) {
throw new NodeOperationError (
node ,
` The ${ location } in item ${ i } does not contain a valid JSON object ` ,
) ;
}
return returnData ;
} ;
export const validateEntry = (
2024-02-06 09:34:34 -08:00
name : string ,
type : FieldType ,
value : unknown ,
2023-09-19 03:16:35 -07:00
node : INode ,
itemIndex : number ,
ignoreErrors = false ,
2023-10-03 01:18:33 -07:00
nodeVersion? : number ,
2023-09-19 03:16:35 -07:00
) = > {
2024-02-06 09:34:34 -08:00
if ( nodeVersion && nodeVersion >= 3.2 && ( value === undefined || value === null ) ) {
2023-10-12 08:07:12 -07:00
return { name , value : null } ;
}
2023-10-03 01:18:33 -07:00
const description = ` To fix the error try to change the type for the field " ${ name } " or activate the option “Ignore Type Conversion Errors” to apply a less strict type validation ` ;
2024-02-06 09:34:34 -08:00
if ( type === 'string' ) {
if ( nodeVersion && nodeVersion > 3 && ( value === undefined || value === null ) ) {
2023-10-03 01:18:33 -07:00
if ( ignoreErrors ) {
return { name , value : null } ;
} else {
throw new NodeOperationError (
node ,
2024-06-03 02:09:48 -07:00
` ' ${ name } ' expects a ${ type } but we got ${ getValueDescription ( value ) } [item ${ itemIndex } ] ` ,
2023-10-03 01:18:33 -07:00
{ description } ,
) ;
}
2024-02-06 09:34:34 -08:00
} else if ( typeof value === 'object' ) {
value = JSON . stringify ( value ) ;
2023-09-19 03:16:35 -07:00
} else {
2024-02-06 09:34:34 -08:00
value = String ( value ) ;
2023-09-19 03:16:35 -07:00
}
}
2024-02-06 09:34:34 -08:00
const validationResult = validateFieldType ( name , value , type ) ;
2023-09-19 03:16:35 -07:00
if ( ! validationResult . valid ) {
if ( ignoreErrors ) {
2024-05-21 06:04:20 -07:00
return { name , value : value ? ? null } ;
2023-09-19 03:16:35 -07:00
} else {
const message = ` ${ validationResult . errorMessage } [item ${ itemIndex } ] ` ;
throw new NodeOperationError ( node , message , {
itemIndex ,
description ,
} ) ;
}
}
2024-02-06 09:34:34 -08:00
return {
name ,
2024-05-21 06:04:20 -07:00
value : validationResult.newValue ? ? null ,
2024-02-06 09:34:34 -08:00
} ;
2023-09-19 03:16:35 -07:00
} ;
2024-10-28 03:37:23 -07:00
export function resolveRawData (
this : IExecuteFunctions | ISupplyDataFunctions ,
rawData : string ,
i : number ,
) {
2023-09-19 03:16:35 -07:00
const resolvables = getResolvables ( rawData ) ;
let returnData : string = rawData ;
if ( resolvables . length ) {
for ( const resolvable of resolvables ) {
const resolvedValue = this . evaluateExpression ( ` ${ resolvable } ` , i ) ;
if ( typeof resolvedValue === 'object' && resolvedValue !== null ) {
returnData = returnData . replace ( resolvable , JSON . stringify ( resolvedValue ) ) ;
} else {
returnData = returnData . replace ( resolvable , resolvedValue as string ) ;
}
}
}
return returnData ;
}