👕 Fix lint issues and some small bugs

This commit is contained in:
Jan Oberhauser 2019-10-16 11:18:39 +02:00
parent 2899d52433
commit c02bf0a7cf
3 changed files with 44 additions and 31 deletions

View file

@ -130,13 +130,15 @@ export class AwsLambda implements INodeType {
loadOptions: {
async getFunctions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let data;
try {
var data = await awsApiRequestREST.call(this, 'lambda', 'GET', '/2015-03-31/functions/');
data = await awsApiRequestREST.call(this, 'lambda', 'GET', '/2015-03-31/functions/');
} catch (err) {
throw new Error(`AWS Error: ${err}`);
}
for (let func of data.Functions!) {
for (const func of data.Functions!) {
returnData.push({
name: func.FunctionName as string,
value: func.FunctionArn as string,
@ -160,8 +162,9 @@ export class AwsLambda implements INodeType {
Qualifier: this.getNodeParameter('qualifier', i) as string,
};
let responseData;
try {
var responseData = await awsApiRequestREST.call(
responseData = await awsApiRequestREST.call(
this,
'lambda',
'POST',
@ -176,19 +179,17 @@ export class AwsLambda implements INodeType {
throw new Error(`AWS Error: ${err}`);
}
if (responseData.errorMessage === undefined) {
returnData.push({
FunctionName: params.FunctionName,
FunctionQualifier: params.Qualifier,
Result: responseData,
} as IDataObject);
if (responseData !== null && responseData.errorMessage !== undefined) {
let errorMessage = responseData.errorMessage;
if (responseData.stackTrace) {
errorMessage += `\n\nStack trace:\n${responseData.stackTrace}`;
}
throw new Error(errorMessage);
} else {
returnData.push({
FunctionName: params.FunctionName,
FunctionQualifier: params.Qualifier,
ErrorMessage: responseData.errorMessage,
ErrorType: responseData.errorType,
ErrorStackTrace: responseData.stackTrace,
result: responseData,
} as IDataObject);
}

View file

@ -43,7 +43,7 @@ export class AwsSns implements INodeType {
description: 'Publish a message to a topic',
},
],
default: 'invoke',
default: 'publish',
description: 'The operation to perform.',
},
{
@ -107,22 +107,31 @@ export class AwsSns implements INodeType {
// select them easily
async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let data;
try {
var data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics');
data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics');
} catch (err) {
throw new Error(`AWS Error: ${err}`);
}
const topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
for (let topic of topics) {
let topicArn = topic.TopicArn as string;
let topicName = topicArn.split(':')[5];
let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
if (!Array.isArray(topics)) {
// If user has only a single topic no array get returned so we make
// one manually to be able to process everything identically
topics = [topics];
}
for (const topic of topics) {
const topicArn = topic.TopicArn as string;
const topicName = topicArn.split(':')[5];
returnData.push({
name: topicName,
value: topicArn,
});
}
return returnData;
}
},
@ -140,8 +149,9 @@ export class AwsSns implements INodeType {
'Message=' + this.getNodeParameter('message', i) as string,
];
let responseData;
try {
var responseData = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=Publish&' + params.join('&'));
responseData = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=Publish&' + params.join('&'));
} catch (err) {
throw new Error(`AWS Error: ${err}`);
}

View file

@ -1,12 +1,13 @@
import { sign } from 'aws4';
import { OptionsWithUri } from 'request';
import { parseString } from 'xml2js';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import { OptionsWithUri } from 'request';
import { sign } from 'aws4';
import { parseString } from 'xml2js';
export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, service: string, method: string, path: string, body?: string, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('aws');
@ -14,15 +15,15 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
throw new Error('No credentials got returned!');
}
const endpoint = `${service}.${credentials.region}.amazonaws.com`
const endpoint = `${service}.${credentials.region}.amazonaws.com`;
// Sign AWS API request with the user credentials
const signOpts = {headers: headers || {}, host: endpoint, method: method, path: path, body: body}
sign(signOpts, {accessKeyId: `${credentials.accessKeyId}`, secretAccessKey: `${credentials.secretAccessKey}`})
const signOpts = {headers: headers || {}, host: endpoint, method, path, body};
sign(signOpts, {accessKeyId: `${credentials.accessKeyId}`, secretAccessKey: `${credentials.secretAccessKey}`});
const options: OptionsWithUri = {
headers: signOpts.headers,
method: method,
method,
uri: `https://${endpoint}${signOpts.path}`,
body: signOpts.body,
};
@ -34,7 +35,7 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
const errorMessage = error.response.body.message || error.response.body.Message;
if (error.statusCode === 403) {
if (errorMessage == 'The security token included in the request is invalid.') {
if (errorMessage === 'The security token included in the request is invalid.') {
throw new Error('The AWS credentials are not valid!');
} else if (errorMessage.startsWith('The request signature we calculated does not match the signature you provided')) {
throw new Error('The AWS credentials are not valid!');
@ -54,10 +55,11 @@ export async function awsApiRequestREST(this: IHookFunctions | IExecuteFunctions
try {
return JSON.parse(response);
} catch (e) {
return response
return response;
}
}
export async function awsApiRequestSOAP(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, service: string, method: string, path: string, body?: string, headers?: object): Promise<any> { // tslint:disable-line:no-any
const response = await awsApiRequest.call(this, service, method, path, body, headers);
try {
@ -70,6 +72,6 @@ export async function awsApiRequestSOAP(this: IHookFunctions | IExecuteFunctions
});
});
} catch (e) {
return response
return response;
}
}