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

View file

@ -43,7 +43,7 @@ export class AwsSns implements INodeType {
description: 'Publish a message to a topic', description: 'Publish a message to a topic',
}, },
], ],
default: 'invoke', default: 'publish',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
{ {
@ -107,22 +107,31 @@ export class AwsSns implements INodeType {
// select them easily // select them easily
async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let data;
try { try {
var data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics'); data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics');
} catch (err) { } catch (err) {
throw new Error(`AWS Error: ${err}`); throw new Error(`AWS Error: ${err}`);
} }
const topics = data.ListTopicsResponse.ListTopicsResult.Topics.member; let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
for (let topic of topics) {
let topicArn = topic.TopicArn as string; if (!Array.isArray(topics)) {
let topicName = topicArn.split(':')[5]; // 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({ returnData.push({
name: topicName, name: topicName,
value: topicArn, value: topicArn,
}); });
} }
return returnData; return returnData;
} }
}, },
@ -140,8 +149,9 @@ export class AwsSns implements INodeType {
'Message=' + this.getNodeParameter('message', i) as string, 'Message=' + this.getNodeParameter('message', i) as string,
]; ];
let responseData;
try { 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) { } catch (err) {
throw new Error(`AWS Error: ${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 { import {
IExecuteFunctions, IExecuteFunctions,
IHookFunctions, IHookFunctions,
ILoadOptionsFunctions, ILoadOptionsFunctions,
} from 'n8n-core'; } 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 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'); const credentials = this.getCredentials('aws');
@ -14,15 +15,15 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
throw new Error('No credentials got returned!'); 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 // Sign AWS API request with the user credentials
const signOpts = {headers: headers || {}, host: endpoint, method: method, path: path, body: body} const signOpts = {headers: headers || {}, host: endpoint, method, path, body};
sign(signOpts, {accessKeyId: `${credentials.accessKeyId}`, secretAccessKey: `${credentials.secretAccessKey}`}) sign(signOpts, {accessKeyId: `${credentials.accessKeyId}`, secretAccessKey: `${credentials.secretAccessKey}`});
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: signOpts.headers, headers: signOpts.headers,
method: method, method,
uri: `https://${endpoint}${signOpts.path}`, uri: `https://${endpoint}${signOpts.path}`,
body: signOpts.body, 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; const errorMessage = error.response.body.message || error.response.body.Message;
if (error.statusCode === 403) { 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!'); 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')) { } 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!'); throw new Error('The AWS credentials are not valid!');
@ -54,10 +55,11 @@ export async function awsApiRequestREST(this: IHookFunctions | IExecuteFunctions
try { try {
return JSON.parse(response); return JSON.parse(response);
} catch (e) { } 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 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); const response = await awsApiRequest.call(this, service, method, path, body, headers);
try { try {
@ -70,6 +72,6 @@ export async function awsApiRequestSOAP(this: IHookFunctions | IExecuteFunctions
}); });
}); });
} catch (e) { } catch (e) {
return response return response;
} }
} }