Remove aws-sdk dependency

This commit is contained in:
Matheus Cansian 2019-10-15 14:21:48 -03:00
parent 39c0b08d9d
commit 0fcbe409b6
4 changed files with 41 additions and 45 deletions

View file

@ -8,9 +8,7 @@ import {
IDataObject IDataObject
} from 'n8n-workflow'; } from 'n8n-workflow';
import { awsApiRequest } from './GenericFunctions'; import { awsApiRequestREST } from './GenericFunctions';
import { Lambda } from 'aws-sdk';
export class AwsLambda implements INodeType { export class AwsLambda implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -91,7 +89,7 @@ export class AwsLambda implements INodeType {
async getFunctions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getFunctions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
try { try {
var data = await awsApiRequest.call(this, 'lambda', 'GET', '/2015-03-31/functions/'); var 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}`);
} }
@ -121,8 +119,7 @@ export class AwsLambda implements INodeType {
}; };
try { try {
var responseData = await awsApiRequestREST.call(
var responseData = await awsApiRequest.call(
this, this,
'lambda', 'lambda',
'POST', 'POST',

View file

@ -8,9 +8,7 @@ import {
IDataObject IDataObject
} from 'n8n-workflow'; } from 'n8n-workflow';
import { awsConfigCredentials } from './GenericFunctions'; import { awsApiRequestSOAP } from './GenericFunctions';
import { SNS } from 'aws-sdk';
export class AwsSns implements INodeType { export class AwsSns implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -51,6 +49,7 @@ export class AwsSns implements INodeType {
name: 'subject', name: 'subject',
type: 'string', type: 'string',
default: '', default: '',
required: true,
description: 'Subject when the message is delivered to email endpoints', description: 'Subject when the message is delivered to email endpoints',
}, },
{ {
@ -72,18 +71,15 @@ export class AwsSns 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[]> {
await awsConfigCredentials.call(this);
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let sns = new SNS();
try { try {
var data = await sns.listTopics({}).promise(); var 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}`);
} }
for (let topic of data.Topics!) { const topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
for (let topic of topics) {
let topicArn = topic.TopicArn as string; let topicArn = topic.TopicArn as string;
let topicName = topicArn.split(':')[5]; let topicName = topicArn.split(':')[5];
@ -102,22 +98,19 @@ export class AwsSns implements INodeType {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
await awsConfigCredentials.call(this);
const sns = new SNS();
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
const params = { const params = [
TopicArn: this.getNodeParameter('topic', i) as string, 'TopicArn=' + this.getNodeParameter('topic', i) as string,
Subject: this.getNodeParameter('subject', i) as string, 'Subject=' + this.getNodeParameter('subject', i) as string,
Message: this.getNodeParameter('message', i) as string, 'Message=' + this.getNodeParameter('message', i) as string,
}; ];
try { try {
var responseData = await sns.publish(params).promise(); var 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}`);
} }
returnData.push({MessageId: responseData.MessageId} as IDataObject); returnData.push({MessageId: responseData.PublishResponse.PublishResult.MessageId} as IDataObject);
} }
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];

View file

@ -4,27 +4,12 @@ import {
ILoadOptionsFunctions, ILoadOptionsFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { config } from 'aws-sdk';
import { OptionsWithUri } from 'request'; import { OptionsWithUri } from 'request';
import { sign } from 'aws4'; import { sign } from 'aws4';
import { parseString } from 'xml2js';
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}`,
});
}
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');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
@ -42,9 +27,8 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
body: signOpts.body, body: signOpts.body,
}; };
let response: string
try { try {
response = await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -57,12 +41,35 @@ export async function awsApiRequest(this: IHookFunctions | IExecuteFunctions | I
} }
} }
if (errorMessage !== undefined) {
throw errorMessage; throw errorMessage;
} }
throw error.response.body;
}
}
export async function awsApiRequestREST(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 { 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
const response = await awsApiRequest.call(this, service, method, path, body, headers);
try {
return await new Promise((resolve, reject) => {
parseString(response, { explicitArray: false }, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
} catch (e) {
return response
}
}

View file

@ -128,7 +128,6 @@
}, },
"dependencies": { "dependencies": {
"aws4": "^1.8.0", "aws4": "^1.8.0",
"aws-sdk": "^2.543.0",
"basic-auth": "^2.0.1", "basic-auth": "^2.0.1",
"cron": "^1.6.0", "cron": "^1.6.0",
"glob-promise": "^3.4.0", "glob-promise": "^3.4.0",