n8n/packages/nodes-base/nodes/Aws/AwsSns.node.ts
Iván Ovejero 2b74b6238e
Deprecate step size and node color (#2586)
* 🔥 Deprecate numberStepSize

* 🔥 Deprecate color in non-FA nodes

*  Minor node name fixes

* 📦 Update package-lock.json

*  Restore Merge node color

* 👕 Fix lint
2021-12-23 13:30:35 +01:00

164 lines
3.6 KiB
TypeScript

import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import { awsApiRequestSOAP } from './GenericFunctions';
export class AwsSns implements INodeType {
description: INodeTypeDescription = {
displayName: 'AWS SNS',
name: 'awsSns',
icon: 'file:sns.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["topic"]}}',
description: 'Sends data to AWS SNS',
defaults: {
name: 'AWS SNS',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'aws',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Publish',
value: 'publish',
description: 'Publish a message to a topic',
},
],
default: 'publish',
description: 'The operation to perform.',
},
{
displayName: 'Topic',
name: 'topic',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTopics',
},
displayOptions: {
show: {
operation: [
'publish',
],
},
},
options: [],
default: '',
required: true,
description: 'The topic you want to publish to',
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
displayOptions: {
show: {
operation: [
'publish',
],
},
},
default: '',
required: true,
description: 'Subject when the message is delivered to email endpoints',
},
{
displayName: 'Message',
name: 'message',
type: 'string',
displayOptions: {
show: {
operation: [
'publish',
],
},
},
required: true,
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'The message you want to send',
},
],
};
methods = {
loadOptions: {
// Get all the available topics to display them to user so that he can
// select them easily
async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics');
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;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
try {
const params = [
'TopicArn=' + this.getNodeParameter('topic', i) as string,
'Subject=' + this.getNodeParameter('subject', i) as string,
'Message=' + this.getNodeParameter('message', i) as string,
];
const responseData = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=Publish&' + params.join('&'));
returnData.push({MessageId: responseData.PublishResponse.PublishResult.MessageId} as IDataObject);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}