2021-07-17 05:14:12 -07:00
import {
IExecuteFunctions ,
IHookFunctions ,
ILoadOptionsFunctions ,
IWebhookFunctions ,
} from 'n8n-core' ;
2022-11-08 06:28:21 -08:00
import { deepCopy , IDataObject , IHttpRequestOptions , INodeExecutionData } from 'n8n-workflow' ;
2021-07-17 05:14:12 -07:00
2022-08-01 13:47:55 -07:00
import { IRequestBody } from './types' ;
2021-07-17 05:14:12 -07:00
2022-08-01 13:47:55 -07:00
export async function awsApiRequest (
this : IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions ,
service : string ,
method : string ,
path : string ,
body? : object | IRequestBody ,
headers? : object ,
) : Promise < any > {
2021-08-20 09:57:30 -07:00
const credentials = await this . getCredentials ( 'aws' ) ;
2022-08-23 10:02:32 -07:00
const requestOptions = {
qs : {
2022-08-01 13:47:55 -07:00
service ,
2022-08-23 10:02:32 -07:00
path ,
2022-08-01 13:47:55 -07:00
} ,
2022-08-23 10:02:32 -07:00
method ,
body : JSON.stringify ( body ) ,
url : '' ,
headers ,
region : credentials?.region as string ,
} as IHttpRequestOptions ;
2021-07-17 05:14:12 -07:00
try {
2022-08-23 10:02:32 -07:00
return JSON . parse (
await this . helpers . requestWithAuthentication . call ( this , 'aws' , requestOptions ) ,
) ;
2021-07-17 05:14:12 -07:00
} catch ( error ) {
2022-07-10 03:16:34 -07:00
const errorMessage =
2022-12-02 12:54:28 -08:00
error . response ? . body ? . message || error . response ? . body ? . Message || error . message ;
2021-07-17 05:14:12 -07:00
if ( error . statusCode === 403 ) {
if ( errorMessage === 'The security token included in the request is invalid.' ) {
throw new Error ( 'The AWS credentials are not valid!' ) ;
2022-08-01 13:47:55 -07:00
} else if (
errorMessage . startsWith (
'The request signature we calculated does not match the signature you provided' ,
)
) {
2021-07-17 05:14:12 -07:00
throw new Error ( 'The AWS credentials are not valid!' ) ;
}
}
throw new Error ( ` AWS error response [ ${ error . statusCode } ]: ${ errorMessage } ` ) ;
}
}
2022-08-01 13:47:55 -07:00
export async function awsApiRequestAllItems (
this : IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions ,
service : string ,
method : string ,
path : string ,
body? : IRequestBody ,
headers? : object ,
) : Promise < any > {
2021-07-17 05:14:12 -07:00
const returnData : IDataObject [ ] = [ ] ;
let responseData ;
do {
2022-12-21 20:36:49 -08:00
const originalHeaders = Object . assign ( { } , headers ) ; //The awsapirequest function adds the hmac signature to the headers, if we pass the modified headers back in on the next call it will fail with invalid signature
responseData = await awsApiRequest . call ( this , service , method , path , body , originalHeaders ) ;
2021-07-17 05:14:12 -07:00
if ( responseData . LastEvaluatedKey ) {
body ! . ExclusiveStartKey = responseData . LastEvaluatedKey ;
}
returnData . push ( . . . responseData . Items ) ;
2022-08-01 13:47:55 -07:00
} while ( responseData . LastEvaluatedKey !== undefined ) ;
2021-07-17 05:14:12 -07:00
return returnData ;
}
export function copyInputItem ( item : INodeExecutionData , properties : string [ ] ) : IDataObject {
// Prepare the data to insert and copy it to be returned
2022-12-02 12:54:28 -08:00
const newItem : IDataObject = { } ;
2021-07-17 05:14:12 -07:00
for ( const property of properties ) {
if ( item . json [ property ] === undefined ) {
newItem [ property ] = null ;
} else {
2022-10-21 08:24:58 -07:00
newItem [ property ] = deepCopy ( item . json [ property ] ) ;
2021-07-17 05:14:12 -07:00
}
}
return newItem ;
}