2023-01-27 03:22:44 -08:00
import type {
2023-03-09 09:13:15 -08:00
IDataObject ,
2021-07-17 05:14:12 -07:00
IExecuteFunctions ,
IHookFunctions ,
ILoadOptionsFunctions ,
IWebhookFunctions ,
2023-03-09 09:13:15 -08:00
IHttpRequestOptions ,
INodeExecutionData ,
2024-02-14 07:29:09 -08:00
IHttpRequestMethods ,
2023-03-09 09:13:15 -08:00
} from 'n8n-workflow' ;
2023-12-05 02:17:08 -08:00
import { ApplicationError , deepCopy } from 'n8n-workflow' ;
2021-07-17 05:14:12 -07:00
2023-01-27 03:22:44 -08:00
import type { 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 ,
2024-02-14 07:29:09 -08:00
method : IHttpRequestMethods ,
2022-08-01 13:47:55 -07:00
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 (
2023-02-27 19:39:43 -08:00
( await this . helpers . requestWithAuthentication . call ( this , 'aws' , requestOptions ) ) as string ,
2022-08-23 10:02:32 -07:00
) ;
2021-07-17 05:14:12 -07:00
} catch ( error ) {
2023-11-29 04:44:13 -08:00
const statusCode = ( error . statusCode || error . cause ? . statusCode ) as number ;
let errorMessage =
2022-12-02 12:54:28 -08:00
error . response ? . body ? . message || error . response ? . body ? . Message || error . message ;
2023-11-29 04:44:13 -08:00
if ( statusCode === 403 ) {
2021-07-17 05:14:12 -07:00
if ( errorMessage === 'The security token included in the request is invalid.' ) {
2023-12-05 02:17:08 -08:00
throw new ApplicationError ( 'The AWS credentials are not valid!' , { level : 'warning' } ) ;
2022-08-01 13:47:55 -07:00
} else if (
errorMessage . startsWith (
'The request signature we calculated does not match the signature you provided' ,
)
) {
2023-12-05 02:17:08 -08:00
throw new ApplicationError ( 'The AWS credentials are not valid!' , { level : 'warning' } ) ;
2021-07-17 05:14:12 -07:00
}
}
2023-11-29 04:44:13 -08:00
if ( error . cause ? . error ) {
try {
errorMessage = JSON . parse ( error . cause ? . error ) . message ;
} catch ( ex ) { }
}
2023-12-05 02:17:08 -08:00
throw new ApplicationError ( ` AWS error response [ ${ statusCode } ]: ${ errorMessage } ` , {
level : 'warning' ,
} ) ;
2021-07-17 05:14:12 -07:00
}
}
2022-08-01 13:47:55 -07:00
export async function awsApiRequestAllItems (
this : IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions ,
service : string ,
2024-02-14 07:29:09 -08:00
method : IHttpRequestMethods ,
2022-08-01 13:47:55 -07:00
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 ;
}
2023-02-27 19:39:43 -08:00
returnData . push ( . . . ( responseData . Items as IDataObject [ ] ) ) ;
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 ;
}