Improve code readability Aws node

This commit is contained in:
Matheus Cansian 2019-10-05 10:27:19 -03:00
parent ec9d65b2e0
commit edb81ef259
2 changed files with 37 additions and 41 deletions

View file

@ -0,0 +1,23 @@
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import { config } from 'aws-sdk';
import { OptionsWithUri } from 'request';
export async function awsConfigCredentials(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions): Promise<void> {
const credentials = this.getCredentials('aws');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
config.update({
region: `${credentials.region}`,
accessKeyId: `${credentials.accessKeyId}`,
secretAccessKey: `${credentials.secretAccessKey}`,
});
}

View file

@ -1,8 +1,3 @@
import {
SNS,
config,
AWSError,
} from 'aws-sdk';
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
INodeTypeDescription, INodeTypeDescription,
@ -13,6 +8,10 @@ import {
IDataObject IDataObject
} from 'n8n-workflow'; } from 'n8n-workflow';
import { awsConfigCredentials } from './GenericFunctions';
import { SNS } from 'aws-sdk';
export class Sns implements INodeType { export class Sns implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Amazon SNS', displayName: 'Amazon SNS',
@ -24,7 +23,7 @@ export class Sns implements INodeType {
description: 'Sends data to Amazon SNS', description: 'Sends data to Amazon SNS',
defaults: { defaults: {
name: 'Amazon SNS', name: 'Amazon SNS',
color: '#BB2244', color: '#FF9900',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
@ -73,17 +72,7 @@ export class Sns implements INodeType {
// Get all the available topics to display them to user so that he can // Get all the available topics to display them to user so that he can
// select them easily // select them easily
async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const credentials = this.getCredentials('aws'); await awsConfigCredentials.call(this);
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
config.update({
region: `${credentials.region}`,
accessKeyId: `${credentials.accessKeyId}`,
secretAccessKey: `${credentials.secretAccessKey}`,
});
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
@ -95,8 +84,9 @@ export class Sns implements INodeType {
} }
for (let topic of data.Topics!) { for (let topic of data.Topics!) {
let topicArn = topic.TopicArn!; let topicArn = topic.TopicArn as string;
let topicName = topicArn.split(':')[5]; let topicName = topicArn.split(':')[5];
returnData.push({ returnData.push({
name: topicName, name: topicName,
value: topicArn, value: topicArn,
@ -112,31 +102,14 @@ export class Sns implements INodeType {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const credentials = this.getCredentials('aws'); await awsConfigCredentials.call(this);
if (credentials === undefined) { const sns = new SNS();
throw new Error('No credentials got returned!');
}
let topicArn: string;
let subject: string;
let message: string;
config.update({
region: `${credentials.region}`,
accessKeyId: `${credentials.accessKeyId}`,
secretAccessKey: `${credentials.secretAccessKey}`,
});
let sns = new SNS();
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
topicArn = this.getNodeParameter('topic', i) as string; const params = {
subject = this.getNodeParameter('subject', i) as string; TopicArn: this.getNodeParameter('topic', i) as string,
message = this.getNodeParameter('message', i) as string; Subject: this.getNodeParameter('subject', i) as string,
Message: this.getNodeParameter('message', i) as string,
let params = {
Message: message,
Subject: subject,
TopicArn: topicArn,
}; };
try { try {